def PostToSCE(lines): logging.info ('Number of impressions: %s' % lines) stomp = Client(host='activemq.dashboard.myinternaldomain.com') stomp.connect() body="{'host' : '%s', 'lines' : %d}" %(socket.gethostname(),lines) stomp.put(body,"/queue/events",conf={'eventtype' : 'banner_line_count'}) stomp.disconnect()
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
def PostToSCE(livecampaigns): logging.info ('Number of live Campaigns: %s' % livecampaigns) stomp = Client(host='activemq.dashboard.internaldomain.com') stomp.connect() body="{'host' : '%s', 'campaigns' : %s}" %(socket.gethostname(),livecampaigns) stomp.put(body,"/queue/events",conf={'eventtype' : 'banner_live_camp'}) stomp.disconnect()
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()
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)
def PostToSCE(responsetime): logging.info ('The Response Timei was: %s' % responsetime) stomp = Client(host='activemq.dashboard.internaldomain.com') stomp.connect() body="{'host' : '%s', 'responsetime' : %s}" %(socket.gethostname(),responsetime) stomp.put(body,"/queue/events",conf={'eventtype' : 'banner_responsetime'}) stomp.disconnect()
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()
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()
def run_stomp(): #initialize #~ Does connection and loops the stomp connection stomp = Client(host=stomphost, port=int(stompport)) try: stomp.connect(username=stompusername, password=stomppw, clientid=stompclientid) except Exception, e: print str(e) sys.exit("Cannot connect to STOMP Server")
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()
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()
class AMQConnection: def __init__(self, host=None, port=None, topic=None, username=None, password=None, continuous=False): try: self.topic = topic self.username = username self.password = password self.host = host self.port = port self.stomp = Client(host=self.host, port=self.port) self.stomp.connect(username=self.username, password=self.password) except Exception, e: raise Exception('Cannot connect to message broker (%s@%s:%d): %s.'\ % (username, host, port, e)) self.continuous = continuous
class StompClient(QueueClient): def __init__(self, host='127.0.0.1', username=None, password=None, exchange=None): #TODO asserts super(StompClient, self).__init__() from stompy.simple import Client self.client = Client(host) self.client.connect(username, password) self.agent_channel = 'jms.queue.msg.' def put(self, message, destination='', persistent=False, conf={}): super(StompClient, self).put(message, destination, persistent, conf) self.client.put(message, destination=destination, persistent=persistent, conf=conf)
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)
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()
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()
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()
class SimpleRPCAction(object): def __init__(self, agent, action, config=None, stomp_client=None, autoconnect=True, **kwargs): self.agent = agent self.action = action self.config = config or Config() self.params = kwargs self.stomp_target = '%s.%s.command' % (self.config.topicprefix, agent) self.stomp_target_reply = '%s.%s.reply' % (self.config.topicprefix, agent) self.stomp_client = stomp_client self.signer = PROVIDERS.get(self.config.securityprovider) if self.signer: caller = basename(self.config.pluginconf['ssl_client_public']).split('.')[0] self.signer = self.signer( self.config.pluginconf['ssl_client_private'], caller, ) if autoconnect and not stomp_client: self.connect_stomp() def connect_stomp(self): self.stomp_client = Client( self.config.pluginconf['stomp.host'], int(self.config.pluginconf['stomp.port']), ) self.stomp_client.connect( self.config.pluginconf['stomp.user'], self.config.pluginconf['stomp.password'], ) def send(self, filter_=None, process_results=True, **kwargs): body = dict() body[':action'] = self.action body[':agent'] = self.agent body[':data'] = dict([(':%s' % k, v) for k, v in kwargs.items()]) body[':data'][':process_results'] = process_results m = Message(body, self.stomp_target, filter_=filter_) if self.signer: self.signer.sign(m) data = safe_dump(m.request, explicit_start=True, explicit_end=False) body = "\n".join([' %s' % line for line in m.body.split("\n")]) data = data + ":body: |\n" + body self.data = data if process_results: self.stomp_client.subscribe(self.stomp_target_reply) self.stomp_client.put(data, self.stomp_target) sleep(2) self.stomp_client.unsubscribe(self.stomp_target_reply) return self.collect_results(m.rid) self.stomp_client.put(data, self.stomp_target) def collect_results(self, request_id): '''Collect the results from a previous :func:`Message.send` call. :rtype: list of STOMP messages which match this object's `:requestid`''' results = [] while True: message = None try: message = self.stomp_client.get_nowait() except Exception, e: break decoded_message = load(message.body.replace('!ruby/sym ', ':')) if decoded_message[':requestid'] == request_id: results.append(decoded_message) return results
class SimpleRPCAction(object): def __init__(self, agent, action, config=None, stomp_client=None, autoconnect=True, **kwargs): self.agent = agent self.action = action self.config = config or Config() self.params = kwargs if self.config.connector == 'stomp': suffix = 'command' else: suffix = 'agent' self.stomp_target = "{0}.{1}".format(self.target, suffix) self.stomp_target_reply = '%s.reply' % self.target self.stomp_client = stomp_client self.signer = PROVIDERS.get(self.config.securityprovider) if self.signer: self.signer = self.signer(config) if autoconnect and not stomp_client: self.connect_stomp() @property def target(self): '''MColletive target, based on topic and collective''' collective = self.params.get('collective', self.config.main_collective) return "{topicprefix}{collective}.{agent}".format( topicprefix=self.config.topicprefix, collective=collective, agent=self.agent) def connect_stomp(self): '''Connect to stomp server''' if self.config.connector == 'stomp': key = 'stomp' elif self.config.connector == 'activemq': # FIXME(rafaduran): take advantage of multiple stomp servers key = 'activemq.pool.1' self.stomp_client = Client( self.config.pluginconf['{key}.host'.format(key=key)], int(self.config.pluginconf['{key}.port'.format(key=key)]), ) self.stomp_client.connect( self.config.pluginconf['{key}.user'.format(key=key)], self.config.pluginconf['{key}.password'.format(key=key)], ) def send(self, filter_=None, process_results=True, **kwargs): if (self.agent == 'discovery') and (self.action == 'ping'): body = 'ping' else: body = dict() body[':action'] = self.action body[':agent'] = self.agent body[':data'] = dict([(':%s' % k, v) for k, v in kwargs.items()]) body[':data'][':process_results'] = process_results collective = self.params.get('collective', self.config.main_collective) if self.signer: # body[':caller'] = self.signer.caller_id m = Message(body, self.stomp_target, filter_=filter_, agent=self.agent, identity=self.config.identity, collective=collective) self.signer.sign(m) else: m = Message(body, self.stomp_target, filter_=filter_, agent=self.agent, identity=self.config.identity, collective=collective) self.request = m.request data = safe_dump(m.request, explicit_start=True, explicit_end=False) body = "\n".join([' %s' % line for line in m.body.split("\n")]) data = data + ":body: " + body self.data = data if process_results: self.stomp_client.subscribe(self.stomp_target_reply) if self.config.connector == 'activemq': conf = {'reply-to': self.stomp_target_reply} else: conf = None self.stomp_client.put(data, self.stomp_target, conf=conf) sleep(2) self.stomp_client.unsubscribe(self.stomp_target_reply) return self.collect_results(m.rid) self.stomp_client.put(data, self.stomp_target) def collect_results(self, request_id): '''Collect the results from a previous :func:`Message.send` call. :rtype: list of STOMP messages which match this object's `:requestid` ''' results = [] while True: message = None try: message = self.stomp_client.get_nowait() except Exception, e: break decoded_message = load(message.body.replace('!ruby/sym ', ':')) if decoded_message[':requestid'] == request_id: results.append(decoded_message) return results
#import fcntl #lockfile = os.path.normpath('/var/lock/' + os.path.basename(__file__) + '.lock') #exclusive_lock = open(lockfile, 'w') #try: # fcntl.lockf(exclusive_lock, fcntl.LOCK_EX | fcntl.LOCK_NB) #except IOError: # print "Another instance is already running, quitting." # time.sleep(1) # sys.exit(-1) config = ConfigParser.ConfigParser() config.read('/opt/ucall/etc/config.ini') stomp = Client(config.get('STOMP', 'host')) stomp.connect(config.get('STOMP', 'username'), config.get('STOMP', 'password')) #sys.stdout = open("/var/log/requests/connector2.log","a") #sys.stderr = open("/var/log/requests/connector-err2.log","a") class AsteriskEvent(SQLObject): added=DateTimeCol(default=sqlbuilder.func.NOW()) event = StringCol() uniqueid = StringCol(default=None) raw = StringCol(default=None) connection = connectionForURI(config.get('SQL', 'dsn')) sqlhub.processConnection = connection def send_message(message, agent): #conf={"expires":(int(time()) + int(connect(config.get('GENERAL', 'message_ttl'))) * 1000}
print '='*80 print 'Stomp host:', stomp_host 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)
from stompy.simple import Client import sys import time time.sleep(2) queue_name = "/queue/test4" if (len(sys.argv) < 2): print("Usage: send.py total") exit() total = int(sys.argv[1]) count = 0 stomp = Client() stomp.connect() while (count < total): stomp.put("{\"message\": \"addUser\", \"userid\": \"" + str(count) + "\"}", destination=queue_name, persistent=False) count = count + 1; stomp.disconnect()
#!/usr/bin/python from mcollective import Message from yaml import load from M2Crypto.RSA import load_pub_key, load_key from stompy.simple import Client from sys import argv, exit, stderr if len(argv) < 7: print >>stderr, '%s <stomp server> <stomp username> <stomp password> <key filename> <key name> <fact>' % argv[0] exit(-1) certificate_name = argv[5] host, port = argv[1].split(':') stomp_client = Client(host, int(port)) stomp_client.connect(argv[2], argv[3]) private_key = load_key(argv[4]) body = {':caller': 'cert=%s' % certificate_name, ':data': {':process_results': True}, ':action': 'inventory', ':agent': 'rpcutil'} m = Message(body, stomp_client) m.sign(private_key, certificate_name) results = m.send_and_await_results(debug=True) print [load(q[':body'])[':data'][':facts'][argv[6]] for q in results]
#import fcntl #lockfile = os.path.normpath('/var/lock/' + os.path.basename(__file__) + '.lock') #exclusive_lock = open(lockfile, 'w') #try: # fcntl.lockf(exclusive_lock, fcntl.LOCK_EX | fcntl.LOCK_NB) #except IOError: # print "Another instance is already running, quitting." # time.sleep(1) # sys.exit(-1) config = ConfigParser.ConfigParser() config.read('/opt/ucall/etc/config.ini') stomp = Client(config.get('STOMP', 'host')) stomp.connect(config.get('STOMP', 'username'), config.get('STOMP', 'password')) #sys.stdout = open("/var/log/requests/connector2.log","a") #sys.stderr = open("/var/log/requests/connector-err2.log","a") class AsteriskEvent(SQLObject): added = DateTimeCol(default=sqlbuilder.func.NOW()) event = StringCol() uniqueid = StringCol(default=None) raw = StringCol(default=None) connection = connectionForURI(config.get('SQL', 'dsn')) sqlhub.processConnection = connection
#!/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()
#!/usr/bin/python import sys import syck as yaml import time from hashlib import sha1 from M2Crypto.RSA import load_pub_key, load_key from stompy.simple import Client # Change these. PREFIX = 'mcollective' CERTNAME = 'certificate-name' s = Client('stomp.server') s.connect('stompusername','stomppassword') rr = load_key('path-to-unencrypted-private-key.pem') target = '/topic/%s.discovery.command' % PREFIX target_reply = '/topic/%s.discovery.reply' % PREFIX s.subscribe(target_reply) rid = sha1(str(time.time())).hexdigest() # Put together message. r = {} r[':msgtime'] = int(time.time()) r[':filter'] = { 'identity': [], 'fact': [], 'agent': [], 'cf_class': [], } r[":requestid"] = rid r[":callerid"] = 'cert=%s' % CERTNAME
ami_host = config.get('AMI', 'host') ami_username = config.get('AMI', 'username') ami_password = config.get('AMI', 'password') print 'AMI host:', ami_host print 'AMI username:'******'AMI password:'******'=' * 80 sql_dsn = config.get('SQL', 'dsn') print 'SQL:', sql_dsn print '=' * 80 stomp = Client(stomp_host) stomp.connect(stomp_username, stomp_password) stomp.agent_channel = 'jms.queue.msg.' connection = connectionForURI(sql_dsn) sqlhub.processConnection = connection manager = asterisk.manager.Manager() #try: #try: manager.connect(ami_host) manager.login(ami_username, ami_password) manager.destination = stomp asteriskProtocolVersion = None
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))
import os.path from math import floor config = {} for line in file('/etc/mcollective/client.cfg', 'rt').readlines(): bits = line.split('=', 1) if len(bits) == 2: config[bits[0].strip()] = bits[1].strip() if len(argv) < 2: print >>stderr, '%s <check> [args]' % argv[0] exit(-1) certificate_name = os.path.basename(config['plugin.ssl_client_public'].replace('.pem', '')) stomp_client = Client(config.get('plugin.stomp.host', 'localhost'), port=int(config.get('plugin.stomp.port', 61613))) stomp_client.connect(config['plugin.stomp.user'], config['plugin.stomp.password']) private_key = load_key(config['plugin.ssl_client_private']) args = ' '.join(argv[2:]) body = { ':caller': 'cert=%s' % certificate_name, ':data': { ':process_results': True, ':command': argv[2], }, ':action': 'runcommand', ':agent': 'nrpe' } m = Message(body, stomp_client, target='nrpe', filter_=Filter(identity=argv[1]).dump())
# simulate fermentation readings from stompy.simple import Client from datetime import datetime import random from array import array import time import json # open the JMS Client using STOMP protocol stomp = Client(host='54.201.254.241', port=61613) stomp.connect() N = 50 # number of readings to take an average of readings = array('f') # the array of readings total = 0 # the sum of N readings target = 20 # set the target temp # initialise the temp readings and calculate the average for i in range(N): temp = random.uniform(target - 5, target + 5) # get random temp value readings.append(temp) # add temp to the array (this grows the array) total += temp # add temp to the total average = total / N # calculate the average temp # define min and max values and set to average min = average max = average
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)
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()