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