def transform(self, data): """Map *data* to the appropriate Broker message. :param data: The data to map. (json) :returns: The corresponding Broker message. (pybroker.Message) """ self.log.debug("Trying to map '{}'.".format(data)) for mapping in self.mappings: event_name = mapping['name'] self.log.debug("Trying mapping for '{}'.".format(event_name)) local_mapping = mapping['mapping'] # the actual traversion try: broker_msg = { k2: v2 for k, v in data.iteritems() for k2, v2 in self._traverse_to_end(k, v, local_mapping).iteritems() } except Exception: self.log.info("Failed to convert message properly. " "Ignoring format.") continue self.log.info("Using mapping for '{}'.".format(event_name)) message = pb.message() # prepending with event-name for broker message.append(pb.data(event_name)) # setting up the final message in desired order local_message = mapping['message'] for item in local_message: if item not in broker_msg: self.log.debug("Invalid message. Format unknown.") break broker_item = broker_msg[item] self.log.debug( "Add converted brokerObject '{}' to message.".format( broker_item)) message.append(pb.data(broker_item)) else: return message else: self.log.warn("No valid mapping found. Discarding message.") return
def testSuccessSlaveLookupOnInit(self): """Test successful lookup of Bro-Slave instance during sender init""" self.assertTrue(self.master_listening) self.master_store.clear() balance_to = "bro-slave-127.0.0.1:9999" self.master_store.insert(pb.data(self.connector_id), pb.data(balance_to)) sender = Sender(self.master_ip, self.master_port, self.connector_ep, self.topic, self.connector_id) # force wait for this test self.master_mock.incoming_connection_status().need_pop() # do test slave lookup on init self.assertEqual(sender.current_slave, balance_to)
def _lookup_and_get_current_slave(self): """Return the slave bro (name) that should be peered with""" current_slave = None if self.balanced_slaves: try: current_slave = self.balanced_slaves.lookup( broker.data(self.broker_endpoint)).data().as_string() self.log.debug("Lookup {} returns {}".format( self.broker_endpoint, current_slave)) except Exception, e: self.log.error("Error looking up slave on connector '{}'. " "Error: '{}'".format(self.broker_endpoint, str(e)))
def testSuccessSlaveLookupOnSend(self): """Test successful (re-)lookup of Bro-Slave during sender send""" self.assertTrue(self.master_listening) self.master_store.clear() sender = Sender(self.master_ip, self.master_port, self.connector_ep, self.topic, self.connector_id) # verify no initial peering took place self.assertEqual(sender.current_slave, None) balance_to = "bro-slave-127.0.0.1:9999" self.master_store.insert(pb.data(self.connector_id), pb.data(balance_to)) sleep(0.1) # time for the update of the shared data msg = pb.message() msg.append(pb.data(self.topic)) msg.append(pb.data("MESSAGE")) sender.send(msg) # do test slave lookup during send self.assertEqual(sender.current_slave, balance_to)
def send(self, msg): """Send the Broker message to the peer. :param msg: The message to be sent. (Broker message) """ msg.append(broker.data(self.connector_id)) try: self._repeer_connector_to_slave() if self.current_slave: self.log.info("Sending to {}".format(self.current_slave)) self._send_to_bro(self.connector_to_slave, False, msg) else: self.log.warn("Not peered with any slave, falling back to " "master: {}".format(self.master_name)) self._send_to_bro(self.connector_to_master, True, msg) except Exception, e: local_endpoint = self.current_slave or self.master_name self.log.error( "Error sending data from {} to {}. Exception: {}".format( self.broker_endpoint, local_endpoint, str(e)))
def sendMessageToTopic(self, msg): """ Send a message to the commands topic :param msg: """ self.endpoint.send("commands", pybroker.message([pybroker.data(msg)]))