1 /*
2 Minetest
3 Copyright (C) 2017 nerzhul, Loic Blot <loic.blot@unix-experience.fr>
4 
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (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 Lesser General Public License for more details.
14 
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19 
20 #pragma once
21 
22 #include <unordered_map>
23 #include <string>
24 #include <vector>
25 #include <memory>
26 #include "network/networkprotocol.h"
27 #include "irrlichttypes.h"
28 
29 enum ModChannelState : u8
30 {
31 	MODCHANNEL_STATE_INIT,
32 	MODCHANNEL_STATE_READ_WRITE,
33 	MODCHANNEL_STATE_READ_ONLY,
34 	MODCHANNEL_STATE_MAX,
35 };
36 
37 class ModChannel
38 {
39 public:
ModChannel(const std::string & name)40 	ModChannel(const std::string &name) : m_name(name) {}
41 	~ModChannel() = default;
42 
getName()43 	const std::string &getName() const { return m_name; }
44 	bool registerConsumer(session_t peer_id);
45 	bool removeConsumer(session_t peer_id);
getChannelPeers()46 	const std::vector<u16> &getChannelPeers() const { return m_client_consumers; }
47 	bool canWrite() const;
48 	void setState(ModChannelState state);
49 
50 private:
51 	std::string m_name;
52 	ModChannelState m_state = MODCHANNEL_STATE_INIT;
53 	std::vector<u16> m_client_consumers;
54 };
55 
56 enum ModChannelSignal : u8
57 {
58 	MODCHANNEL_SIGNAL_JOIN_OK,
59 	MODCHANNEL_SIGNAL_JOIN_FAILURE,
60 	MODCHANNEL_SIGNAL_LEAVE_OK,
61 	MODCHANNEL_SIGNAL_LEAVE_FAILURE,
62 	MODCHANNEL_SIGNAL_CHANNEL_NOT_REGISTERED,
63 	MODCHANNEL_SIGNAL_SET_STATE,
64 };
65 
66 class ModChannelMgr
67 {
68 public:
69 	ModChannelMgr() = default;
70 	~ModChannelMgr() = default;
71 
72 	void registerChannel(const std::string &channel);
73 	bool setChannelState(const std::string &channel, ModChannelState state);
74 	bool joinChannel(const std::string &channel, session_t peer_id);
75 	bool leaveChannel(const std::string &channel, session_t peer_id);
76 	bool channelRegistered(const std::string &channel) const;
77 	ModChannel *getModChannel(const std::string &channel);
78 	/**
79 	 * This function check if a local mod can write on the channel
80 	 *
81 	 * @param channel
82 	 * @return true if write is allowed
83 	 */
84 	bool canWriteOnChannel(const std::string &channel) const;
85 	void leaveAllChannels(session_t peer_id);
86 	const std::vector<u16> &getChannelPeers(const std::string &channel) const;
87 
88 private:
89 	bool removeChannel(const std::string &channel);
90 
91 	std::unordered_map<std::string, std::unique_ptr<ModChannel>>
92 			m_registered_channels;
93 };
94