-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanager.h
144 lines (122 loc) · 3.99 KB
/
manager.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
* File: manager.h
* Author: Dirk Vermeir
* Edited by: Wouter Van Rossem
*
* Created on July 22, 2009, 10:54 AM
*/
#ifndef _MANAGER_H
#define _MANAGER_H
#include <set>
#include <map>
#include <dvutil/debug.h>
#include <dvutil/props.h> // for config()
#include <dvthread/thread.h>
#include <dvthread/actor.h>
#include "player.h"
#include "maildrops.h"
/** The class that manages the maildrops. It communicates with
* the players via an Dv::Thread::Actor thread which itself
* uses a Dv::Thread::MailBox: the thread simply
* waits for messages to be delivered to its mailbox and
* then processes them using the Manager::operator() function.
*/
class Manager : public std::unary_function<Player::Message, std::string>,
public Player::Manager
{
public:
enum State
{
Authorization,
Transaction,
Update
};
/** The following is pure virtual in Player::Manager and
* the only thing a Player needs to know about the manager. */
void request (Player::Message m, Player::MailBox* mbox)
{
thread_.request(m, mbox);
}
/** Function called by the Actor thread associated with this Manager.
* The thread will read messages from its mailbox and then
* process them using this function.
* @param m message put by a player in the actor's mailbox.
* @return a string that will be put into the client's reply
* mailbox, if any.
* @exception std::runtime_error if the manager refuses
* for some reason to react to a request
*/
std::string operator()(const Player::Message& m) throw (std::runtime_error);
/** This function will return true after the manager (thread)
* has processed a 'shutdown' command.
* The main server program should check for this
* function in its main loop. If true, it can
* safely call Manager::kill.
* @return true iff the manager may be killed
* @see Manager::kill
*/
bool done () const
{
return done_;
}
/** This function will
* first kill all the players and then the manager thread.
* @warning this function cannot be called from the
* manager thread (otherwise, this would be suicide).
* @see Manager::done
*/
void kill ();
/** The configuration of this program.
* @return the configuration parameters as a Dv::Props object
*/
const Dv::Props& config ()
{
return config_;
}
/** Constructor.
* @param name of this manager
* @param config contains configuration parameters
* @param debug object which allows connected objects (e.g.
* threads) to write debug info
*/
Manager (const std::string& name, const Dv::Props& config, Dv::Debugable* debug = 0);
private:
Manager (const Manager&);
Manager & operator= (const Manager&);
/** Remove all references to a player from the manager's database
* and kill its thread.
* The function is robust: calling it twice will have no effect
* the second time. I.e. it is idempotent.
* @param player pointer to player object
*/
void remove_player (Player* player);
/** The set of active players, including nameless ones. */
Player::Set players_;
/** The set of active players that have 'root' status (via a
* successful 'su' command. */
Player::Set roots_;
/** A map that supports finding an active named player by name. */
Player::Map players_by_name_;
/** Type of the actor thread associated with Manager::manager. */
typedef Dv::Thread::Actor<Manager> Actor;
/** This is the manager's thread. */
Actor thread_;
/** Has the manager thread processed a shutdown command? */
bool done_;
/** The server configuration */
Dv::Props config_;
/** Return a pseudo-stream to write log info on.
* @param i debug level, the pseudo stream is real only
* if the actual debug level is at least @c i
* @return a pseudo stream to write on
*/
Dv::ostream_ptr& log (unsigned int i = 0)
{
return thread_.log(i);
}
/** The active maildrops */
Maildrops _maildrops;
/** A map of players and their current state */
std::map<Player*, State> _players_states;
};
#endif /* _MANAGER_H */