1 #ifndef REMOTE_MCS_SESSION_H
2 #define REMOTE_MCS_SESSION_H
6 #include "protocols/Message.h"
7 #include "protocols/MsgPayload.h"
8 #include "protocols/client_server/ClientMsg.h"
9 #include "protocols/client_server/MsgClientConfirm.h"
10 #include "protocols/client_server/MsgClientRequest.h"
11 #include "protocols/client_server/MsgSession.h"
13 #include "mcs/FileDescriptor.h"
15 #include "mcs/database.h"
17 namespace remote
{ namespace mcs
{
19 using namespace protocols
;
20 using namespace protocols::client_server
;
23 typedef std::map
<int,Session
*> sessionmapbyfd_t
;
25 /** Each instance of this class handles a single client connection.
27 class Session
: public FileDescriptor
30 /** Session constructor.
31 * \param p_fd File descriptor for the new client connection.
32 * \param p_sessionMap The client object will register itself in this map.
34 Session(int p_fd
,sessionmapbyfd_t
& p_sessionMap
);
35 /** Makes the session object release any controlled motes and the call its destructor.
36 * \param silent If true, any exceptions generated during clean-up will be caught an logged silently.
38 void destroy(bool silent
= false);
39 /** Sends a confirm message for a mote to the remote client.
40 * \param mote_id Database key of the mote
41 * \param confirm Confirm message to encapsulate
44 /** Clears all session records from the database **/
45 static void resetDb();
47 void confirm(dbkey_t mote_id
, MsgPayload
& confirm
);
48 /** Forces the session to drop control of a mote.
49 *\param mote_id Database key of the mote to free
51 void freeMote(dbkey_t mote_id
);
52 /** Database key for this sessions session record **/
55 /** Session object destructor
58 /** Handles an event on the socket of the remote client by invoking the appropriate event handler.
59 * \param events Event descriptor.
61 void handleEvent(short events
);
63 /** Creates a new session database record upon connection and sends it to the client.
64 * The session is not initially authenticated. The authentication is performed
65 * externally and registered on the record.
66 * Any client attempting to send messages to the server must authenticate before
67 * messages are accepted. Failure to do so will disconnect the client.
69 void createSessionRecord();
70 /** Deletes the session record upon end of session. **/
71 void deleteSessionRecord();
72 /** On the first invocation, performs a database check to see if the session
73 * is authenticated. Subsequent invocations only check the database if no
74 * positive result has been obtained yet.
76 bool isAuthenticated();
77 /** Handles a request directed to the mote server. Requests directed to a
78 * mote are directed through this method as well.
79 * \param request Request to handle
81 void handleClientRequest(MsgClientRequest
& request
);
82 /** Handles a request directed to a specific mote. This method looks up the mote
83 * object in the map of controlled motes and passess the request to the mote object.
84 * \param mote_id Database key of the mote to receive the request.
85 * \param request Request to handle
87 void handleMoteRequest(dbkey_t mote_id
,MsgPayload
& request
);
88 /** Attempts to take control of a mote.
89 * \param mote_id Database key of the mote to control.
91 void getMoteControl(dbkey_t mote_id
);
92 /** Releases control of a mote.
93 * \param mote_id Database key of the mote to release
95 void dropMoteControl(dbkey_t mote_id
);
97 /** Set to true when proper client authentication has been completed. **/
99 /** Map of motes currently controlled in this session.**/
100 motemapbykey_t motes
;
101 /** Incoming message handler object. **/
103 /** Session map that this session is registered in. **/
104 sessionmapbyfd_t
& sessionMap
;