Esempio n. 1
0
void MessengerClient::run() {
	_running = true;
	while (_running) {
		int command = MessengerClient::readCommandFromPeer(_serverSocket);
		if (command == 0) {
			continue;
		}

		string parameter1;
		string name;
		string ip;
		int port;
		int partnerPort;

		switch (command) {
		case SESSION_ESTABLISHED:
			name = MessengerClient::readDataFromPeer(_serverSocket);
			ip = MessengerClient::readDataFromPeer(_serverSocket);
			partnerPort = MessengerClient::readCommandFromPeer(_serverSocket);

			_sessionPeer.setName(name);
			_sessionPeer.setIp(ip);
			_sessionPeer.setPort(partnerPort);

			port = MessengerClient::readCommandFromPeer(_serverSocket);
			_clientLinker = new ClientLinker(port);
			_inSession = true;

			cout << "You are in direct connection with  " << _sessionPeer.getName() << endl;
			break;
		case SESSION_CREATE_REFUSED:
			parameter1 = MessengerClient::readDataFromPeer(_serverSocket);
			cout << "Session has been denied because " << parameter1 << endl;
			break;
		case SESSION_CLOSED:
			_sessionPeer.reset();
			_inSession = false;

			delete (_clientLinker);
			_clientLinker = NULL;

			cout << "Session is now terminated" << endl;
			break;
		case CHAT_ROOM_CREATE_DENIED:
			parameter1 = MessengerClient::readDataFromPeer(_serverSocket);
			cout << "Room cannot be opened because " << parameter1 << endl;
			break;
		case CHAT_ROOM_CREATED:
			cout << "Chat room has been opened" << endl;
			break;
		case CHAT_ROOM_USER_LEFT:
			chatRoomLeft();
			cout << "You left the chat room" << endl;
			break;
		case CHAT_ROOM_USER_ENTERED:
			port = MessengerClient::readCommandFromPeer(_serverSocket); // UDP listen port
			_clientLinker = new ClientLinker(port);
			_inChatRoom = true;

			cout << "You have joined to the room" << endl;
			break;
		case CHAT_ROOM_ENTERING_DENIED:
			cout << MessengerClient::readDataFromPeer(_serverSocket) << endl;
			break;
		case CHAT_ROOM_UPDATED:
			chatRoomUpdate();
			break;
		case CHAT_ROOM_CLOSED:
			cout << "The chat room has been closed" << endl;
			break;
		case CHAT_ROOM_UNCLOSED:
			cout << "You cannot delete the chat room" << endl;
			break;
		case CHAT_ROOM_NOT_EXIST:
			cout << "Chat room does not exist" << endl;
			break;
		case LIST_CONNECTED_USERS:
			printConnectedUsers();
			break;
		case LIST_CHAT_ROOMS:
			printRoomsList();
			break;
		case LIST_CONNECTED_USERS_IN_CHAT_ROOM:
			printConnectedUsers(); // Fits to our need
			break;
		case LIST_USERS:
			printListUsers();
			break;
		default:
			cout << "Unrecognized command received from server" << endl;
			break;
		}
	}
}
Esempio n. 2
0
// Client receive loop, ran once a login to the server is created
void ClientManager::run()
{
	string parameter1;
	int partnerPort;
	int command;
	running = true;

	while (running)
	{
		command = serverSocket->readCommand();
		if (!command)
			continue;
		switch(command)
		{
			case SESSION_ESTABLISHED:
				udpChatSideB._sideB_name = serverSocket->readMsg();
				udpChatSideB._IP = serverSocket->readMsg();
				udpChatSideB._port = serverSocket->readCommand();

				partnerPort = serverSocket->readCommand();
				clientLinker = new ClientLinker(partnerPort);
				sessionOn = true;

				cout<<"You are connected to "<<udpChatSideB._sideB_name<<endl;
				break;
			case SESSION_CREATE_REFUSED:
				parameter1 = serverSocket->readMsg();
				cout<<"Can't create session because "<<parameter1 <<endl;
				break;
			case SESSION_CLOSED:
				udpChatSideB.Clean();
				sessionOn = false;
				delete(clientLinker);
				cout<<"the session is now terminated"<<endl;
				break;
			case CHAT_ROOM_CREATE_DENIED:
				parameter1 = serverSocket->readMsg();
				cout << "Room cannot be opened because " << parameter1 << endl;
				break;
			case CHAT_ROOM_CREATED:
				cout << "Chat room has been created" << endl;
				break;
			case CHAT_ROOM_LEAVED:
				chatRoomLeaved();
				cout<<"You left the chat room"<<endl;
				break;
			case CHAT_ROOM_LOGED_IN:
				partnerPort = serverSocket->readCommand(); // UDP listen port
				clientLinker = new ClientLinker(partnerPort);
				roomOn = true;
				cout << "You have joined the chat room" << endl;
				break;
			case CHAT_ROOM_LOGED_IN_DENIED:
				cout << serverSocket->readMsg() << endl;
				break;
			case CHAT_ROOM_UPDATED:
				chatRoomUpdate();
				break;
			case CHAT_ROOM_CLOSED:
				cout<<"The chat room was closed"<<endl;
				break;
			case CHAT_ROOM_UNCLOSED:
				cout<<"You cannot delete this chat room"<<endl;
				break;
			case LIST_CONNECTED_USERS:
				printConnectedUsers();
				break;
			case LIST_ROOMS:
				printRoomsList();
				break;
			case LIST_CONNECTED_USERS_IN_ROOM:
				printConnectedUsers();
				break;
			case LIST_USERS:
				printListUsers();
		}
	} // End command interface

}