1 ////////////////////////////////////////////////////////////////////////////////
2 //    Scorched3D (c) 2000-2011
3 //
4 //    This file is part of Scorched3D.
5 //
6 //    Scorched3D is free software; you can redistribute it and/or modify
7 //    it under the terms of the GNU General Public License as published by
8 //    the Free Software Foundation; either version 2 of the License, or
9 //    (at your option) any later version.
10 //
11 //    Scorched3D is distributed in the hope that it will be useful,
12 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 //    GNU General Public License for more details.
15 //
16 //    You should have received a copy of the GNU General Public License along
17 //    with this program; if not, write to the Free Software Foundation, Inc.,
18 //    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 ////////////////////////////////////////////////////////////////////////////////
20 
21 #if !defined(__INCLUDE_ServerChannelManagerh_INCLUDE__)
22 #define __INCLUDE_ServerChannelManagerh_INCLUDE__
23 
24 #include <coms/ComsMessageHandler.h>
25 #include <server/ServerChannelFilter.h>
26 #include <server/ServerChannelAuth.h>
27 #include <time.h>
28 #include <set>
29 #include <map>
30 
31 class ServerChannelManager
32 {
33 public:
34 	ServerChannelManager(ComsMessageHandler &comsMessageHandler);
35 	virtual ~ServerChannelManager();
36 
37 	struct MessageEntry
38 	{
39 		std::string message;
40 		unsigned int messageid;
41 	};
42 
43 	void sendText(const ChannelText &text, bool serverLog, bool filter = true);
44 	void sendText(const ChannelText &text, unsigned int destination, bool serverLog, bool filter = true);
getLastMessages()45 	std::list<MessageEntry> &getLastMessages() { return lastMessages_; }
46 	std::list<std::string> getAllChannels();
47 
48 	void simulate(fixed frameTime);
49 
50 	// Notification of when players disconnect
51 	void destinationDisconnected(unsigned int destinationId);
52 	void refreshDestination(unsigned int destinationId);
53 
54 protected:
55 	static ServerChannelManager *instance_;
56 
57 	class DestinationLocalEntry
58 	{
59 	public:
60 		DestinationLocalEntry(unsigned int localId = 0);
61 
getLocalId()62 		unsigned int getLocalId() { return localId_; }
getChannels()63 		std::set<std::string> &getChannels() { return channels_; }
getAvailableChannels()64 		std::set<std::string> &getAvailableChannels() { return availableChannels_; }
65 
66 	protected:
67 		unsigned int localId_;
68 		std::set<std::string> channels_; // Subscribed channels
69 		std::set<std::string> availableChannels_; // Available channeld
70 	};
71 	class DestinationEntry
72 	{
73 	public:
74 		DestinationEntry(unsigned int destinationId);
75 
getDestinationId()76 		unsigned int getDestinationId() { return destinationId_; }
77 
78 		bool hasChannel(const std::string &channel);
79 		void addChannel(const std::string &channel, unsigned int localId, bool current);
80 		void removeChannel(const std::string &channel, unsigned int localId);
81 
82 		bool hasLocalId(unsigned int localId);
83 		void addLocalId(unsigned int localId);
84 		void removeLocalId(unsigned int localId);
85 
setMessageCount(unsigned int count)86 		void setMessageCount(unsigned int count) { messageCount_ = count; }
getMessageCount()87 		unsigned int getMessageCount() { return messageCount_; }
88 
getMuteTime()89 		time_t getMuteTime() { return muteTime_; }
setMuteTime(time_t t)90 		void setMuteTime(time_t t) { muteTime_ = t; }
91 
92 		void getLocalIds(const std::string &channel, std::list<unsigned int> &ids);
getLocalEntries()93 		std::map<unsigned int, DestinationLocalEntry> &getLocalEntries() { return localEntries_; }
94 
95 	protected:
96 		time_t muteTime_;
97 		unsigned int messageCount_;
98 		unsigned int destinationId_;
99 		std::set<std::string> channels_; // Subscribed channeld for all localids
100 		std::map<unsigned int, DestinationLocalEntry> localEntries_;
101 
102 		void updateChannels();
103 	};
104 	class ChannelEntry
105 	{
106 	public:
107 		ChannelEntry(ChannelDefinition def,
108 			ServerChannelFilter *filter = 0,
109 			ServerChannelAuth *auth = 0);
110 		virtual ~ChannelEntry();
111 
getName()112 		const char *getName() { return channelDef_.getChannel(); }
getDefinition()113 		ChannelDefinition &getDefinition() { return channelDef_; }
getFilter()114 		ServerChannelFilter *getFilter() { return filter_; }
getAuth()115 		ServerChannelAuth *getAuth() { return auth_; }
116 
117 	protected:
118 		ChannelDefinition channelDef_;
119 		ServerChannelFilter *filter_;
120 		ServerChannelAuth *auth_;
121 	};
122 
123 	unsigned int lastMessageId_;
124 	fixed totalTime_;
125 	std::map<unsigned int, DestinationEntry *> destinationEntries_;
126 	std::list<ChannelEntry *> channelEntries_;
127 	std::list<MessageEntry> lastMessages_;
128 	ComsMessageHandlerI *handler1_, *handler2_;
129 
130 	ChannelEntry *getChannelEntryByName(const std::string &name);
131 	DestinationEntry *getDestinationEntryById(unsigned int destinationId);
132 
133 	void registerClient(unsigned int destinationId, unsigned int localId,
134 		std::list<ChannelDefinition> &startChannels);
135 	void deregisterClient(unsigned int destinationId, unsigned int localId);
136 	void joinClient(unsigned int destinationId, unsigned int localId,
137 		std::list<ChannelDefinition> &startChannels);
138 	void actualSend(const ChannelText &constText,
139 		std::map<unsigned int, DestinationEntry *> &destinations,
140 		bool serverLog, bool filter);
141 
142 	bool processChannelMessage(NetMessage &message, NetBufferReader &reader);
143 	bool processChannelTextMessage(NetMessage &message, NetBufferReader &reader);
144 
145 };
146 
147 #endif
148