def handle_client_reply(self, conn): try: data = utils.recv_all(conn) client_reply = json.loads(data) conn.close() except: logger.exception("Failed to decode client reply.") return try: validate(client_reply, settings.REPLY_SCHEMA) except: logger.exception("Failed to validate client reply.") return logger.debug("Got client reply: %s" % (json.dumps(client_reply))) if 'clientScan' in client_reply and 'iwScanOutput' in client_reply['clientScan'][0]: client_reply['clientScan'][0]['resultList'] = parse_iw_scan(client_reply['clientScan'][0]['iwScanOutput']) del client_reply['clientScan'][0]['iwScanOutput'] self.reply_lock.acquire() for key in CLIENT_COLLECT: if key in client_reply: self.reply[key].extend(client_reply[key]) self.reply_lock.release()
def main() : public_ip = utils.get_wan_ip() if public_ip is not None: logger.debug("Public IP is %s" % (public_ip)) else: logger.error("Failed to get public IP.") return logger.debug("Cleanup iperf servers.") try: subprocess.check_call('pgrep -f "iperf" | xargs kill -9', shell=True) except: pass server_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server_sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) server_sock.bind(('0.0.0.0', settings.PUBLIC_TCP_PORT)) server_sock.listen(settings.PUBLIC_BACKLOG) logger.debug("Listening on %s (%d)..." % (public_ip, settings.PUBLIC_TCP_PORT)) for t in [HeartbeatThread]: t().start() try: while True: conn, addr = server_sock.accept() data = utils.recv_all(conn) try: request = json.loads(data) except: logger.exception("Failed to read message.") continue if 'action' in request: schema = settings.REQUEST_SCHEMA handler_thread = HANDLER_MAPPING[request['action']](conn, request) elif 'request' in request: schema = settings.REPLY_SCHEMA handler_thread = ReplyHandler(request) else: logger.error("Not a request or reply.") continue try: validate(request, schema) except: logger.exception("Failed to validate request msg.") continue logger.debug("Got message from %s: %s" % (addr, json.dumps(request))) handler_thread.start() except: logger.exception("Failed to listen.") server_sock.close()
def init_nn(): with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock: sock.connect(SERVER_INFO) sock.sendall(b'w') # Ask for initial weights as worker node # Receive and unpack weights into a list data = recv_all(sock, bytes_expected) # Unpack and set network weights init_weights = deque(struct.unpack('{}f'.format(num_weights), data)) nn.set_weights(init_weights)
def init_nn(): """Send initialization string to the server to request to be a validator.""" with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock: sock.connect(SERVER_INFO) sock.sendall(b'v') # Ask for initial weights # Receive and unpack weights into a list data = recv_all(sock, bytes_expected) # Unpack weights from bytes and put into a queue for efficient network updating init_weights = deque(struct.unpack('{}f'.format(num_weights), data)) nn.set_weights(init_weights)
def connect_to_master(): with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock: sock.connect(SERVER_INFO) data = recv_all(sock, bytes_expected) if not data: return False try: # Gather new network weights new_network_weights = deque( struct.unpack('{}f'.format(num_weights), data)) except: print('Error unpacking weights!') return True # Set local network weights nn.set_weights(new_network_weights) return True
def transmit_to_master(network_weight_updates): print('Transmitting updates to master...') # As packed byte data data = struct.pack('{}f'.format(num_weights), *network_weight_updates) with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock: sock.connect(SERVER_INFO) sock.sendall(data) # Send weight updates to master data = recv_all(sock, bytes_expected) # Receive global network weights if not data: return False try: new_network_weights = deque( struct.unpack('{}f'.format(num_weights), data)) except: print('Error unpacking weights!') return True nn.set_weights(new_network_weights) return True
def handle(self): """A client connected to the server; handle the request.""" global batch_no if self.client_address[0] in workers: # Established worker # self.request is a TCP socket connected to the client print('Worker connected... handling') self.data = recv_all(self.request, bytes_expected) try: network_weight_updates = deque(struct.unpack('{}f'.format(num_weights), self.data)) print('\tReceived data unpacked') except: print('\tError unpacking data') else: print('\tTraining master network') nn.train_master(network_weight_updates) batch_no += 1 print('\tCompleted batch #{} of {}'.format(batch_no, total_batches)) print('\tGetting network weights') network_weights = nn.get_weights() print('\tPacking response') response = struct.pack('{}f'.format(num_weights), *network_weights) print('\tSending back weights') self.request.sendall(response) elif self.client_address[0] in validators: # Established validator print('Validator connected... handling') print('\tGetting network weights') network_weights = nn.get_weights() print('\tPacking response') response = struct.pack('{}f'.format(num_weights), *network_weights) print('\tSending back weights') self.request.sendall(response) else: # New node connected # Initialize client into set of workers print('Contacted by new node...') self.data = self.request.recv(1024).strip() print('\tReceived init string') # New nodes should send b'w' to become a worker, or b'v' to become a validator if self.data == b'w': print('\tNew WORKER node... handling') workers.add(self.client_address[0]) # Add IP to list of workers elif self.data == b'v': print('\tNew VALIDATOR node... handling') validators.add(self.client_address[0]) # Add IP to list of validators else: print('\tUnknown node request! Moving on...') return # Gather current network weights, pack as bytes, and send back to client network_weights = nn.get_weights() response = struct.pack('{}f'.format(num_weights), *network_weights) print('\tSending network weights') self.request.sendall(response) print('\tDONE with node') if batch_no > total_batches: # Done with desired number of batches! Finalize network training save_pkl(nn, 'saved_nn/iris_nn_relu_16n8n_100e_16b_l2.pkl') # Save trained network print('Eval training...') eval_tr = nn.eval_classification(train) print('Eval validation...') eval_v = nn.eval_classification(validation) print('Eval testing...') eval_t = nn.eval_classification(test) print('Batches: {}\tTrain: {:.3f}\tValidation: {:.3f}\tTest: {:.3f}' .format(batch_no, eval_tr, eval_v, eval_t)) exit()
def main(): public_ip = utils.get_wan_ip() if public_ip is not None: logger.debug("Public IP is %s" % (public_ip)) else: logger.error("Failed to get public IP.") return logger.debug("Cleanup iperf servers.") try: subprocess.check_call('pgrep -f "iperf" | xargs kill -9', shell=True) except: pass server_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server_sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) server_sock.bind(('0.0.0.0', settings.PUBLIC_TCP_PORT)) server_sock.listen(settings.PUBLIC_BACKLOG) logger.debug("Listening on %s (%d)..." % (public_ip, settings.PUBLIC_TCP_PORT)) for t in [HeartbeatThread]: t().start() try: while True: conn, addr = server_sock.accept() data = utils.recv_all(conn) try: request = json.loads(data) except: logger.exception("Failed to read message.") continue if 'action' in request: schema = settings.REQUEST_SCHEMA handler_thread = HANDLER_MAPPING[request['action']](conn, request) elif 'request' in request: schema = settings.REPLY_SCHEMA handler_thread = ReplyHandler(request) else: logger.error("Not a request or reply.") continue try: validate(request, schema) except: logger.exception("Failed to validate request msg.") continue logger.debug("Got message from %s: %s" % (addr, json.dumps(request))) handler_thread.start() except: logger.exception("Failed to listen.") server_sock.close()