void Dispatcher::openSession(TCPSocket* peer, const string& data){
    if (isLoggedIn(peer)){
        if (loggedInUsers.find(data) != loggedInUsers.end()) {
            TCPSocket *peerB = loggedInUsers[data]; // find second peer according to the data

            //if peer was found and client did not open session with himself then allow session
            if ((peerB != NULL && peer != peerB) && !(std::find(peers.begin(),peers.end(),peerB) == peers.end()))
            {
                //get the username from the peers
                string peerName, peerBName;
                for (map<string, TCPSocket *>::iterator itr = loggedInUsers.begin();
                     itr != loggedInUsers.end(); ++itr) {
                    if (itr->second == peer) {
                        peerName = itr->first;
                    }
                    if (itr->second == peerB) {
                        peerBName = itr->first;
                    }
                }
                //tell peerB to change sessionIsActive=true
                TCPMessengerProtocol::sendToServer(SESSION_ESTABLISHED,
                                                   peerBName + " " + peerB->fromAddr(), peer);
                TCPMessengerProtocol::sendToServer(SESSION_ESTABLISHED,
                                                   peerName + " " + peer->fromAddr(), peerB);
                //remove peers from the dispatcher responsibility
                this->removePeer(peer);
                this->removePeer(peerB);
                //give responsibility of the peers to a new brocker
                Brocker *broker = new Brocker(this, peer, peerB, peerName, peerBName);
                //keep reference of brocker in brockers vector
                brockers.push_back(broker);
            }
            else {
                //get peer name
                string peerName;
                for (map<string, TCPSocket *>::iterator itr = loggedInUsers.begin();
                     itr != loggedInUsers.end(); ++itr) {
                    if (itr->second == peer) {
                        peerName = itr->first;
                    }
                }
                //if peer does not exist in peers list - refuse the session
                TCPMessengerProtocol::sendToServer(SESSION_REFUSED, peerName, peer);
            }
        }
    }
}
void Dispatcher::run(){
    int command;
    string data;
	while(running){
			if(peers.size() == 0){
				running = false;
				break;
			}
			TCPSocket* peer = listener->listen(TIMEOUT);
			if(peer!=NULL){
				command = 0;
				data.clear();
				TCPMessengerProtocol::readFromServer(command, data, peer);
				//cout<<"read command from peer: "<< command << " " << data << endl;
                switch(command){
                    case LOGIN: {
                        login(peer,data);
                        break;
                    }
                    case REGISTER:{
                        reg(peer,data);
                        break;
                    }

                    case OPEN_OR_CONNECT_TO_ROOM: {
                        openOrConnectRoom(peer,data);
                        break;
                    }
                    case OPEN_SESSION_WITH_PEER: {
                        openSession(peer,data);
                        break;
                    }
                    case LIST_USERS:{
                        listUsers(peer,data);
                        break;
                    }
                    case LIST_CONNECTED_USERS:{
                        listConnectedUsers(peer,data);
                        break;
                    }
                    case LIST_ROOMS:{
                        listRooms(peer,data);
                        break;
                    }
                    case LIST_ROOM_USERS:{
                        listRoomUsers(peer,data);
                        break;
                    }
                    case EXIT: {
                        exit(peer,data);
                        break;
                    }
                    default: {
                        cout << BOLDRED<< "Problems with client: " << peer->fromAddr() <<". disconnecting"<< RESET<<endl;
                        removePeer(peer);
                        for (map<string, TCPSocket *>::iterator itr = loggedInUsers.begin();
                             itr != loggedInUsers.end(); ++itr){
                            if(itr->second==peer){
                                loggedInUsers.erase(itr->first);
                            }
                        }
                        break;
                    }
                }

            }
    }
}