Example #1
0
class Librarian:
    def __init__(self, file=None, host=None):
        """
Make a database writing librarian, listening to packets from host, and writing into the the sqlite3 file specified.
Defaults provided match my own personal environment.
If the database file is not found, or does not contain the appropriate tables and indexes, it is created
        """
        self.log = logging.getLogger("alexandria.Librarian")
        if file is None:
            file = config['library_file']
        try:
            self.conn = sqlite3.connect(file)
        except sqlite3.OperationalError as ex:
            self.log.error(ex)
            raise ex

        self.log.debug("library exists, validating schema")
        self.cur = self.conn.cursor()
        try:
            self.cur.execute('select * from karlnet_sensor')
        except sqlite3.OperationalError as ex:
            if string.find(str(ex), "no such table: karlnet_sensor") != -1:
                self.log.debug(
                    "karlnet_sensor table didn't exist, creating it!")
                self.cur.execute(config['create_sql'])
                self.cur.execute(config['create_index'])
        self.log.debug("library is valid, connecting to stomp")

        if host is None:
            host = config['stomp_host']
        self.stomp = Client(host=host)
        self.log.info(
            "Valid library and stomp is up, starting to write books!")

    def save_books(self):
        """
Loop forever, saving readings into the database.  TODO: topic could be a config option?
        """
        clientid = "karlnet_alexandria@%s/%d" % (socket.gethostname(),
                                                 os.getpid())
        self.stomp.connect(clientid=clientid)
        self.stomp.subscribe("/topic/karlnet.>")

        while True:
            message = self.stomp.get()
            kp = jsonpickle.decode(message.body)
            self.log.info("saving into the library for: %s", kp)

            for i in range(len(kp.sensors)):
                sensor = kp.sensors[i]
                if sensor.type == ord('z'):
                    self.log.debug(
                        "Skipping archiving of sensor test value: %s", sensor)
                    continue
                self.log.debug("saving to db for sensor %d: %s", i, sensor)
                self.cur.execute(
                    'insert into karlnet_sensor (sampleTime, node, sensorType, channel, sensorRaw, sensorValue) values (?,?,?,?,?,?)',
                    (kp.time_received, kp.node, sensor.type, i,
                     sensor.rawValue, sensor.value))
                self.conn.commit()
Example #2
0
class StompThread(QThread):
    def __init__(self, host, port, channel, nconcepts, k=2):
        QThread.__init__(self)
        self.stomp = Client(host, port)
        self.channel = channel

        self.array = np.zeros((nconcepts, k))
        self.k = k
        self.nconcepts = nconcepts
        self.labels = RecyclingSet(nconcepts)
        self.labels.listen_for_drops(self.on_drop)

        self.exiting = False
    
    def on_message(self, body):
        message = json.loads(body)
        x = message['x']
        y = message['y']
        print x,y
        idx = self.labels.add(message['text'])
        self.array[idx,:] = [x,y]
        self.emit(SIGNAL("newData()"))

    def on_drop(self, idx):
        self.array[idx,:] = 0
        self.emit(SIGNAL("newData()"))

    def run(self):
        self.stomp.connect(username=None, password=None)
        self.stomp.subscribe(destination=self.channel, ack='auto')

        while not self.exiting:
            msg = self.stomp.get()
            self.on_message(msg.body)
Example #3
0
def main():
    TOPIC_1 = '/topic/julian_1'
    TOPIC_2 = '/topic/remote_queue'

    SERVER = 'pcbunn.cacr.caltech.edu'
    PORT = 61613
    
    try:
        # Testing remote queue
        q = RemoteQueue(host=SERVER, port=PORT, destination=TOPIC_2)
        stomp_1 = Client(host=SERVER, port=PORT)
        stomp_1.connect()
        stomp_1.subscribe(destination=TOPIC_1)
        
        for i in range(4):
            stomp_1.put(i, destination=TOPIC_1)
            q.put(i+40)

        for j in range(4):
            message_1 = stomp_1.get(block=True)
            message_2 = q.get()
            print 'message_1 is ', message_1.body
            print 'message_2 is ', message_2

            #print 'ack'
            #stomp.ack(message)
            time.sleep(1.0)

    except Exception, err:
        print 'Error', err
        return
Example #4
0
class StompClient(object):
    def __init__(self, host='localhost', port=61613):
        self.stomp = Client(host=host, port=port)
        self.stomp.connect()

    def subscribe(self, queue):
        self.stomp.subscribe(queue, ack='auto')

    def get(self):
        return self.stomp.get()
Example #5
0
class Librarian:

    def __init__(self, file=None, host=None):
        """
Make a database writing librarian, listening to packets from host, and writing into the the sqlite3 file specified.
Defaults provided match my own personal environment.
If the database file is not found, or does not contain the appropriate tables and indexes, it is created
        """
        self.log = logging.getLogger("alexandria.Librarian")
        if file is None:
            file = config['library_file']
        try:
            self.conn = sqlite3.connect(file)
        except sqlite3.OperationalError as ex:
            self.log.error(ex)
            raise ex

        self.log.debug("library exists, validating schema")
        self.cur = self.conn.cursor()
        try:
            self.cur.execute('select * from karlnet_sensor')
        except sqlite3.OperationalError as ex:
            if string.find(str(ex), "no such table: karlnet_sensor") != -1:
                self.log.debug("karlnet_sensor table didn't exist, creating it!")
                self.cur.execute(config['create_sql'])
                self.cur.execute(config['create_index'])
        self.log.debug("library is valid, connecting to stomp")

        if host is None:
            host = config['stomp_host']
        self.stomp = Client(host=host)
        self.log.info("Valid library and stomp is up, starting to write books!")

    def save_books(self):
        """
Loop forever, saving readings into the database.  TODO: topic could be a config option?
        """
        clientid = "karlnet_alexandria@%s/%d" % (socket.gethostname(), os.getpid())
        self.stomp.connect(clientid=clientid)
        self.stomp.subscribe("/topic/karlnet.>")
    
        while True:
            message = self.stomp.get()
            kp = jsonpickle.decode(message.body)
            self.log.info("saving into the library for: %s", kp)

            for i in range(len(kp.sensors)):
                sensor = kp.sensors[i]
                if sensor.type == ord('z'):
                    self.log.debug("Skipping archiving of sensor test value: %s", sensor)
                    continue
                self.log.debug("saving to db for sensor %d: %s", i, sensor)
                self.cur.execute('insert into karlnet_sensor (sampleTime, node, sensorType, channel, sensorRaw, sensorValue) values (?,?,?,?,?,?)', 
                    (kp.time_received, kp.node, sensor.type, i, sensor.rawValue, sensor.value))
                self.conn.commit()
def main():
    stomp = Client('10.200.10.194', 61624)
    stomp.connect()
    stomp.subscribe('/queue/test1', ack='client')
    while True:
        message = stomp.get(block=True)
        print(type(message), message.body)
        stomp.ack(message)
    connection.unsubscribe({'destination': '/queue/test1'})
    time.sleep(1)
    connection.disconnect()
Example #7
0
def simple_receive():
    stomp = Client("192.168.1.166", 61613)
    stomp.connect()
    stomp.subscribe("/queue/hello", ack="client")

    while True:
        message = stomp.get()
        print message.body
        stomp.ack(message)

    stomp.unsubscribe("/queue/hello")
    stomp.disconnect()
Example #8
0
def simple_receive():
    stomp = Client('192.168.1.166', 61613)
    stomp.connect()
    stomp.subscribe("/queue/hello", ack="client")

    while True:
        message = stomp.get()
        print message.body
        stomp.ack(message)

    stomp.unsubscribe("/queue/hello")
    stomp.disconnect()
Example #9
0
class StompThread(QThread):
    def __init__(self, host, port, channel, nconcepts, k=19):
        QThread.__init__(self)
        self.stomp = Client(host, port)
        self.channel = channel

        self.array = np.zeros((nconcepts, k))
        self.k = k
        self.nconcepts = nconcepts
        self.labels = RecyclingSet(nconcepts)
        self.labels.listen_for_drops(self.on_drop)

        self.exiting = False
    
    def on_message(self, body):
        message = json.loads(body)
        print message
        if 'text' not in message: return
        if message['text'].startswith('('): return
        vec = unpack64(message['coordinates'])
        self.handle_vector(vec[1:], message['text'])
        for concept, value in message['concepts'].items():
            vec = unpack64(value)
            self.handle_vector(vec[1:], concept)

    def on_drop(self, index, label):
        self.array[index,:] = 0
        self.emit(SIGNAL("newData()"))
    
    def handle_vector(self, vec, text):
        if len(text) < 20:
            idx = self.labels.add(text)
            norm = max(0.0000001, np.linalg.norm(vec))
            self.array[idx,:] = vec/norm
            self.emit(SIGNAL("newData()"))

    def run(self):
        self.stomp.connect(username=None, password=None)
        self.stomp.subscribe(destination=self.channel, ack='auto')

        while not self.exiting:
            msg = self.stomp.get()
            self.on_message(msg.body)
Example #10
0
class RemoteQueue(object):
    def __init__(self, host, port, destination):
        self.host = host
        self.port = port
        self.destination = destination
        self.stomp = Client(host, port)
        self.stomp.connect()
        self.stomp.subscribe(destination)

    def put(self, message):
        message = json.dumps(message)
        self.stomp.put(message, self.destination)

    def get(self):
        mq_message = self.stomp.get(block=True)
        message = mq_message.body
        message = json.loads(message)
        return message

    def disconnect(self):
        self.stomp.unsubscribe(self.destination)
        self.stomp.disconnect()
Example #11
0
def simple():
    # 通过simple方式连接JMS服务器
    # 指定hostname和port(tips:ActiveMQ支持多种协议连接stomp协议的默认端口为61613,这里不要写为61616)
    stomp = Client("192.168.1.166", 61613)
    # stomp = Client()#如果是ActiveMQ和ActiveMQ客户端(本程序)在同一台机器可使用默认值:hostname="localhost",port=61613

    # 连接服务器
    stomp.connect()
    # 发送消息到指定的queue
    stomp.put("The quick brown fox...", destination="/queue/hello")
    # 从指定的queue订阅消息。ack参数指定为"client",不然可能出现一个问题(具体忘了,以后补充),ack默认值为"auto"
    stomp.subscribe("/queue/hello", ack="client")
    # 等待接收ActiveMQ推送的消息
    message = stomp.get()
    # 打印消息的主体
    print message.body
    message.body
    "quick brown fox..."
    stomp.ack(message)
    # 退订
    stomp.unsubscribe("/queue/hello")
    # 关闭连接
    stomp.disconnect()
Example #12
0
def simple():
    # 通过simple方式连接JMS服务器
    # 指定hostname和port(tips:ActiveMQ支持多种协议连接stomp协议的默认端口为61613,这里不要写为61616)
    stomp = Client('192.168.1.166', 61613)
    #stomp = Client()#如果是ActiveMQ和ActiveMQ客户端(本程序)在同一台机器可使用默认值:hostname="localhost",port=61613

    # 连接服务器
    stomp.connect()
    # 发送消息到指定的queue
    stomp.put("The quick brown fox...", destination="/queue/hello")
    # 从指定的queue订阅消息。ack参数指定为"client",不然可能出现一个问题(具体忘了,以后补充),ack默认值为"auto"
    stomp.subscribe("/queue/hello", ack="client")
    # 等待接收ActiveMQ推送的消息
    message = stomp.get()
    # 打印消息的主体
    print message.body
    message.body
    'quick brown fox...'
    stomp.ack(message)
    # 退订
    stomp.unsubscribe("/queue/hello")
    # 关闭连接
    stomp.disconnect()
Example #13
0
class JSONupdate(object):

    """Interact with JSON and STOMP servers

    This class provides functions that interact with the JSON and STOMP
    servers.

    clone_db(): This method clones multiple collections within a database.
    The set of collections is passed as a dictionary with keys being the
    collection name, and values being the max id within that collection name.
    For example, {'alerts': 40} will clone the alerts database up to alert_id
    40.  The rate limit paramater gives a time in seconds between calls to the
    json api.

    clone_collection(): This method clones a single collection within a
    database.  It uses the same paramaters as clone_db, except the input is
    not a dictionary.

    poll(): This function will put the execution in a blocking state until a
    new message is passed via the stomp server.  When the new message is
    received, the callback function is executed.

    json_request(): This function will make a json request to the main
    database server.  The data of this json request is returned as a dict.

    callback(): This function is executed after an incoming message from
    the stomp server is received.  If the message is not of type 'view', then
    it will perform a json_request for the updated data.

    """

    def __init__(self, scot_uri, scot_database='scot3'):
        """Create an instance of the JSONupdate class

        arguments:
            stomp_uri: the base uri to the scot stomp server.
            stomp_port: the port used for communicating with the scot stomp
                server.  Defaults to 61613
            scot_database: the scot database to write new alert data to.
                Defaults to scot_v3

        """
        self._scot_uri = scot_uri
        self._stomp_uri = stomp_uri
        self._stomp_port = stomp_port
        self._db = database.Database(database=scot_database,
                                     collection='alerts')

        self._stomp_client = Client(host=self._stomp_uri, port=self._stomp_port)
        self._stomp_client.connect()
        self._stomp_client.subscribe('/topic/CHAT.DEMO',
                                     conf={'message':'chat', 'type':'listen'})

    def clone_db(self, collection_list, rate_limit=3):
        """clone multiple collections from the main scot server via JSON

        arguments:
            collection_list: A dictionary of each collection to clone.  The
                keys are collection names, and the keys are the max id within
                that collection.
            rate_limit: Time in seconds to wait between each connection
                attempt to the JSON server.  Defaults to 3.

        """
        for collection in collection_list:
            self.clone_collection(collection, collection_list[collection],
                                  rate_limit)

    def cdb(self, entity_type, s1, s2, rate_limit=3):
        """clone multiple collections from the main scot server via JSON

        arguments:
            collection_list: A dictionary of each collection to clone.  The
                keys are collection names, and the keys are the max id within
                that collection.
            rate_limit: Time in seconds to wait between each connection
                attempt to the JSON server.  Defaults to 3.

        """
        for id in xrange(s1, s2):
            data = self.json_request(entity_type, id)
            self._db.write(data, collection=entity_type)

            if _DEBUG:
                print "added id:", str(id), "entity_type:", entity_type,
                time.sleep(rate_limit)


    def clone_collection(self, entity_type, max_id, rate_limit=3):
        """clone a single collection from the main scot server via JSON

        arguments:
            entity_type: the name of the collection to clone
            max_id: The value of the last id to clone
            rate_limit: Time in seconds to wait between each connection
                attempt to the JSON server.  Defaults to 3.

        """
        for id in range(1, max_id):
            data = self.json_request(entity_type, id)
            self._db.write(data, collection=entity_type)
            if _DEBUG:
                print "id:", str(id), "entity_type:", entity_type
            time.sleep(rate_limit)


    def cc(self, entity_type, max_id, rate_limit=3):
        """clone a single collection from the main scot server via JSON

        arguments:
            entity_type: the name of the collection to clone
            max_id: The value of the last id to clone
            rate_limit: Time in seconds to wait between each connection
                attempt to the JSON server.  Defaults to 3.

        """
        for id in range(1, max_id):
            data = self.json_request(entity_type, id)
            print data
            if _DEBUG:
                print "entity_type:", entity_type
                print "id:", str(id)
            time.sleep(rate_limit)

    def poll(self):
        """Block and wait until a message is recieved from the STOMP server"""
        self._stomp_client.get(block=True, callback=self.callback)

    def json_request(self, entity_type, id):
        """Retrieve data from the JSON server

        arguments:
            entity_type: The name of the collection to get data from
            id: The id value of the data to get

        """
        print "json_request"
        url = self._scot_uri + str(entity_type) + '/' + str(id)

    def callback(self, frame):
        """Request JSON data after a STOMP message has been received

        arguments:
            frame: A STOMPY frame that contains thet data sent over the
                STOMP channel

        """
        print "callback"
        body = frame.body

        print frame
        #print type(body)
        print "----"


        #Conversion into python data structure
        body = body.replace('null', 'None')
        #change from str to dict
        body = ast.literal_eval(body)


        print "action:", body['action']
        print "type:", body['type']
        if body['action'] != 'view': #ignore views as nothing has changed.
            type = body['type']
            id = body['id']
            if _DEBUG:
                print 'STOMP Body {1}'.format(body)
                print '{1} number {2}'.format(type, id)
            self._db.write(self.json_request(type, id))
Example #14
0
class WhenUsingSimpleClient(DingusTestCase(Client,
    exclude=['TransactionError', 'Empty'])):

    def setup(self):
        super(WhenUsingSimpleClient, self).setup()
        self.client = Client()

    def should_connect(self):
        self.client.connect()
        assert self.client.stomp.calls('connect')

    def should_disconnect(self):
        self.client.disconnect()
        assert self.client.stomp.calls('disconnect')

    def should_subscribe(self):
        self.client.subscribe('/queue/nose_test')
        print self.client.stomp.calls
        assert self.client.stomp.calls('subscribe',
                {'ack': 'auto', 'destination': '/queue/nose_test'})

    def should_unsubscribe(self):
        self.client.unsubscribe('/queue/nose_test')
        assert self.client.stomp.calls('unsubscribe',
                {'destination': '/queue/nose_test'})

    def should_begin_transaction(self):
        self.client.begin('bah')
        assert self.client.stomp.calls('begin',
            {"transaction": self.client._current_transaction})

    def should_fail_to_begin_already_in_transaction(self):
        self.client._current_transaction = "meh"
        nose_tools.assert_raises(TransactionError, self.client.begin, 'bah')

    def should_commit_transaction(self):
        self.client._current_transaction = 'meh'
        self.client.commit('bah')
        assert self.client.stomp.calls('commit', {'transaction': 'meh'})

    def should_fail_to_commit_transaction(self):
        nose_tools.assert_raises(TransactionError, self.client.commit, 'bah')

    def should_abort_transaction(self):
        self.client._current_transaction = 'meh'
        self.client.abort()
        assert self.client.stomp.calls('abort', {'transaction': 'meh'})

    def should_fail_to_abort_transaction(self):
        nose_tools.assert_raises(TransactionError, self.client.abort)

    def should_ack_message(self):
        self.client.ack("fake_frame")
        assert self.client.stomp.calls('ack', "fake_frame")

    def should_make_conf(self):
        conf = self.client._make_conf(None,
            destination='/queue/nose_test', ack='auto')
        assert isinstance(conf, type({}))

    def should_make_conf_with_transaction(self):
        self.client._current_transaction = 'meh'
        conf = self.client._make_conf({},
            destination='/queue/nose_test', ack='auto')
        assert isinstance(conf, type({}))

    def should_put_item_into_queue(self):
        self.client.put('bah', '/queue/nose_test')
        conf = self.client._make_conf(None, body='bah',
            destination='/queue/nose_test',
            persistent='true')

        assert self.client.stomp.calls('send', conf)

    def should_get_message(self):
        self.client.get()
        assert self.client.stomp.calls('receive_frame', nonblocking=False, callback=None)

    def should_get_message_without_blocking(self):
        self.client.get_nowait()
        assert self.client.stomp.calls('receive_frame', nonblocking=True, callback=None)

    def should_not_get_message(self):
        self.client.stomp.receive_frame.return_value = None
        nose_tools.assert_raises(self.client.Empty,
            self.client.get, block=False)
# How many messages are we expecting to come in?
expected = int(sys.argv[1])

count = 0

start = None
end = None

stomp = Client()
stomp.connect()

stomp.subscribe(queue_name, "client")

while (count < expected):
	try: 
		frame = stomp.get()
		if start is None:
			start = timer()
		count = count + 1
		stomp.ack(frame)
	except:
		print("got nothing :(")
		time.sleep(1)

end = timer()

result = (end - start)

print(str(result))

stomp.unsubscribe(queue_name)
Example #16
0
#!/usr/bin/python

from stompy.simple import Client
import json

Dict_Message = dict()
Dict_Message["Test1"] = "CONDOR"

stomp = Client("localhost", 61613)
stomp.connect("producer", "pass")
stomp.put(json.dumps(Dict_Message),
          destination="/queue/test",
          conf={'Test': 'Test123'})
stomp.disconnect()

stomp = Client("localhost", 61613)
stomp.connect("consumer", "pass")
stomp.subscribe("/queue/test", conf={'selector': "Test = 'Test123'"})
#stomp.subscribe("/queue/test")
message = stomp.get()

print message.headers
New_Dict = json.loads(message.body)
print New_Dict
stomp.ack(message)
stomp.unsubscribe("/queue/test")
stomp.disconnect()
Example #17
0
print 'Stomp username:'******'Stomp password:'******'Stomp queue:', stomp_queue
print '='*80


ami_host = config.get('AMI', 'host')
ami_username = config.get('AMI', 'username')
ami_password = config.get('AMI', 'password')

stomp = Client(stomp_host)
stomp.connect(stomp_username, stomp_password)
stomp.subscribe(stomp_queue)

while True:
    message = stomp.get()

    channel_message = ChannelMessage()

    logging.debug(message.body)

    data = channel_message.load_data_json(message.body)

    if data['type'] == channel_message.TYPE_AGENT_STATUS:
        manager = asterisk.manager.Manager()
        manager.connect(ami_host)
        manager.login(ami_username, ami_password)

        if data['statusId'] == 'available':
            response = queue_add(manager, data)
        elif data['statusId'] == 'offline':
Example #18
0
config.read('trains.conf')

topics = {
  'tm': 'TRAIN_MVT_ALL_TOC',
  'td': 'TD_ALL_SIG_AREA',
  'vstp': 'VSTP_ALL',
  'rtppm': 'RTPPM_ALL',
  'tsr': 'TSR_ALL_ROUTE',
}

feedname = sys.argv[1].lower()
if feedname not in topics:
  print 'Valid feeds:'
  print '\n'.join(topics.keys())

dest = '/topic/%s' % topics[feedname]
feed = Client(host='datafeeds.networkrail.co.uk', port=61618)

feed.connect(config.get('user', 'email'), config.get('user', 'password'))
feed.subscribe(dest)
print 'Subscribed'

while True:
  message = feed.get()
  print message
  data = json.loads(message.body)
  pprint.pprint(data)

feed.unsubscribe(dest)
feed.disconnect()