def __init__(self, **kwargs): '''Create and register the external sockets.''' super(Proxy, self).__init__(**kwargs) # Use separate context for these sockets to avoid # authentication conflicts with other sockets. ctx = zmq.Context() self.zap_sock = ctx.socket(zmq.REP) self.zap_sock.bind('inproc://zeromq.zap.01') self.reactor.register(self.zap_sock, self.handle_authentication) self.outgoing = messaging.Socket(zmq.XPUB, context=ctx) self._config_socket(self.outgoing) self.outgoing.zap_domain = 'building.outgoing' self.incoming = messaging.Socket(zmq.PULL, context=ctx) self._config_socket(self.incoming) self.incoming.zap_domain = 'building.incoming' key = config.get('secret-key') if key: self.outgoing.curve_secretkey = key self.outgoing.curve_server = 1 self.incoming.curve_secretkey = key self.incoming.curve_server = 1 self.hosts = config.get('hosts', {}) print "self.hosts " + str(self.hosts) self.allow_sub = set(key for host in self.hosts.itervalues() for key, allow in [(host.get('public-key'), host.get('allow', 'sub'))] if key and allow in ['pub', 'sub']) print "self.allow_sub " + str(self.allow_sub) self.allow_pub = set(key for host in self.hosts.itervalues() for key, allow in [(host.get('public-key'), host.get('allow', 'sub'))] if key and allow == 'pub') print "self.allow_pub " + str(self.allow_pub)
def add_subscription(self, building, topic, cookie=None): '''Add external building subscription.''' sock = self.subs.get(building) _launch_file = os.path.join( Agents_DIR + "MultiBuilding/multibuildingagent.launch.json") with open(_launch_file, 'r') as f: self.multi_node_data = json.load(f) self.hosts = self.multi_node_data['hosts'] if not sock: host = self.hosts.get(building) address = host.get('sub') if host else None print "address" + str(address) # Handle missing address if not address: headers = {'Cookie': cookie} if cookie else {} self.publish_error( building, topic, headers, errno.ENOENT, 'building subscription address not found') return sock = messaging.Socket(zmq.SUB) key = host.get('public-key') if key: sock.curve_serverkey = key sock.curve_secretkey = config.get('secret-key') sock.curve_publickey = config.get('public-key') self._config_socket(sock) sock.connect(address) self.subs[building] = sock self.rsubs[sock] = building self.reactor.register(sock, self.handle_republish) sock.subscribe = topic.encode('utf-8')
def add_subscription(self, building, topic, cookie=None): '''Add external building subscription.''' sock = self.subs.get(building) if not sock: host = self.hosts.get(building) address = host.get('sub') if host else None # Handle missing address if not address: headers = {'Cookie': cookie} if cookie else {} self.publish_error( building, topic, headers, errno.ENOENT, 'building subscription address not found') return sock = messaging.Socket(zmq.SUB) key = host.get('public-key') if key: sock.curve_serverkey = key sock.curve_secretkey = config.get('secret-key') sock.curve_publickey = config.get('public-key') self._config_socket(sock) sock.connect(address) self.subs[building] = sock self.rsubs[sock] = building self.reactor.register(sock, self.handle_republish) sock.subscribe = topic.encode('utf-8')
def on_send(self, full_topic, headers, message, match): '''Handle external building publish requests.''' building, topic = match.groups() _launch_file = os.path.join( Agents_DIR + "MultiBuilding/multibuildingagent.launch.json") with open(_launch_file, 'r') as f: self.multi_node_data = json.load(f) # self.connected_nodes = list() # for k, v in self.multi_node_data['hosts'].items(): self.hosts = self.multi_node_data['hosts'] # pass # else: # self.connected_nodes.append(k) print "MultiBuilding Agent sending message to building: " + building print "MultiBuilding Agent sending message topic: " + topic sock, seq = self.pubs.get(building, (None, None)) # print "sock "+str(sock) # print "seq "+str(seq) if not sock: host = self.hosts.get(building) address = host.get('pub') if host else None print "host " + str(host) print "address " + str(address) # Handle missing address if not address: cookie = headers.get('Cookie') headers = {'Cookie': cookie} if cookie else {} self.publish_error(building, topic, headers, errno.ENOENT, 'building publish address not found') return sock = messaging.Socket(zmq.PUSH) key = host.get('public-key') if key: sock.curve_serverkey = key sock.curve_secretkey = config.get('secret-key') sock.curve_publickey = config.get('public-key') self._config_socket(sock) sock.connect(address) if seq != self.sequence: self.pubs[building] = sock, self.sequence while True: try: sock.send_message(topic, headers, *message, flags=NOBLOCK) print "MultiBuiding Agent sent message" except ZMQError as e: if e.errno == EINTR: continue self.pubs.pop(building, None) sock.close() self.publish_error(building, topic, headers, errno.ECONNABORTED, 'message not sent; socket closed') break
def on_send(self, full_topic, headers, message, match): '''Handle external building publish requests.''' building, topic = match.groups() sock, seq = self.pubs.get(building, (None, None)) if not sock: host = self.hosts.get(building) address = host.get('pub') if host else None # Handle missing address if not address: cookie = headers.get('Cookie') headers = {'Cookie': cookie} if cookie else {} self.publish_error(building, topic, headers, errno.ENOENT, 'building publish address not found') return sock = messaging.Socket(zmq.PUSH) key = host.get('public-key') if key: sock.curve_serverkey = key sock.curve_secretkey = config.get('secret-key') sock.curve_publickey = config.get('public-key') self._config_socket(sock) sock.connect(address) if seq != self.sequence: self.pubs[building] = sock, self.sequence while True: try: sock.send_message(topic, headers, *message, flags=NOBLOCK) except ZMQError as e: if e.errno == EINTR: continue self.pubs.pop(building, None) sock.close() self.publish_error(building, topic, headers, errno.ECONNABORTED, 'message not sent; socket closed') break
def run(): sock = messaging.Socket(zmq.PUSH) sock.connect(publish_address) with contextlib.closing(sock): algo(self, sock)