def send(self, supl_message): pdu = ULP.ULP.ULP_PDU pdu.set_val(supl_message) logger.debug(pdu.to_asn1()) tx_data = pdu.to_uper() self.supl_server.send(self.client_fileno, tx_data)
def run(self): self.ssl_context.load_cert_chain(certfile="security/server.pem", keyfile="security/server.key") # need key without password logger.debug('Create socket') serversocket = socket.socket() serversocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) serversocket.bind(('192.168.1.30', 7275)) logger.debug('Listenning...') serversocket.listen() with self.ssl_context.wrap_socket(serversocket, server_side=True) as ssock: self.epoll.register(ssock.fileno(), select.EPOLLIN) try: while self.is_running: events = self.epoll.poll(1) for fileno, event in events: if fileno == ssock.fileno(): client, address = ssock.accept() self.add_client(client, address) elif event & select.EPOLLIN: self.receive(fileno) #elif event & select.EPOLLOUT: # self.send(fileno) elif event & select.EPOLLHUP: self.del_client(fileno) finally: self.epoll.unregister(ssock.fileno()) self.epoll.close() ssock.close()
def add_client(self, client, address): self.clients[client.fileno()] = (client, address) self.epoll.register(client.fileno(), select.EPOLLIN) self.callback.process_client_connected(client.fileno()) logger.debug("Client is connected, address: %s:%d" % address) # address format is ('192.168.1.30', 55908)
def del_client(self, fileno): if self.clients.__contains__(fileno): (client, address) = self.clients[fileno] self.epoll.unregister(fileno) client.close() del self.clients[fileno] self.callback.process_client_disconnected(fileno) logger.debug("Client is disconnected, address: %s:%d" % (address[0], address[1]))
def send(self, fileno, data): if (data != None and self.clients.__contains__(fileno)): (client, address) = self.clients[fileno] logger.debug("Sending %d bytes data to client: %s:%d" % (len(data), address[0], address[1])) length = client.send(data) logger.debug("Sent %d bytes data to client: %s:%d" % (length, address[0], address[1])) else: logger.debug("Failed to send data to: %s:%d, no such client!" % (address[0], address[1]))
def receive(self, rx_data): string = rx_data.hex() logger.debug("[RX][%d]: %s" % (len(string) / 2, string)) pdu = ULP.ULP.ULP_PDU pdu.from_uper(rx_data) logger.debug(pdu.to_asn1()) (msgtype, message) = get_val_at(pdu, ["message"]) if "msSUPLSTART" == msgtype: self.send_supl_reponse(message) elif "msSUPLPOSINIT" == msgtype: self.send_supl_pos(message) else: logger.debug("Received unexpected message: %s" % msgtype)
def process_client_connected(self, client_fileno): if self.sessions.__contains__(client_fileno): logger.debug("Error, failed to add session with fileno: %d, session already exists!" % client_fileno) else: self.sessions[client_fileno] = SuplSession(self, client_fileno)
def send(self, client_fileno, tx_data): if self.sessions.__contains__(client_fileno): self.tls_server.send(client_fileno, tx_data) else: logger.debug("Error, failed to send data to peer with fileno: %d, no such session!" % client_fileno)
def receive(self, client_fileno, rx_data): if self.sessions.__contains__(client_fileno): self.sessions[client_fileno].receive(rx_data) else: logger.debug("Error, failed to deliver data to session with fileno: %d, no such session!" % client_fileno)
def process_client_disconnected(self, client_fileno): if self.sessions.__contains__(client_fileno): del self.sessions[client_fileno] else: logger.debug("Error, failed to remove session with fileno: %d, no such session!" % client_fileno)