def init_socks(self):
     """Connect sockets with users whose id is less than me."""
     if self.group_leader_id is None:  # I start the chat, should send request to other users
         for user in self.other_user.values():
             sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
             sock.connect((user.ip, user.port))
             self.socks[user.user_id] = sock
             sock.setblocking(False)
             utils.send_msg(
                 sock,
                 utils.encode_message(self.name, self.me_user.port, 4,
                                      self.me_user.user_id, '', 0))
     else:  # should send response to those whose user_id is larger than me
         time.sleep(
             1)  # wait for everyone to first create a GroupChat and update
         for user in self.other_user.values():
             if self.me_user.user_id > user.user_id or user.user_id == self.group_leader_id:
                 continue
             sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
             sock.connect((user.ip, user.port))
             self.socks[user.user_id] = sock
             sock.setblocking(False)
             utils.send_msg(
                 sock,
                 utils.encode_message(self.name, self.me_user.port, 6,
                                      self.me_user.user_id, '', 0))
 def send_file_message(self, file_message, sock):
     """Send file message via client_sock.
     Split the file into different packages,
         first send 'type_id_filename', then send packages.
     """
     # send notification message
     utils.send_msg(
         sock,
         utils.encode_message(self.name, self.me_user.port, 2,
                              self.me_user.user_id, file_message.path,
                              file_message.size))
     # start send file packages
     file = open(file_message.path, 'rb')
     while True:
         package = file.read(config.MAX_PACKAGE_SIZE)
         if not package:  # file send over
             break
         while True:
             try:
                 sock.send(package)
                 break
             except BlockingIOError:
                 time.sleep(0.001)
     file.close()
     return DisplayMessage(
         file_message.sender_icon, file_message.sender_id, file_message.t,
         2, 'File {} send success!'.format(file_message.path))
 def send_log_out_message(self):
     """Delete other user as your friend, send a message to inform."""
     for sock in self.socks.values():
         utils.send_msg(
             sock,
             utils.encode_message(self.name, self.me_user.port, 8,
                                  self.me_user.user_id, '', 0))
 def send_text_message(self, text_message, sock):
     """Send text message via client_sock."""
     utils.send_msg(
         sock,
         utils.encode_message(self.name, self.me_user.port, 0,
                              self.me_user.user_id, text_message.text, 0))
     return DisplayMessage(text_message.sender_icon, text_message.sender_id,
                           text_message.t, 0, text_message.text)
Beispiel #5
0
	def boardcast(self, sender, message):
		offline_users = []
		
		for user in self.members:
			channel = core.Channel.create_from_user(user, _attr)
			if not channel.is_online:
				offline_users.append(user)
				continue
			channel.send_json(utils.encode_message(
					sender,
					consts.ATTR_DISCUSSION,
					message,
					discussion = self
			))
			
		return offline_users
    def __init__(self,
                 me_user,
                 other_user,
                 normal_chat_signals,
                 video_chat_signals,
                 sock=None):
        super(PrivateChat, self).__init__(me_user, other_user,
                                          normal_chat_signals)

        # signals for video chat
        self.video_request_signal = video_chat_signals[0]
        self.video_agree_signal = video_chat_signals[1]
        self.video_reject_signal = video_chat_signals[2]

        # generate name for the private chat
        # in the order of sort()
        all_id = [me_user.user_id, other_user.user_id]
        self.name = utils.get_chat_name_from_id(all_id)  # len == 21

        # setup socket
        if sock is None:  # I start the chat, should send a request
            self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            self.sock.connect((self.other_user.ip, self.other_user.port))
        else:
            self.sock = sock
        self.sock.setblocking(False)

        # if I start this chat, then I should send private chat request
        if sock is None:
            utils.send_msg(
                self.sock,
                utils.encode_message(self.name, self.me_user.port, 3,
                                     self.me_user.user_id, '', 0))
        # can only send one thing at one time
        self.is_sending = False

        # when receiving images or files, not check self.sock
        self.is_receiving = False
 def send_delete_message(self):
     """Delete other user as your friend, send a message to inform."""
     utils.send_msg(
         self.sock,
         utils.encode_message(self.name, self.me_user.port, 7,
                              self.me_user.user_id, '', 0))
 def send_video_chat_reject(self):
     """Send a message to inform video chat request."""
     utils.send_msg(
         self.sock,
         utils.encode_message(self.name, self.me_user.port, 10,
                              self.me_user.user_id, 'n', 0))
Beispiel #9
0
def send_message(sender, receiver, message):
	channel = core.Channel.create_from_user(receiver, consts.ATTR_CHAT)
	if not channel.is_online:
		return False
	channel.send_json(utils.encode_message(sender, consts.ATTR_CHAT, message))
	return True