示例#1
0
 async def read_message(self):
     message = await self._reader.readline()
     if not message:
         return None, None
     message_type, parsed_message = protocol.load(message.decode("utf8"))
     if message_type:  # type is "message"
         parsed_message["nick"] = self.nick
     return message_type, parsed_message
示例#2
0
 async def handle_message(self):
     while True:
         try:
             message_type, message = protocol.load((await self._reader.readline()).decode("utf8"))
         except protocol.ProtocolException:
             print("Connection broken. Press Ctrl-C to exit")
             return
         if message_type == None: # connection is break
             return
         elif message_type: # message_type is "message"
             self.message_handler(message)
         else: # message_type is "service"
             self.service_handler(message)
示例#3
0
 async def get_client(self, reader, writer):
     user_data = (await reader.readline()).decode("utf8")
     _type, data = protocol.load(user_data)
     try:
         if not re.match(r"^[A-Za-z0-9]+$", data["nick"]):
             return None
         client = user.User(data, reader, writer)
     except KeyError:
         writer.write(
             protocol.dump(
                 protocol.new_status(
                     418,
                     'Registration request should contains "nick" field')).
             encode("utf8"))
     return client
示例#4
0
 async def registration(self):
     error = ""
     while error != None:
         print("Enter your nick: ", end='')
         sys.stdout.flush()
         self._nick = await self.ainput()
         self._writer.write(protocol.dump(protocol.form_service({"nick": self._nick})).encode("utf8"))
         self._writer.write(b'\n')
         
         nick_response = await self._reader.readline()
         _type, response = protocol.load(nick_response.decode("utf8"))
         error = protocol.check_error(response)
         if error != None:
             print(protocol.check_error(response))
     print("Welcome to chat, {}!\n".format(self._nick))
     handle_stdin_task = self._loop.create_task(self.handle_stdin())
     handle_message_task = self._loop.create_task(self.handle_message())
示例#5
0
 def auth(self):
     # this function should run in case we have known the ip and don't know other info
     self.prepare_sock()
     self.sock.send(b"headcall.device.lookup {type:UPS;}")
     ret = self.sock.recv(1024)
     answer = protocol.load(ret.split(b" ")[1])
     self.uid = answer["uid"]
     self.logger.info("Got Echo from {}:{}".format(self.ip, self.uid))
     payload = {
         "hello": True,
         "ctag": self.get_ctag(),
         "cseq": self.get_cseq(),
         "cmagic": "magic" + self.get_ctag(),
         "to": self.uid
     }
     ret = self.send_payload(payload)
     if '200' in ret["ack"]:
         self.stag = ret["stag"]
示例#6
0
 def auth(self):
     # this function should run in case we have known the ip and don't know other info
     self.prepare_sock()
     self.sock.send(b"headcall.device.lookup {type:UPS;}")
     ret = self.sock.recv(1024)
     answer = protocol.load(ret.split(b" ")[1])
     self.uid = answer["uid"]
     self.logger.info("Got Echo from {}:{}".format(self.ip, self.uid))
     payload = {
         "hello": True,
         "ctag": self.get_ctag(),
         "cseq": self.get_cseq(),
         "cmagic": "magic" + self.get_ctag(),
         "to": self.uid,
     }
     ret = self.send_payload(payload)
     if "200" in ret["ack"]:
         self.stag = ret["stag"]
示例#7
0
 def send_payload(self, data, noack=False):
     self.prepare_sock()
     encoded_payload = protocol.dump(data)
     tried = 0
     while tried < 3:
         try:
             tried += 1
             self.sock.send(encoded_payload)
             self.logger.debug("Sendto {}: {}".format(self.ip, encoded_payload))
             if noack:
                 return
             recv = self.sock.recv(1024)
             self.logger.debug("Recv   {}: {}".format(self.ip, recv))
             answer = protocol.load(recv)
             # TODO: check status
             return answer
         except Exception as e:
             print(e)
             # self.connect()  # FIXME
             continue
     raise Exception("Fail to connect to server")
示例#8
0
 def send_payload(self, data, noack=False):
     self.prepare_sock()
     encoded_payload = protocol.dump(data)
     tried = 0
     while tried < 3:
         try:
             tried += 1
             self.sock.send(encoded_payload)
             self.logger.debug("Sendto {}: {}".format(
                 self.ip, encoded_payload))
             if noack:
                 return
             recv = self.sock.recv(1024)
             self.logger.debug("Recv   {}: {}".format(self.ip, recv))
             answer = protocol.load(recv)
             # TODO: check status
             return answer
         except Exception as e:
             print(e)
             # self.connect()  # FIXME
             continue
     raise Exception("Fail to connect to server")
示例#9
0
 async def connect(self, host, port):
     self._reader, self._writer = await asyncio.open_connection(host, port, loop=self._loop)
     _type, response = protocol.load((await self._reader.readline()).decode("utf8"))
     if not response["status"]:
         print("Server refused connection with: {}".format(response["error"]))
     await self.registration()