Ejemplo n.º 1
0
    def startMeshListener(self):
        # make sure we find an unused port
        while Comms.test_port(self.meshPort):
            self.meshPort = random.randint(40000, 65000)

        self.meshListener = Comms.create_server_socket("0.0.0.0",
                                                       self.meshPort)
Ejemplo n.º 2
0
    def server(self, host, port):
        slist = []  #array of client sockets
        nid = 0
        prompt = "# (stuck? type HELP)> "

        server_sock = Comms.create_server_socket(host, port)
        slist.append(server_sock)
        slist.append(sys.stdin)

        print("Server started on IPs : " + str(host))
        print("Server started on port: " + str(port))

        # add mesh listener if necessary
        if (self.meshListener):
            slist.append(self.meshListener)
            print("MeshNet Listener started on port: " + str(self.meshPort))

        sys.stdout.write(prompt)
        sys.stdout.flush()

        displayPrompt = False

        while (1):
            self.addNeighbor()
            displayPrompt = False
            # get the list sockets which are ready to be read through select
            # 4th arg, time_out  = 0 : poll and never block
            ready_to_read, ready_to_write, in_error = select.select(
                slist, [], [], 0)

            for sock in ready_to_read:
                displayPrompt = False
                if (self.meshListener) and (sock == self.meshListener):
                    sockfd, addr = sock.accept()
                    msg = Comms.readMsg(sockfd, 4096)

                    # construct msg hash
                    m = hashlib.sha256()
                    m.update(msg.encode('ISO-8859-1'))
                    hash_key = m.hexdigest()
                    timestamp = datetime.datetime.now()

                    good = True
                    if hash_key in self.hashMsgs:
                        stored_timestamp = datetime.datetime.strptime(
                            self.hashMsgs[hash_key], '%Y-%m-%d %H:%M:%S.%f')
                        if (timestamp >= (stored_timestamp +
                                          datetime.timedelta(minutes=10))):
                            good = False

                    # if we have not seen the message before then process it
                    if good:
                        self.hashMsgs[hash_key] = timestamp
                        (srcip, srcport, dstip, dstport,
                         data) = msg.split(':', 4)
                        if (dstip == self.ip) and (int(dstport)
                                                   == self.meshPort):
                            #process msg
                            sys.stdout.write(data)
                            sys.stdout.write("\r\n")
                            displayPrompt = True
                        else:
                            # the server does not forward messages
                            None
                elif (sock == server_sock
                      ):  # a new connection request received
                    nid = nid + 1
                    sockfd, addr = sock.accept()
                    slist.append(sockfd)
                    self.neighbors[self.nodeCount] = Node(
                        addr[0], addr[1], self.genUID(), 1, sockfd, "Direct")
                    self.nodeCount += 1
                    sys.stdout.write("\r" +
                                     "Client %i : (%s, %s) connected\n" %
                                     (nid, addr[0], addr[1]))
                    displayPrompt = True
                elif (sock == sys.stdin):  # server sending message
                    msg = sys.stdin.readline()
                    msg = msg.lstrip('\r\n')
                    msg = msg.rstrip('\r\n')

                    displayPrompt = self.server_process_cmds(
                        slist, [server_sock, self.meshListener], msg,
                        server_sock)

                elif (sock != server_sock) and (sock != sys.stdin):
                    msg = Comms.readMsg(sock, 4096)
                    sys.stdout.write("=====================")
                    sys.stdout.write("\r\n")
                    (ip, port) = sock.getpeername()
                    sys.stdout.write(ip + ":" + str(port))
                    sys.stdout.write("\r\n")
                    sys.stdout.write("---------------------")
                    sys.stdout.write("\r\n")
                    sys.stdout.write(msg)
                    sys.stdout.write("\r\n")
                    displayPrompt = True
                else:
                    sys.stdout.write("[UNKNOWN SOCKET]")
                    sys.stdout.write("\r\n")
                    displayPrompt = True

            if (displayPrompt):
                sys.stdout.write(prompt)
                sys.stdout.flush()