1 /*****************************************************************************
2  * PokerTH - The open source texas holdem engine                             *
3  * Copyright (C) 2006-2012 Felix Hammer, Florian Thauer, Lothar May          *
4  *                                                                           *
5  * This program is free software: you can redistribute it and/or modify      *
6  * it under the terms of the GNU Affero General Public License as            *
7  * published by the Free Software Foundation, either version 3 of the        *
8  * License, or (at your option) any later version.                           *
9  *                                                                           *
10  * This program is distributed in the hope that it will be useful,           *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of            *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
13  * GNU Affero General Public License for more details.                       *
14  *                                                                           *
15  * You should have received a copy of the GNU Affero General Public License  *
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.     *
17  *                                                                           *
18  *                                                                           *
19  * Additional permission under GNU AGPL version 3 section 7                  *
20  *                                                                           *
21  * If you modify this program, or any covered work, by linking or            *
22  * combining it with the OpenSSL project's OpenSSL library (or a             *
23  * modified version of that library), containing parts covered by the        *
24  * terms of the OpenSSL or SSLeay licenses, the authors of PokerTH           *
25  * (Felix Hammer, Florian Thauer, Lothar May) grant you additional           *
26  * permission to convey the resulting work.                                  *
27  * Corresponding Source for a non-source form of such a combination          *
28  * shall include the source code for the parts of OpenSSL used as well       *
29  * as that of the covered work.                                              *
30  *****************************************************************************/
31 /* Thread safe server session manager. */
32 
33 #ifndef _SESSIONMANAGER_H_
34 #define _SESSIONMANAGER_H_
35 
36 #include <boost/function.hpp>
37 #include <map>
38 
39 #include <net/sessiondata.h>
40 #include <playerdata.h>
41 #include <gamedata.h>
42 #include <core/thread.h>
43 
44 class NetPacket;
45 class SenderHelper;
46 
47 class SessionManager
48 {
49 public:
50 	SessionManager();
51 	virtual ~SessionManager();
52 
53 	void AddSession(boost::shared_ptr<SessionData> sessionData);
54 	void SetSessionPlayerData(SessionId session, boost::shared_ptr<PlayerData> playerData);
55 	bool RemoveSession(SessionId session);
56 
57 	boost::shared_ptr<SessionData> GetSessionById(SessionId id) const;
58 	boost::shared_ptr<SessionData> GetSessionByPlayerName(const std::string &playerName) const;
59 	boost::shared_ptr<SessionData> GetSessionByUniquePlayerId(unsigned uniqueId, bool initSessions = false) const;
60 
61 	PlayerDataList GetPlayerDataList() const;
62 	PlayerDataList GetSpectatorDataList() const;
63 	PlayerIdList GetPlayerIdList(int state) const;
64 	bool IsPlayerConnected(const std::string &playerName) const;
65 	bool IsPlayerConnected(unsigned uniqueId) const;
66 	bool IsClientAddressConnected(const std::string &clientAddress) const;
67 	bool IsGuestAllowedToConnect(const std::string &clientAddress) const;
68 
69 	void ForEach(boost::function<void (boost::shared_ptr<SessionData>)> func);
70 
71 	unsigned CountReadySessions() const;
72 	void ResetAllReadyFlags();
73 
74 	void Clear();
75 	unsigned GetRawSessionCount() const;
76 	unsigned GetSessionCountWithState(int state) const;
77 	bool HasSessionWithState(int state) const;
78 
79 	void SendToAllSessions(SenderHelper &sender, boost::shared_ptr<NetPacket> packet, int state);
80 	void SendLobbyMsgToAllSessions(SenderHelper &sender, boost::shared_ptr<NetPacket> packet, int state);
81 	void SendToAllButOneSessions(SenderHelper &sender, boost::shared_ptr<NetPacket> packet, SessionId except, int state);
82 
83 protected:
84 
85 	typedef std::map<SessionId, boost::shared_ptr<SessionData> > SessionMap;
86 
87 private:
88 
89 	SessionMap m_sessionMap;
90 	mutable boost::recursive_mutex m_sessionMapMutex;
91 };
92 
93 #endif
94