示例#1
0
def SMBStart():
	global conn
	global io_loop
	logging.basicConfig()
	conn = Connection(host='localhost')
	conn.connect(on_connect)
	io_loop = IOLoop.instance()
	io_loop.start()
示例#2
0
class RBClient(object):
    def __init__(self, app, host, queue, key):
        self.app = app
        self.queue = queue
        self.host = host
        self.connected = False
        self.connecting = False
        self.connection = None
        self.channel = None
        self.key = key
        self.mq = {}

    def connect(self):
        if self.connecting:
            return
        self.connecting = True
        self.connection = Connection(
            host=self.host,
        )
        self.connection.connect(self.on_connected)
        self.connection.on_disconnect = self.on_closed

    def on_connected(self):
        self.connected = True
        self.channel = self.connection.channel()
        self.channel.exchange_declare(
            exchange=self.queue,
            type='topic',
            durable=False,
        )
        self.channel.queue_declare(
            queue=self.queue,
            auto_delete=True,
            durable=False,
        )
        self.channel.queue_bind(
            exchange=self.queue,
            queue=self.queue,
            routing_key=self.key,
        )
        self.channel.consume(
            self.queue,
            self.callback,
            no_ack=True,
        )

    def on_basic_cancel(self, frame):
        self.connection.close()

    def on_closed(self, connection):
        tornado.ioloop.IOLoop.instance().stop()

    def callback(self, msg):
        subscribers = self.mq.get(msg.rx_data.routing_key, None)
        if subscribers:
            for subscriber in subscribers:
                subscriber.release(msg.body)
示例#3
0
def start(on_connect):
	global conn
	global io_loop

	logging.basicConfig()
	conn = Connection(host=str_Server)
	conn.connect(on_connect)
	io_loop = IOLoop.instance()
	io_loop.start()
示例#4
0
def Start():
	global conn
	global io_loop
	logging.basicConfig()
	conn = Connection(host='localhost')
	conn.connect(on_connect)
	io_loop = IOLoop.instance()
	io_loop.start()
	print ' [*] Waiting for messages. To exit press CTRL+C'
def init_worker():
    global conn
    conn = Connection(host='localhost')
    conn.connect(push_connect)
    io_loop = IOLoop.instance()
    print '=> Waiting for push notifications to notify all devices.'
    try:
        io_loop.start()
    except KeyboardInterrupt:
        conn.close(io_loop.stop)
示例#6
0
def main():
    global ch, conn
    conn = Connection(host='localhost')
    conn.connect(on_amqp_connection)

    application = web.Application([
        (r"/round_trip", RoundTripHandler),
    ])

    http_server = httpserver.HTTPServer(application)
    http_server.listen(8001)
    ioloop.IOLoop.instance().start()
示例#7
0
 def __init__(self, playerId, position, otherPlayers, onQuit = None):
   # Acking for messages. You cannot send a message until the previous 
   # message has been ack'd (in a message, not just ack'd to the MQ). This
   # guarantees message order, which RabbitMQ does not otherwise do.
   self.waitlet = None
   self.waitingForAck = False
   self.blockingForAck = False
   self.messages = []
   self.playerId = playerId
   self.inQueue = "from-%s" % playerId
   self.outQueue = "to-%s" % playerId    
   
   # Number of messages we've recieved. Used to track whether the client has
   # stopped talking to us.
   self.msgCount = 0
   
   # Callback -- If we lose the player, call this with our position.
   self.onQuit = onQuit
   
   # If we've seen our hand we can't bid double nil. We (MQView) have to guarantee 
   # this, since we get given the hand before 00 bidding time. 
   self.seenHand = False
   
   self.position = position    
   self.otherPlayers = dict((self._getRelPosition(position), name) for (position, name) in otherPlayers)
   
   # Connect to the MQ.
   self.mq = None
   self.mqConn = Connection(host='localhost')
   self.mqConn.connect(self._onConnect)
示例#8
0
 def connect(self):
     if self.connecting:
         return
     self.connecting = True
     self.connection = Connection(
         host=self.host,
     )
     self.connection.connect(self.on_connected)
     self.connection.on_disconnect = self.on_closed
示例#9
0
文件: comet.py 项目: pimterry/50plus2
 def _handler(self, gameId, userId, alreadyJoined = False, messagesToSend=[]):
   self.cancelled = False
   self.gameId = gameId
   self.userId = userId
   self.alreadyJoined = alreadyJoined
   self.messagesToSend = messagesToSend
   
   self._pingTimeout = None
   
   self.mqConn = Connection(host='localhost')
   self.mqConn.connect(self._onConnect)
示例#10
0
 def makeAMQPConnection(self, token='', callback=None):
     def notifsCallback(msg):
         if callback:
             callback(msg)
             
     def on_connect():
         ch = self.conn.channel()
         ch.queue_declare(queue=token, durable=True, exclusive=False, auto_delete=False)
         ch.consume(token, notifsCallback, no_ack=True)
         ch.qos(prefetch_count=1)
     
     self.conn = Connection(host=self.config['broker_host'],port=self.config['broker_amqp_port'])
     self.conn.connect(on_connect)
class FibonacciRpcClient(object):
    def __init__(self, n):
        self.conn = Connection(host='localhost')
        self.conn.connect(self.on_connect)
        self.n = n
    
    def on_connect(self):
        self.ch = self.conn.channel()
        self.ch.queue_declare(exclusive=True, callback=self.on_queue_declare)
    
    def on_queue_declare(self, q_info):
        callback_queue = q_info.queue
        self.ch.consume(callback_queue, self.on_response)
        self.corr_id = str(uuid.uuid4())
        msg = Message(str(self.n), delivery_mode=2, reply_to=callback_queue,
                      correlation_id=self.corr_id)
        self.ch.publish(msg, exchange='', routing_key='rpc_queue')
    
    def on_response(self, msg):
        if self.corr_id == msg.correlation_id:
            print " [x] Received %r" % msg.body
            self.conn.close(callback=IOLoop.instance().stop)
            print 'Closing connection.'
示例#12
0
class RabbitHelper(object):

    def __init__(self, on_sent=None):
        self.conn = Connection(host='localhost')
        self.on_sent = on_sent

    def send_message(self, message, queue='', exchange='', routing_key=''):
        self.msg = Message(message, delivery_mode=2)
        self.queue = queue
        self.exchange = exchange
        self.routing_key = routing_key
        self.conn.connect(lambda: self.on_connect())

    def on_connect(self):
        ch = self.conn.channel()
        ch.queue_declare(queue=self.queue, durable=True)
        ch.publish(self.msg, exchange=self.exchange,
                    routing_key=self.routing_key)
        self.conn.close(callback=self.done)

    def done(self):
        if self.on_sent:
            self.on_sent()
示例#13
0
from tornado.ioloop import IOLoop
from stormed import Connection, Message
import simplejson as json

def on_connect():
    ch = conn.channel()
    ch.queue_declare(queue='reply')
    ch.consume('reply', callback, no_ack=True)

def callback(msg):
	print " [x] Received %r" % msg.body
	runmsg(msg)
	
def runmsg(msg):
	print "Exec", msg.body
	strMsg = json.loads(msg.body)
	print "Head:",strMsg[0]
	print "Body:",strMsg[1]
	#exec msg
	
logging.basicConfig()
conn = Connection(host='localhost')
conn.connect(on_connect)
io_loop = IOLoop.instance()
print ' [*] Waiting for messages. To exit press CTRL+C'

try:
    io_loop.start()
except KeyboardInterrupt:
    conn.close(io_loop.stop)
示例#14
0
from stormed import Connection, Message

msg = Message('Hello World!')

def on_connect():
	ch = conn.channel()
	ch.queue_declare(queue='hello')	
	SendHello(ch)
	SendNo(ch)	
	conn.close(callback=done)

def SendHello(ch):
	ch.publish(msg, exchange='', routing_key='hello')
	print "Send Hello Finish"
	
def SendNo(ch):
	for i in range(1,10):
		msgno = Message('Hello %(ID)02d'%{"ID":i})
		ch.publish(msgno, exchange='', routing_key='hello')
		print "Send No ",i,"Finish"
		
def done():
	print " End."
	io_loop.stop()

logging.basicConfig()
conn = Connection(host='localhost')
conn.connect(on_connect)
io_loop = IOLoop.instance()
io_loop.start()
示例#15
0
class MQView:
  """
  A view for the game that lets it communicate with the comet clients, via RabbitMQ.
  Pretends to be a normal blocking interface for this purpose, acts completely
  synchronous.
  """
  
  def __init__(self, playerId, position, otherPlayers, onQuit = None):
    # Acking for messages. You cannot send a message until the previous 
    # message has been ack'd (in a message, not just ack'd to the MQ). This
    # guarantees message order, which RabbitMQ does not otherwise do.
    self.waitlet = None
    self.waitingForAck = False
    self.blockingForAck = False
    self.messages = []
    self.playerId = playerId
    self.inQueue = "from-%s" % playerId
    self.outQueue = "to-%s" % playerId    
    
    # Number of messages we've recieved. Used to track whether the client has
    # stopped talking to us.
    self.msgCount = 0
    
    # Callback -- If we lose the player, call this with our position.
    self.onQuit = onQuit
    
    # If we've seen our hand we can't bid double nil. We (MQView) have to guarantee 
    # this, since we get given the hand before 00 bidding time. 
    self.seenHand = False
    
    self.position = position    
    self.otherPlayers = dict((self._getRelPosition(position), name) for (position, name) in otherPlayers)
    
    # Connect to the MQ.
    self.mq = None
    self.mqConn = Connection(host='localhost')
    self.mqConn.connect(self._onConnect)
  
  # Every single position that goes out from here should be relative to
  # the player we represent.
  def _getRelPosition(self, position):
    return (position - self.position) % 4
    
  def playerJoined(self, name, position):
    self.otherPlayers[self._getRelPosition(position)] = name
    self._send({'type' : 'joined', 
               'name' : name, 
               'position' : self._getRelPosition(position)}, async=True)
               
  def playerQuit(self, position):
    self.otherPlayers[self._getRelPosition(position)] = None  
    self._send({'type' : 'quit',
                'position' : self._getRelPosition(position)}, async=True)
               
  def startRound(self, leadPosition):
    self._send({'type' : 'startGame', 
                'leadPosition' : self._getRelPosition(leadPosition),
                'playerList' : map(lambda (position, name) : {
                         'name' : name, 
                         'position' : position
                }, self.otherPlayers.items())})  
    
  def setHand(self, hand):
    self.hand = hand
    self.seenHand = False

  def goDoubleNil(self):
    if self.seenHand:
      return False
      
    else:
      self._send({'type' : 'question', 'question' : 'bid00?'})
      result = self._recv()
      result = result['00']
      if result is True and self.seenHand:
        print "* Player attempted to bid 00 after having looked at their hand!"
        return False
        
      return result
      
  def showHand(self):
    if not self.seenHand:
      self.sendHand()
    
  def bidSomethingSensible(self):
    self._send({'type' : 'question', 'question' : 'bid?'})
    return self._recv()['bid']
    
  def sendHand(self, async=False):
    msg = {'type' : 'hand', 'hand' : self.hand}
  
    self._send(msg, async)
    self.seenHand = True
示例#16
0
文件: comet.py 项目: pimterry/50plus2
class CometHandler(tornado.web.RequestHandler):
  """
  Handles communication between MQView and the Javascript side, acting as the server end of the
  long-polling AJAX connection.
  """
  
  @tornado.web.asynchronous
  def post(self):
    self._handler(self.get_argument('gameId'), 
                  self.get_argument('userId'),
                  self.get_argument('alreadyJoined', False),
                  json.loads(self.get_argument('messages', "[]")))

  def _handler(self, gameId, userId, alreadyJoined = False, messagesToSend=[]):
    self.cancelled = False
    self.gameId = gameId
    self.userId = userId
    self.alreadyJoined = alreadyJoined
    self.messagesToSend = messagesToSend
    
    self._pingTimeout = None
    
    self.mqConn = Connection(host='localhost')
    self.mqConn.connect(self._onConnect)
    
    
  def _onConnect(self):
    self.mq = self.mqConn.channel()  
  
    if not self.alreadyJoined:
      joinMessage = Message(pickle.dumps(
        { 
          'type' : 'join',
          'id' : self.userId,
          'name' : 'Tim'
        }
      ))
      self.mq.queue_declare(self.gameId)
      self.mq.publish(joinMessage, exchange = '', routing_key = self.gameId)
    
    self.outQueue = "from-%s" % self.userId
    self.inQueue = "to-%s" % self.userId    
    self.mq.queue_declare(self.inQueue)
    self.mq.queue_declare(self.outQueue)    
    
    self.mq.consume(self.inQueue, self._onRecv)
      
    # We need the MQView to know that we're connected, either by us pinging it
    # or by us sending actual relevant messages:
    if len(self.messagesToSend) > 0:    
      for message in self.messagesToSend:
        self._send(message)
        
      # If we're still here in 14 seconds, make sure the MQView knows that we 
      # haven't disconnected.
      self._pingTimeout = tornado.ioloop.IOLoop.instance().add_timeout(time.time() + 14, 
                                                                       self._ping)
    
    # If we don't have any messages to send, just starting pinging instead (sooner, since
    # we don't know when the last message was)
    else:
      self._pingTimeout = tornado.ioloop.IOLoop.instance().add_timeout(time.time() + 5, 
                                                                       self._ping)
    
  def _send(self, msg):      
    # These messages are not going to be expected, and the MQView needs
    # do deal with them now.
    if 'type' in msg and (msg['type'] == 'quit' or msg['type'] == 'hand'):
      msg['async'] = True
  
    if 'bid' in msg:
      msg['bid'] = Bid(msg['bid'])
      
    if 'card' in msg:
      msg['card'] = Card(msg['card']['suit'], msg['card']['value'])
      
    self.mq.publish(Message(pickle.dumps(msg)), exchange = '', routing_key=self.outQueue)
    
  def _ping(self):
    self._pingTimeout = None
    
    if not self.request.connection.stream.closed() and not self.cancelled:
      self._send({'type' : 'ping'})
      self._pingTimeout = tornado.ioloop.IOLoop.instance().add_timeout(time.time() + 14, 
                                                                       self._ping)   
    
  def on_connection_close(self):
    self.stopConsuming()
    
  def _onRecv(self, msg):  
    if self.cancelled:
      # Don't print this, it happens quite a lot, not an issue unless we start getting
      # messages out of order again (should be fixed by our 'ack' messages.
      #   print "* Cancelled comet got message! (%s)" % self.userId
      msg.reject()
      return
           
    data = pickle.loads(msg.body)
    ackRequired = True
    
    if 'async' in data:
      if data['async'] == True:
        ackRequired = False
      del data['async']            
    
    # Turns hands (lists of Card objects) into dictionaries of the cards values
    # Suits are in alphabetical order (0 clubs, 1 diamonds etc), values in 
    # standard order, aces high, all 0 indexed.
    if 'hand' in data:
      data['hand'] = map(lambda c : {'suit' : c.suit, 
                                     'value' : c.value}, data['hand'])
      
    # Turn bids (lists of Bid objects) into lists of numbers, with
    # 0 as nil and -1 as double nil.
    elif 'bid' in data:
      bid = str(data['bid'].value)
      data['bid'] = bid if bid != '-1' else '00'
      
    # Turn individual cards into key'd maps.
    elif 'card' in data:
      data['card'] = {'suit' : data['card'].suit, 
                      'value' : data['card'].value }
      
    # If the connection got dropped, we leave this, and we give it back to the MQ, so we 
    # can resend it out next time.
    if self.request.connection.stream.closed():
      print "* Comet was closed whilst passing on message"
      msg.reject() # Marks the message as not sent.
      return      
    
    self.write(json.dumps(data))
    self.finish()
    
    msg.ack()
    if ackRequired:
      self._send({'ack' : True})
    self.stopConsuming()    

  def stopConsuming(self):
    self.cancelled = True
    self.mqConn.close()
    if self._pingTimeout:
      tornado.ioloop.IOLoop.instance().remove_timeout(self._pingTimeout)
 def __init__(self, n):
     self.conn = Connection(host='localhost')
     self.conn.connect(self.on_connect)
     self.n = n
示例#18
0
def main():
    global ch, conn
    conn = Connection(host='localhost')
    conn.connect(on_amqp_connection)

    ioloop.IOLoop.instance().start()
示例#19
0
def main():
    global ch, conn
    conn = Connection(host='localhost')
    conn.connect(on_amqp_connection)

    ioloop.IOLoop.instance().start()
示例#20
0
class SSEServer(object):
    def __init__(self, config):
        self.config = config
        self.conn = None
        
        self.webapp = self.makeWebFrontend()
        self.webapp.listen(8888)
        
        io_loop = IOLoop.instance()
        print ' [*] Waiting for messages. To exit press CTRL+C'
        try:
            io_loop.start()
        except KeyboardInterrupt:
            self.conn.close(io_loop.stop)

    def makeAMQPConnection(self, token='', callback=None):
        def notifsCallback(msg):
            if callback:
                callback(msg)
                
        def on_connect():
            ch = self.conn.channel()
            ch.queue_declare(queue=token, durable=True, exclusive=False, auto_delete=False)
            ch.consume(token, notifsCallback, no_ack=True)
            ch.qos(prefetch_count=1)
        
        self.conn = Connection(host=self.config['broker_host'],port=self.config['broker_amqp_port'])
        self.conn.connect(on_connect)
        
    def makeWebFrontend(self):
        
        thisSSE = self
        
        class NotificationsWebHandler(tornado.web.RequestHandler):
            @tornado.web.asynchronous
            def get(self, token):   
                self.set_header("Content-Type","text/event-stream")        
                self.set_header("Cache-Control","no-cache")        
                self.set_header("Connection","keep-alive")

                self.notifID = 0
                
                self.flush()
                
                def callbackFunc(msg):
                    self.publishNotification(msg.body)

                thisSSE.makeAMQPConnection(token, callbackFunc)
            
            @tornado.web.asynchronous
            def publishNotification(self, notifData):
                self.write("id: %i\n" % self.notifID)
                self.write("data:")
                self.write(notifData)
                self.write("\n\n")
                self.flush()

                self.notifID+=1
            
        routes = [
            (r"/feed/(.*)", NotificationsWebHandler)
        ]
        
        return tornado.web.Application(routes)
示例#21
0
from tornado.options import define, options
from stormed import Connection, Message
import settings
import logging
from tornado.ioloop import IOLoop

conn = Connection(host=settings.MQ_HOST,
                  username=settings.MQ_USERNAME,
                  password=settings.MQ_PASSWORD,
                  heartbeat=settings.MQ_HEARTBEAT)
msg = Message('Hello World!')


def callback(msg):
    logging.info(" test=> Received %r" % msg.body)


def on_connect():
    ch = conn.channel()
    ch.queue_declare(queue='hello')
    ch.publish(msg, exchange='', routing_key='hello')
    logging.info(" test=> Sent 'Hello World!'")
    ch.consume('hello', callback, no_ack=True)

    logging.info("Init amqp success")
    options.mq_connection = conn
    init_queue()


def init_queue():
    ch = conn.channel()
示例#22
0
#! /usr/bin/env python

import logging
from tornado.ioloop import IOLoop
from stormed import Connection, Message


def on_connect():
    ch = conn.channel()
    ch.queue_declare(queue='hello')
    ch.consume('hello', callback, no_ack=True)


def callback(msg):
    print " [x] Received %r" % msg.body


logging.basicConfig()
conn = Connection(host='localhost')
conn.connect(on_connect)
io_loop = IOLoop.instance()
print ' [*] Waiting for messages. To exit press CTRL+C'
try:
    io_loop.start()
except KeyboardInterrupt:
    conn.close(io_loop.stop)
示例#23
0
#! /usr/bin/env python

import logging
import sys
from tornado.ioloop import IOLoop
from stormed import Connection, Message

# delivery_mode=2 makes message persistent
routing_key = sys.argv[1] if len(sys.argv) > 1 else 'anonymous.info'
msg = Message(' '.join(sys.argv[2:]) or 'Hello World!')


def on_connect():
    ch = conn.channel()
    ch.exchange_declare(exchange='topic_logs', type='topic')
    ch.publish(msg, exchange='topic_logs', routing_key=routing_key)
    conn.close(callback=done)


def done():
    print " [x] Sent %r:%r" % (routing_key, msg.body)
    io_loop.stop()


logging.basicConfig()
conn = Connection(host='localhost')
conn.connect(on_connect)
io_loop = IOLoop.instance()
io_loop.start()
示例#24
0
 def __init__(self, on_sent=None):
     self.conn = Connection(host='localhost')
     self.on_sent = on_sent