def __init__(self, host, port, thread_pool_size=20, buffer_size=150): self.host = host self.port = port self.thread_pool_size = thread_pool_size self.buffer_size = buffer_size self.listener = None self.thread_pool = [] self.chat_rooms = { 'room1': ChatRoom('room1', 1, self.host, self.port), 'room2': ChatRoom('room2', 1, self.host, self.port) }
class Oper (object): def __init__(self): self.clients = [] self.channels = ChatRoom("Master") def add_client(self, client): if client: self.clients.append(client) self.channels.add_client(client) return True else: return False def remove_client(self,client): if client in self.clients: self.clients.remove(client) self.channels.remove_client(client) return True return False def num_of_clients(self): return len(self.clients) def get_clients(self): return self.clients def get_usernames(self): names = [] for client_it in self.clients: names.append(client_it.get_username()) return names def get_room(self): return self.channels def set_client_color(self,client,color): if client in self.clients: client.set_text_color(color) def set_client_beep(self,client,status): status = True if status == "ON" else False if client in self.clients: client.set_beep(status) def kill_client(self,client): if client: client.commit_suicide() def user_join_msg(self,username): self.channels.send_all("",Utils.color_system_msg("<"+username+"> joined the room")) def user_left_msg(self,username): self.channels.send_all("",Utils.color_system_msg("<"+username+"> left the room")) def username_exists(self,username): for client_it in self.clients: if client_it.get_username() == username: return True return False
def f(): chat_room = self.get_chat_room_by_name(chat_room_name) if not chat_room: chat_room = ChatRoom(chat_room_name) chat_room_id = chat_room.id self.chat_rooms[chat_room_id] = chat_room return chat_room
def test_three_chat_clients(): print() expected = [ ChatMessage('client1', 'message 1'), ChatMessage('client2', 'message 2'), ChatMessage('client3', 'message 3'), ChatMessage('client3', 'message 4'), ChatMessage('client2', 'message 5'), ChatMessage('client1', 'message 6'), ] chat_host = ChatRoom() chat_client1 = ChatClient('client1', chat_host) chat_client2 = ChatClient('client2', chat_host) chat_client3 = ChatClient('client3', chat_host) chat_client1.new_message('message 1') chat_client2.new_message('message 2') chat_client3.new_message('message 3') chat_client3.new_message('message 4') chat_client2.new_message('message 5') chat_client1.new_message('message 6') actual = chat_host.messages assert expected == actual
def __init__(self): tk.Tk.__init__(self) self._frame = None self.iconbitmap("images\icon.ico") self.switch_frame(StartPage) self.title("IOTA Almost-Instant Messenger") self.geometry("600x350") self._chat_room = ChatRoom()
class TestChatRoom(TestCase): def setUp(self): self.room = ChatRoom(RoomId, "room name", 10) def test_remove(self): """ Tests removing a registered client. """ client_id = ClientId() client_info = ClientInformation(client_id, message_id=PacketId(10)) self.room.register(client_id, client_info) self.assertDictEqual(self.room.clients, {client_id: client_info}) self.room.remove(client_id) self.assertDictEqual(self.room.clients, {}).__hash__() def test_remove_non_existent_client(self): """ Tests if the remove method raises a LookupError when trying to remove a client that does not exist in the room. """ with self.assertRaises(LookupError): self.room.remove(ClientId()) def test_iteration(self): """ Tests if the room iteration works correctly. When iterating through a room we have to iterate through the information of all clients. """ client_count = 5 client_ids = {} # create a set of client ids with a boolean flag to indicate if the client id # was visited during iteration for i in range(client_count): # initially all client ids are marked not visited client_ids[ClientId()] = False # register all client ids for client_id in client_ids.keys(): self.room.register(client_id, ClientInformation(client_id, message_id=PacketId(10))) # iterate through the room for client in self.room: # type: ClientInformation # verify if the client id was not already visited self.assertFalse(client_ids[client.client_id]) client_ids[client.client_id] = True # verify if all client ids were checked for checked in client_ids.values(): self.assertTrue(checked)
def _updateTotalMessagesByRoom(roomId): objChatRoom = ChatRoom() itmChatRoom = objChatRoom.getById(roomId) if itmChatRoom is None: objChatRoom.create({'room_id': 1, 'total_messages': 1}) else: itmChatRoom['total_messages'] = itmChatRoom['total_messages'] + 1 objChatRoom.update(itmChatRoom)
def main(): # TODO: create a ChatRoom instance and assign it to a variable named # `chat_host` chat_host = ChatRoom() # TODO: note the chat_host argument to the ChatClientGui constructor for # all three ChatClientGui instances # (no code change required) chat_client1 = ChatClientGui('Client 1', root, chat_host) chat_client2 = ChatClientGui('Client 2', chat_client1, chat_host) chat_client3 = ChatClientGui('Client 3', chat_client1, chat_host) root.mainloop()
def setUp(self): self.driver3 = webdriver.Chrome() self.threeuser = ChatRoom(self.driver3, user3, gpasswd, url, user2, groupname) options = webdriver.ChromeOptions() options.add_argument('disable-infobars') self.browser = webdriver.Chrome(chrome_options=options) self.twouser = ChatRoom(self.browser, user2, gpasswd, url, user1, groupname) driver1 = webdriver.Chrome() self.oneuser = ChatRoom(driver1, user1, gpasswd, url, user2, groupname)
import sys, os from socket import * from Queue import Queue from threading import Thread import collections from chat_room import ChatRoom chat_room = ChatRoom(sys.argv[1], int(sys.argv[2])) rooms = collections.OrderedDict() threads = [] activE = True threadLimit = 100 numb_of_threads = 0 hostname = sys.argv[1] port_number = int(sys.argv[2]) sock = socket(AF_INET, SOCK_STREAM) sock.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1) sock.bind((hostname, port_number)) sock.listen(10) def messages(connect): while activE: msg = connect.recv(2048) if msg[:13] == "JOIN_CHATROOM": print("joining chatroom") msgs = msg.split("\n") chatroom = msgs[0].split(":")[1] client_name = msgs[3].split(":")[1] numb = 0
def make_chat_room(self, address, decoder_key): # create a ChatRoom object with an address and key. self._chat_room = ChatRoom(address, decoder_key)
def setUp(self): self.room = ChatRoom(RoomId, "room name", 10)
class TestChatRoom(unittest.TestCase): def setUp(self): self.driver3 = webdriver.Chrome() self.threeuser = ChatRoom(self.driver3, user3, gpasswd, url, user2, groupname) options = webdriver.ChromeOptions() options.add_argument('disable-infobars') self.browser = webdriver.Chrome(chrome_options=options) self.twouser = ChatRoom(self.browser, user2, gpasswd, url, user1, groupname) driver1 = webdriver.Chrome() self.oneuser = ChatRoom(driver1, user1, gpasswd, url, user2, groupname) def testJoinChatroom_1(self): u'验证加入聊天室' global roomID self.oneuser.sign_in() self.twouser.sign_in() self.threeuser.sign_in() self.oneuser.login() self.twouser.login() roomID = createroom() #global roomID #result, roomID = self.oneuser.JoinChatroom() self.oneuser.JoinAssignRoom(roomID) self.twouser.JoinAssignRoom(roomID) self.threeuser.login() self.assertTrue(self.threeuser.JoinAssignRoom(roomID), True) def testSendImage_2(self): u'验证chatroom发送图片' self.oneuser.login() self.twouser.login() self.oneuser.JoinAssignRoom(roomID) self.twouser.JoinAssignRoom(roomID) self.oneuser.sendimage() self.assertTrue(self.twouser.receiveimage(), True) def testSendFile_3(self): u'验证chatroom发送文件' self.oneuser.login() self.twouser.login() self.oneuser.JoinAssignRoom(roomID) self.twouser.JoinAssignRoom(roomID) self.oneuser.sendfile() self.assertTrue(self.twouser.receivefile(), True) def testSdMultiMess_4(self): u'验证发送多条消息' self.twouser.login() self.threeuser.login() self.twouser.JoinAssignRoom(roomID) self.threeuser.JoinAssignRoom(roomID) self.twouser.sendroomMess(groupmess_num) self.assertTrue(self.threeuser.RoomMessNum(groupmess_num), True) def testCleanMessage_5(self): u'验证清除所有消息' self.oneuser.login() self.threeuser.login() self.oneuser.JoinAssignRoom(roomID) self.threeuser.JoinAssignRoom(roomID) self.oneuser.sendroomMess(groupmess_num) self.threeuser.RoomMessNum(groupmess_num) time.sleep(2) self.threeuser.cleanchat() self.assertFalse(self.threeuser.RoomMessNum(groupmess_num), False) delroom(roomID) def tearDown(self): self.oneuser.quitBrowser() self.threeuser.quitBrowser() self.twouser.quitBrowser() self.oneuser = None self.twouser = None self.threeuser = None
def __init__(self): self.clients = [] self.channels = ChatRoom("Master")