Пример #1
0
 def __init__(self, port):
     self.stopped = False
     self.host = ""
     self.port = port
     #
     self.users = dict()
     self.userLock = threading.Lock()
     self.connections = dict()
     self.conLock = threading.Lock()
     #
     for i in range(180): # try 3 minutes to bind the socket
         try:
             self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
             self.sock.bind((self.host, self.port)) # bind socket to localhost:port
             dprint("Port is set to: " + str(self.port))
             dprint("Address bound successfully.")
             break
         except OSError:
             if i == 0: lprint("Address not available yet, waiting...")
             self.sock.close()
             print("\rTime passed: " + str(i+1) + "s", end="", flush=True)
             time.sleep(1)
     else:
         sys.exit(2) # could not bind to address
     #
     self.msgcenter_thread = MSGCenterThread(self)
     self.manage_thread = ManageThread(self)
     self.accept_thread = AcceptThread(self.sock, self)
     self.console_thread = ConsoleThread(self)
     lprint("Server started successfully on port " + str(self.port))
Пример #2
0
 def addToQueue(self, message, sender_id):
     self.msgLock.acquire()
     dprint("<" + self.name + "> " + str(sender_id) + " sent '" + message +
            "'")
     self.msgQueue.append(
         Message(message, [], sender_id,
                 self.parentServer.getUsername(sender_id)))
     self.msgLock.release()
Пример #3
0
 def addToQueue(self, message, sender_id):
     self.msgLock.acquire()
     ids = self.parentServer.getIDs()
     if sender_id in ids:
         ids.remove(sender_id)
     dprint("<" + self.name + "> " + str(sender_id) + " sent '" + message + "' to " + str(ids))
     self.msgQueue.append(Message(message, ids, sender_id, self.parentServer.getUsername(sender_id)))
     self.msgLock.release()
Пример #4
0
 def addToQueue(self, message, sender_id):
     self.msgLock.acquire()
     ids = self.parentServer.getIDs()
     if sender_id in ids:
         ids.remove(sender_id)
     dprint("<" + self.name + "> " + str(sender_id) + " sent '" + message +
            "' to " + str(ids))
     self.msgQueue.append(
         Message(message, ids, sender_id,
                 self.parentServer.getUsername(sender_id)))
     self.msgLock.release()
Пример #5
0
 def run(self):
     while not self.shouldStop:
         self.msgLock.acquire()
         if len(self.msgQueue) > 0:
             message = self.msgQueue.pop(0)
             dprint("<" + self.name + "> " + repr(message))
             if message.text.startswith("[cur]"):
                 new_username = message.text[5:]
                 dprint("<" + self.name + "> Client " +
                        str(message.sender_id) + " wants to be called: '" +
                        new_username + "'")
                 if self.parentServer.usernameExists(new_username):
                     self.parentServer.sendToClient(
                         "[cun]" + new_username + " exists",
                         message.sender_id)
                     dprint("<" + self.name + "> New username '" +
                            new_username + "' exists")
                 else:
                     self.parentServer.renameClient(new_username,
                                                    message.sender_id)
                     self.parentServer.sendToClient("[cua]" + new_username,
                                                    message.sender_id)
                     dprint("<" + self.name + "> New username '" +
                            new_username + "' was set")
         self.msgLock.release()
         time.sleep(0.2)
     self.shutdownMsg()
Пример #6
0
 def run(self):
     while not self.shouldStop:
         stdinput = sys.stdin.readline()  # blocking call
         command = stdinput.strip()
         if command != "":
             dprint("<Console> Echoing input: " + command)
         if command.lower() in ["h", "help", "?"]:
             lprint(
                 "<Console> Available commands: stop/q, ls/list, kickid, " +
                 "broadcast, send..to")
         elif command.lower() in ["stop", "q"]:
             self.parentServer.stop()
             break
         elif command.lower() in ["ls", "list"]:
             self.parentServer.ls()
         elif command.lower().startswith("kickid "):
             try:
                 cid = int(command[7:].strip())
             except ValueError:
                 lprint("Invalid id for kickid command: '" + cid + "'")
                 continue
             if cid in self.parentServer.connections:
                 self.parentServer.closeConnection(cid)
             else:
                 lprint("<Console> ID '" + str(cid) +
                        "' is not present on this server.",
                        mtype=1)
         elif command.lower().startswith("broadcast "):
             message = command[10:]
             self.parentServer.broadcast("[Server] " + message)
             lprint("<Console> Broadcast done.")
         elif re.search('send .+ to ', command):
             message = re.search("send .* to", command).group().replace(
                 "send ", "", 1).replace(" to", "", 1)
             recipient = re.search('to .*',
                                   command).group().replace("to ", "", 1)
             dprint("<Console> message='" + message + "', recipient='" +
                    recipient + "'")
             try:
                 recipient = int(recipient)
                 self.parentServer.sendToClient("[Server] " + message,
                                                recipient)
                 lprint("<Console> Message sent to client.")
             except ValueError:
                 lprint("<Console> Invalid recipient.")
         else:
             lprint("Don't know what to do with: '" + command + "'",
                    mtype=3)
     self.shutdownMsg()
Пример #7
0
 def run(self):
     while not self.shouldStop:
         stdinput = sys.stdin.readline() # blocking call
         command = stdinput.strip()
         if command != "":
             dprint("<Console> Echoing input: " + command)
         if command.lower() in ["h", "help", "?"]:
             lprint("<Console> Available commands: stop/q, ls/list, kickid, " +
                    "broadcast, send..to")
         elif command.lower() in ["stop", "q"]:
             self.parentServer.stop()
             break
         elif command.lower() in ["ls", "list"]:
             self.parentServer.ls()
         elif command.lower().startswith("kickid "):
             try: cid = int(command[7:].strip())
             except ValueError:
                 lprint("Invalid id for kickid command: '" + cid + "'")
                 continue
             if cid in self.parentServer.connections:
                 self.parentServer.closeConnection(cid)
             else: lprint("<Console> ID '" + str(cid) + "' is not present on this server.", mtype=1)
         elif command.lower().startswith("broadcast "):
             message = command[10:]
             self.parentServer.broadcast("[Server] " + message)
             lprint("<Console> Broadcast done.")
         elif re.search('send .+ to ', command):
             message = re.search("send .* to", command).group().replace("send ", "", 1).replace(" to", "", 1)
             recipient = re.search('to .*', command).group().replace("to ", "", 1)
             dprint("<Console> message='" + message + "', recipient='" + recipient + "'")
             try:
                 recipient = int(recipient)
                 self.parentServer.sendToClient("[Server] " + message, recipient)
                 lprint("<Console> Message sent to client.")
             except ValueError: lprint("<Console> Invalid recipient.")
         else:
             lprint("Don't know what to do with: '" + command + "'", mtype=3)
     self.shutdownMsg()
Пример #8
0
 def run(self):
     while not self.shouldStop:
         self.msgLock.acquire()
         if len(self.msgQueue) > 0:
             message = self.msgQueue.pop(0)
             dprint("<" + self.name + "> " + repr(message))
             if message.text.startswith("[cur]"):
                 new_username = message.text[5:]
                 dprint("<" + self.name + "> Client " + str(message.sender_id)
                      + " wants to be called: '" + new_username + "'")
                 if self.parentServer.usernameExists(new_username):
                     self.parentServer.sendToClient("[cun]" + new_username
                                                  + " exists", message.sender_id)
                     dprint("<" + self.name + "> New username '" + new_username + "' exists")
                 else:
                     self.parentServer.renameClient(new_username, message.sender_id)
                     self.parentServer.sendToClient("[cua]" + new_username, message.sender_id)
                     dprint("<" + self.name + "> New username '" + new_username + "' was set")
         self.msgLock.release()
         time.sleep(0.2)
     self.shutdownMsg()
Пример #9
0
 def shutdownMsg(self):
     dprint("<" + self.name + "> Shut down.")
Пример #10
0
 def alreadyShuttingDownMsg(self):
     dprint("<" + self.name + "> Thread is already shutting down...")
Пример #11
0
 def shutdownAttemptMsg(self):
     dprint("<" + self.name + "> Attempting to shut down...")
Пример #12
0
 def addToQueue(self, message, sender_id):
     self.msgLock.acquire()
     dprint("<" + self.name + "> " + str(sender_id) + " sent '" + message + "'")
     self.msgQueue.append(Message(message, [], sender_id, self.parentServer.getUsername(sender_id)))
     self.msgLock.release()