def listen_udp_proc(self, addrinfo, task=None): task.set_daemon() if addrinfo.family == socket.AF_INET6: mreq = socket.inet_pton(addrinfo.family, addrinfo.broadcast) mreq += struct.pack('@I', addrinfo.ifn) bc_sock = AsyncSocket(socket.socket(addrinfo.family, socket.SOCK_DGRAM)) if addrinfo.family == socket.AF_INET: bc_sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1) else: # addrinfo.family == socket.AF_INET6 bc_sock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_MULTICAST_HOPS, struct.pack('@i', 1)) bc_sock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_MULTICAST_IF, addrinfo.ifn) bc_sock.bind((addrinfo.ip, 0)) if self.scheduler_ip_addrs and self.scheduler_port: relay_request = {'ip_addrs': self.scheduler_ip_addrs, 'port': self.scheduler_port, 'version': __version__, 'sign': None} bc_sock.sendto('PING:'.encode() + serialize(relay_request), (self._broadcast, self.node_port)) listen_sock = AsyncSocket(socket.socket(addrinfo.family, socket.SOCK_DGRAM)) if addrinfo.family == socket.AF_INET: listen_sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) else: # addrinfo.family == socket.AF_INET6 listen_sock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_JOIN_GROUP, mreq) listen_sock.bind((addrinfo.ip, self.listen_port)) while 1: msg, addr = yield listen_sock.recvfrom(1024) if not msg.startswith('PING:'.encode()): logger.debug('Ignoring message "%s" from %s', msg[:min(len(msg), 5)], addr[0]) continue logger.debug('Ping message from %s (%s)', addr[0], addr[1]) try: info = deserialize(msg[len('PING:'.encode()):]) if info['version'] != __version__: logger.warning('Ignoring %s due to version mismatch: %s / %s', info['ip_addrs'], info['version'], __version__) continue self.scheduler_ip_addrs = info['ip_addrs'] + [addr[0]] self.scheduler_port = info['port'] except: logger.debug('Ignoring ping message from %s (%s)', addr[0], addr[1]) logger.debug(traceback.format_exc()) continue if info.get('relay', None): logger.debug('Ignoring ping back (from %s)', addr[0]) continue logger.debug('relaying ping from %s / %s', info['ip_addrs'], addr[0]) if self.node_port == self.listen_port: info['relay'] = 'y' # 'check if this message loops back to self yield bc_sock.sendto('PING:'.encode() + serialize(info), addr)
def verify_broadcast(self, addrinfo, msg, task=None): if msg.get('relay', None): raise StopIteration msg['relay'] = 'y' # TODO: check if current scheduler is done with nodes? if msg['sign']: msg['auth'] = dispy.auth_code(self.secret, msg['sign']) reply = None for scheduler_ip_addr in msg['ip_addrs']: msg['scheduler_ip_addr'] = scheduler_ip_addr sock = AsyncSocket(socket.socket(addrinfo.family, socket.SOCK_STREAM), keyfile=self.keyfile, certfile=self.certfile) sock.settimeout(dispy.MsgTimeout) try: yield sock.connect((scheduler_ip_addr, msg['port'])) yield sock.send_msg('RELAY_INFO:'.encode() + serialize(msg)) reply = yield sock.recv_msg() reply = deserialize(reply) except: continue else: break finally: sock.close() if not reply: raise StopIteration # TODO: since dispynetrelay is not aware of computations closing, if # more than one client sends ping, nodes will respond to different # clients self.scheduler_ip_addr = reply['ip_addrs'] = [scheduler_ip_addr] self.scheduler_port = reply['port'] bc_sock = AsyncSocket(socket.socket(addrinfo.family, socket.SOCK_DGRAM)) ttl_bin = struct.pack('@i', 1) if addrinfo.family == socket.AF_INET: if self.ipv4_udp_multicast: bc_sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, ttl_bin) else: bc_sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1) else: # addrinfo.family == socket.AF_INET6 bc_sock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_MULTICAST_HOPS, ttl_bin) bc_sock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_MULTICAST_IF, addrinfo.ifn) bc_sock.bind((addrinfo.ip, 0)) yield bc_sock.sendto('PING:'.encode() + serialize(msg), (addrinfo.broadcast, self.node_port)) bc_sock.close()
def tcp_req(conn, addr, task=None): conn.settimeout(5) try: msg = yield conn.recvall(auth_len) msg = yield conn.recv_msg() except: logger.debug(traceback.format_exc()) logger.debug('Ignoring invalid TCP message from %s:%s', addr[0], addr[1]) raise StopIteration finally: conn.close() logger.debug('Ping message from %s (%s)', addr[0], addr[1]) try: info = deserialize(msg[len('PING:'.encode()):]) if info['version'] != __version__: logger.warning('Ignoring %s due to version mismatch: %s / %s', info['ip_addrs'], info['version'], __version__) raise StopIteration # TODO: since dispynetrelay is not aware of computations # closing, if more than one client sends ping, nodes will # respond to different clients self.scheduler_ip_addrs = info['ip_addrs'] + [addr[0]] self.scheduler_port = info['port'] except: logger.debug('Ignoring ping message from %s (%s)', addr[0], addr[1]) logger.debug(traceback.format_exc()) raise StopIteration if info.get('relay', None): logger.debug('Ignoring ping back (from %s)', addr[0]) raise StopIteration logger.debug('relaying ping from %s / %s', info['ip_addrs'], addr[0]) if self.node_port == self.listen_port: info['relay'] = 'y' # 'check if this message loops back to self yield bc_sock.sendto('PING:'.encode() + serialize(info), (self._broadcast, self.node_port))
def discover_nodes(self, task=None): addrinfos = list(self.addrinfos.values()) for addrinfo in addrinfos: info_msg = { 'ip_addr': addrinfo.ip, 'port': self.info_port, 'sign': self.sign, 'version': _dispy_version } bc_sock = AsyncSocket( socket.socket(addrinfo.family, socket.SOCK_DGRAM)) bc_sock.settimeout(MsgTimeout) ttl_bin = struct.pack('@i', 1) if addrinfo.family == socket.AF_INET: if self.ipv4_udp_multicast: bc_sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, ttl_bin) else: bc_sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1) else: # addrinfo.family == socket.AF_INET6 bc_sock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_MULTICAST_HOPS, ttl_bin) bc_sock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_MULTICAST_IF, addrinfo.ifn) bc_sock.bind((addrinfo.ip, 0)) try: yield bc_sock.sendto(b'NODE_INFO:' + serialize(info_msg), (addrinfo.broadcast, self.node_port)) except Exception: pass bc_sock.close()
def service_time(self, host, control, time, task=None): node = self.nodes.get(dispy._node_ipaddr(host), None) if not node or not node._priv.auth: raise StopIteration(-1) sock = AsyncSocket(socket.socket(node._priv.sock_family, socket.SOCK_STREAM), keyfile=self.keyfile, certfile=self.certfile) sock.settimeout(MsgTimeout) try: yield sock.connect((node.ip_addr, node._priv.port)) yield sock.sendall(node._priv.auth) yield sock.send_msg('SERVICE_TIME:'.encode() + serialize({ 'control': control, 'time': time })) resp = yield sock.recv_msg() info = deserialize(resp) node.service_start = info['service_start'] node.service_stop = info['service_stop'] node.service_end = info['service_end'] resp = 0 except Exception: resp = -1 sock.close() if resp: dispy.logger.debug('Setting service %s time of %s to %s failed', control, host, time) raise StopIteration(resp)
def serve_clients(self, host, serve, task=None): node = self.nodes.get(dispy._node_ipaddr(host), None) if not node or not node._priv.auth: raise StopIteration(-1) sock = AsyncSocket(socket.socket(node._priv.sock_family, socket.SOCK_STREAM), keyfile=self.keyfile, certfile=self.certfile) sock.settimeout(MsgTimeout) try: yield sock.connect((node.ip_addr, node._priv.port)) yield sock.sendall(node._priv.auth) yield sock.send_msg('SERVE_CLIENTS:'.encode() + serialize({'serve': serve})) resp = yield sock.recv_msg() info = deserialize(resp) node.serve = info['serve'] resp = 0 except Exception: dispy.logger.debug('Setting serve clients %s to %s failed', host, serve) resp = -1 finally: sock.close() raise StopIteration(resp)
def verify_broadcast(self, addrinfo, msg, task=None): if msg.get('relay', None): raise StopIteration msg['relay'] = 'y' # TODO: check if current scheduler is done with nodes? if msg['sign']: msg['auth'] = dispy.auth_code(self.secret, msg['sign']) reply = None for scheduler_ip_addr in msg['ip_addrs']: msg['scheduler_ip_addr'] = scheduler_ip_addr sock = AsyncSocket(socket.socket(addrinfo.family, socket.SOCK_STREAM), keyfile=self.keyfile, certfile=self.certfile) sock.settimeout(dispy.MsgTimeout) try: yield sock.connect((scheduler_ip_addr, msg['port'])) yield sock.send_msg('RELAY_INFO:'.encode() + serialize(msg)) reply = yield sock.recv_msg() reply = deserialize(reply) except Exception: continue else: break finally: sock.close() if not reply: raise StopIteration # TODO: since dispynetrelay is not aware of computations closing, if # more than one client sends ping, nodes will respond to different # clients self.scheduler_ip_addr = reply['ip_addrs'] = [scheduler_ip_addr] self.scheduler_port = reply['port'] bc_sock = AsyncSocket(socket.socket(addrinfo.family, socket.SOCK_DGRAM)) ttl_bin = struct.pack('@i', 1) if addrinfo.family == socket.AF_INET: if self.ipv4_udp_multicast: bc_sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, ttl_bin) else: bc_sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1) else: # addrinfo.family == socket.AF_INET6 bc_sock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_MULTICAST_HOPS, ttl_bin) bc_sock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_MULTICAST_IF, addrinfo.ifn) bc_sock.bind((addrinfo.ip, 0)) yield bc_sock.sendto('PING:'.encode() + serialize(msg), (addrinfo.broadcast, self.node_port)) bc_sock.close()
def relay_msg(msg, task=None): relay = {'ip_addrs': self.scheduler_ip_addr, 'port': self.scheduler_port, 'version': __version__} relay['relay'] = 'y' sock = AsyncSocket(socket.socket(addrinfo.family, socket.SOCK_STREAM), keyfile=self.keyfile, certfile=self.certfile) sock.settimeout(dispy.MsgTimeout) yield sock.connect((msg['ip_addr'], msg['port'])) yield sock.sendall(dispy.auth_code(self.secret, msg['sign'])) yield sock.send_msg('PING:'.encode() + serialize(relay)) sock.close()
def sched_udp_proc(self, addrinfo, task=None): task.set_daemon() sched_sock = AsyncSocket( socket.socket(addrinfo.family, socket.SOCK_DGRAM)) if addrinfo.family == socket.AF_INET: sched_sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) else: # addrinfo.family == socket.AF_INET6 mreq = socket.inet_pton(addrinfo.family, addrinfo.broadcast) mreq += struct.pack('@I', addrinfo.ifn) sched_sock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_JOIN_GROUP, mreq) sched_sock.bind((addrinfo.ip, self.scheduler_port)) while 1: msg, addr = yield sched_sock.recvfrom(1024) if (not msg.startswith('PING:'.encode()) or not self.scheduler_ip_addrs or not self.scheduler_port): logger.debug('Ignoring ping message from %s (%s)', addr[0], addr[1]) continue try: info = deserialize(msg[len('PING:'.encode()):]) assert info['version'] == __version__ # assert isinstance(info['cpus'], int) except: logger.debug(traceback.format_exc()) msg = { 'ip_addrs': self.scheduler_ip_addrs, 'port': self.scheduler_port, 'version': __version__ } if info.get('relay', None): logger.debug('Ignoring ping back from %s: %s', addr[0], info) continue msg['relay'] = 'y' relay_sock = AsyncSocket( socket.socket(addrinfo.family, socket.SOCK_DGRAM)) relay_sock.bind((addrinfo.ip, 0)) yield relay_sock.sendto('PING:'.encode() + serialize(msg), (info['ip_addr'], info['port'])) relay_sock.close()
def set_cpus(self, host, cpus, task=None): node = self.nodes.get(host, None) if not node or not node._priv.auth: raise StopIteration(-1) sock = AsyncSocket(socket.socket(node._priv.sock_family, socket.SOCK_STREAM), keyfile=self.keyfile, certfile=self.certfile) sock.settimeout(MsgTimeout) try: yield sock.connect((node.ip_addr, node._priv.port)) yield sock.sendall(node._priv.auth) yield sock.send_msg('SET_CPUS:'.encode() + serialize({'cpus': cpus})) resp = yield sock.recv_msg() info = deserialize(resp) node.cpus = info['cpus'] except Exception: dispy.logger.debug('Setting cpus of %s to %s failed', host, cpus) raise StopIteration(-1) else: raise StopIteration(0) finally: sock.close()
def serve_clients(self, host, serve, task=None): node = self.nodes.get(dispy._node_ipaddr(host), None) if not node or not node._priv.auth: raise StopIteration(-1) sock = AsyncSocket(socket.socket(node._priv.sock_family, socket.SOCK_STREAM), keyfile=self.keyfile, certfile=self.certfile) sock.settimeout(MsgTimeout) try: yield sock.connect((node.ip_addr, node._priv.port)) yield sock.sendall(node._priv.auth) yield sock.send_msg('SERVE_CLIENTS:' + serialize({'serve': serve})) resp = yield sock.recv_msg() info = deserialize(resp) node.serve = info['serve'] resp = 0 except Exception: dispy.logger.debug('Setting serve %s to %s failed', host, serve) resp = -1 finally: sock.close() raise StopIteration(resp)
def set_cpus(self, host, cpus, task=None): node = self.nodes.get(host, None) if not node or not node._priv.auth: raise StopIteration(-1) sock = AsyncSocket(socket.socket(node._priv.sock_family, socket.SOCK_STREAM), keyfile=self.keyfile, certfile=self.certfile) sock.settimeout(MsgTimeout) try: yield sock.connect((node.ip_addr, node._priv.port)) yield sock.sendall(node._priv.auth) yield sock.send_msg('SET_CPUS:' + serialize({'cpus': cpus})) resp = yield sock.recv_msg() info = deserialize(resp) node.cpus = info['cpus'] except Exception: dispy.logger.debug('Setting cpus of %s to %s failed', host, cpus) raise StopIteration(-1) else: raise StopIteration(0) finally: sock.close()
def get_node_info(self, node, task=None): auth = node._priv.auth if not auth: auth = dispy.auth_code(self.secret, node._priv.sign) sock = AsyncSocket(socket.socket(node._priv.sock_family, socket.SOCK_STREAM), keyfile=self.keyfile, certfile=self.certfile) sock.settimeout(MsgTimeout) try: yield sock.connect((node.ip_addr, node._priv.port)) yield sock.sendall(auth) yield sock.send_msg(b'NODE_INFO:' + serialize({'sign': self.sign})) info = yield sock.recv_msg() except Exception: dispy.logger.debug('Could not get node information from %s:%s', node.ip_addr, node._priv.port) # dispy.logger.debug(traceback.format_exc()) raise StopIteration(-1) finally: sock.close() try: info = deserialize(info) node.name = info['name'] node.cpus = info['cpus'] node.max_cpus = info['max_cpus'] except Exception: sign = info.decode() if node._priv.sign == sign: node.update_time = time.time() raise StopIteration(0) else: node._priv.sign = sign ret = yield self.get_node_info(node, task=task) raise StopIteration(ret) else: node._priv.auth = auth self.set_node_info(node, info) raise StopIteration(0)
def timer_proc(self, task=None): task.set_daemon() last_ping = 0 addrinfos = list(self.addrinfos.values()) while 1: yield task.sleep(self.poll_interval) now = time.time() with self.lock: nodes = list(self.nodes.values()) # TODO: it may be better to have nodes send updates periodically for node in nodes: if node._priv.auth: Task(self.update_node_info, node) if (now - last_ping) >= self.ping_interval: last_ping = now for addrinfo in addrinfos: info_msg = {'ip_addr': addrinfo.ip, 'port': self.info_port, 'sign': self.sign, 'version': _dispy_version} bc_sock = AsyncSocket(socket.socket(addrinfo.family, socket.SOCK_DGRAM)) bc_sock.settimeout(MsgTimeout) ttl_bin = struct.pack('@i', 1) if addrinfo.family == socket.AF_INET: if self.ipv4_udp_multicast: bc_sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, ttl_bin) else: bc_sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1) else: # addrinfo.family == socket.AF_INET6 bc_sock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_MULTICAST_HOPS, ttl_bin) bc_sock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_MULTICAST_IF, addrinfo.ifn) bc_sock.bind((addrinfo.ip, 0)) try: yield bc_sock.sendto('NODE_INFO:' + serialize(info_msg), (addrinfo.broadcast, self.node_port)) except Exception: pass bc_sock.close()
def service_time(self, host, control, time, task=None): node = self.nodes.get(dispy._node_ipaddr(host), None) if not node or not node._priv.auth: raise StopIteration(-1) sock = AsyncSocket(socket.socket(node._priv.sock_family, socket.SOCK_STREAM), keyfile=self.keyfile, certfile=self.certfile) sock.settimeout(MsgTimeout) try: yield sock.connect((node.ip_addr, node._priv.port)) yield sock.sendall(node._priv.auth) yield sock.send_msg('SERVICE_TIME:' + serialize({'control': control, 'time': time})) resp = yield sock.recv_msg() info = deserialize(resp) node.service_start = info['service_start'] node.service_stop = info['service_stop'] node.service_end = info['service_end'] resp = 0 except Exception: resp = -1 sock.close() if resp: dispy.logger.debug('Setting service %s time of %s to %s failed', control, host, time) raise StopIteration(resp)
def get_node_info(self, node, task=None): auth = node._priv.auth if not auth: auth = dispy.auth_code(self.secret, node._priv.sign) sock = AsyncSocket(socket.socket(node._priv.sock_family, socket.SOCK_STREAM), keyfile=self.keyfile, certfile=self.certfile) sock.settimeout(MsgTimeout) try: yield sock.connect((node.ip_addr, node._priv.port)) yield sock.sendall(auth) yield sock.send_msg('NODE_INFO:' + serialize({'sign': self.sign})) info = yield sock.recv_msg() except Exception: dispy.logger.debug('Could not get node information from %s:%s', node.ip_addr, node._priv.port) # dispy.logger.debug(traceback.format_exc()) raise StopIteration(-1) finally: sock.close() try: info = deserialize(info) node.name = info['name'] node.cpus = info['cpus'] node.max_cpus = info['max_cpus'] except Exception: sign = info.decode() if node._priv.sign == sign: node.update_time = time.time() raise StopIteration(0) else: node._priv.sign = sign raise StopIteration(yield self.get_node_info(node, task=task)) else: node._priv.auth = auth self.set_node_info(node, info) raise StopIteration(0)
cnn = dbConnection.Cnn('gnss_data.cfg') GamitConfig = pyGamitConfig.GamitConfiguration('CPC-Ar_session.cfg') archive = pyArchiveStruct.RinexStruct(cnn) import pycos import pyDate dr = [pyDate.Date(year=2010, doy=1), pyDate.Date(year=2010, doy=2)] s1 = Station(cnn, 'rms', 'igm1', dr) s2 = Station(cnn, 'rms', 'lpgs', dr) s3 = Station(cnn, 'rms', 'chac', dr) s4 = Station(cnn, 'cap', 'chac', dr) si = StationInstance(cnn, archive, s1, dr[0], GamitConfig) gs = pyGamitSession.GamitSession(cnn, archive, 'igg', 'IGN', None, pyDate.Date(year=2020, doy=100), GamitConfig, [s1, s2, s3]) c = StationCollection() a = pycos.unserialize(pycos.serialize(gs)) print(a) b = pycos.unserialize(pycos.serialize(s1)) print(b) c.append(s1) c.append(s2) c.append(s3) c = pycos.unserialize(pycos.serialize(c)) print(c) # c.append(s4) # print c # c.replace_alias([s1, s2], ['zzz1', 'zzz1']) # print c # print c.get_active_stations(dr[0]).ismember(s3) # for i, s in enumerate(c): # print i, s