def __init__(self, hostname=None, port=None): self.buffer_map = BufferQueueMap() args = { 'hostname': hostname, 'port': port, 'buffer_map': self.buffer_map } super(self.__class__, self).__init__(**args) logger.info("Starting Bqueue server on port {}".format(str(self.port)))
def run(self): while 1: logger.info('Server thread waiting for data') data = self.sock.recv(BUFFER_SIZE) if data: response = self.process_data(data) self.sock.send(response.get_message()) else: break
def callback_method(data): jsoned_data = json.loads(data) queue_name = jsoned_data['queue'] actual_data = jsoned_data['data'] if PRINT_DATA_LOG: logger.info( "data in callback subscriber for queue {} received \n{}".format( queue_name, actual_data)) increment_counter(len(actual_data))
def send_data_to_host(self, subscriber, data): host, port = self.get_subscriber_host(subscriber) try: client = BQueueSocketClient(hostname=host, port=port) client.send_data(data) logger.info("sending data to host {} with size {}".format( host, sys.getsizeof(data))) except socket.error, e: if 'Connection refused' in e: logger.error('*** Connection refused ***')
def is_queue_full(self, queue_name): if self.queue_size_counter[queue_name] == self.queue_size_definition[ queue_name]: logger.info( '{} queue is full, broadcasting data to subscribers'.format( queue_name)) subscriber_list = self.subscriber_map[queue_name] logger.info('available subscribers for queue {} - {}'.format( queue_name, subscriber_list)) data = json.dumps({ 'data': self.queue_map[queue_name], 'queue': queue_name }) self.publish_data_to_consumer(subscriber_list, data) self.clear_queue(queue_name)
def send_data(self, data): message = data logger.info("sending data from client") if PRINT_DATA_LOG: logger.info("{}".format(message)) self.sock.send(data) data = self.sock.recv(BUFFER_SIZE) logger.info("receive from server") if PRINT_DATA_LOG: logger.info("{}".format(data))
def process_data(self, data): data = json.loads(data) buffer_map = self.args['buffer_map'] if data.get('operation') == 'create_queue': logger.info("create queue request received") return buffer_map.add_queue(data.get('queue'), data.get('buffer_size')) elif data.get('operation') == 'subscribe': logger.info("queue subscribe request received") return buffer_map.subscribe_to_queue(data.get('host'), data.get('queue')) elif data.get('operation') == 'delete_queue': logger.info("queue delete request received") return buffer_map.delete_queue(data.get('queue')) elif data.get('queue'): logger.info("add data to queue request received") return buffer_map.append_data_to_queue(data.get('queue'), data.get('data'))
def bind_thread(self, clientsocket): BQueueServerThread(clientsocket, **self.args) logger.info("BQueueServerThread bounded with server")
def send_data(self, data): message = data logger.info("sending data {} from subscriber".format(message)) self.sock.send(data) data = self.sock.recv(BUFFER_SIZE) logger.info("{} receive from bufferqueue in subscriber".format(data))
def increment_counter(count): global received_messages received_messages += count logger.info("received messages count \n{}".format(str(received_messages)))
def callback_method(data): jsoned_data = json.loads(data) queue_name = jsoned_data['queue'] actual_data = jsoned_data['data'] if PRINT_DATA_LOG: logger.info( "data in callback subscriber for queue {} received \n{}".format( queue_name, actual_data)) increment_counter(len(actual_data)) def increment_counter(count): global received_messages received_messages += count logger.info("received messages count \n{}".format(str(received_messages))) if __name__ == "__main__": subscriber = BQueueSubscriber(hostname=LOCALHOST, s_port=11001, callback=callback_method) subscriber.subscribe_queue('test1') subscriber.subscribe_queue('test2') subscriber.subscribe_queue('test3') subscriber.subscribe_queue('test4') subscriber.subscribe_queue('test5') logger.info("received messages count \n{}".format(str(received_messages))) subscriber.listen()