Initial commit
[remote/remote-mci.git] / mcs / Session.h
blobc7e8197bd6f9117dc183bc253415dd69ea58a8e3
1 #ifndef _CLIENT_H_
2 #define _CLIENT_H_
4 #include "FileDescriptor.h"
5 #include "Mote.h"
6 #include "Message.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>
13 #include <unistd.h>
14 #include <fcntl.h>
16 namespace remote { namespace mcs {
18 using namespace protocols;
19 using namespace protocols::client_server;
21 class Mote;
22 typedef std::map<dbkey_t,Mote*> motemapbykey_t;
24 class Session;
25 typedef std::map<int,Session*> sessionmapbyfd_t;
27 /** Each instance of this class handles a single client connection.
28 **/
29 class Session : public FileDescriptor
31 public:
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.
35 **/
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.
39 **/
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
44 **/
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
52 **/
53 void freeMote(dbkey_t mote_id);
54 /** Database key for this sessions session record **/
55 dbkey_t session_id;
56 protected:
57 /** Session object destructor
58 **/
59 virtual ~Session();
60 /** Handles an event on the socket of the remote client by invoking the appropriate event handler.
61 * \param events Event descriptor.
62 **/
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.
70 **/
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.
77 **/
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
82 **/
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
88 **/
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.
92 **/
93 void getMoteControl(dbkey_t mote_id);
94 /** Releases control of a mote.
95 * \param mote_id Database key of the mote to release
96 **/
97 void dropMoteControl(dbkey_t mote_id);
99 /** Set to true when proper client authentication has been completed. **/
100 bool authenticated;
101 /** Map of motes currently controlled in this session.**/
102 motemapbykey_t motes;
103 /** Incoming message handler object. **/
104 Message message_in;
105 /** Session map that this session is registered in. **/
106 sessionmapbyfd_t& sessionMap;
112 #endif