appkit  1.5.1
RoadNarrows Robotics Application Kit
Session.h
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // Package: RoadNarrows Robotics Application Tool Kit
4 //
5 // Link: https://github.com/roadnarrows-robotics/rnr-sdk
6 //
7 // Library: librnr_appkit
8 //
9 // File: Session.h
10 //
11 /*! \file
12  *
13  * $LastChangedDate: 2013-05-06 10:03:14 -0600 (Mon, 06 May 2013) $
14  * $Rev: 2907 $
15  *
16  * \brief Session base class.
17  *
18  * \author Robin Knight (robin.knight@roadnarrows.com)
19  * \author Daniel Packard (daniel@roadnarrows.com)
20  *
21  * \par Copyright
22  * \h_copy 2012-2017. RoadNarrows LLC.\n
23  * http://www.roadnarrows.com\n
24  * All Rights Reserved
25  */
26 /*
27  * @EulaBegin@
28  *
29  * Permission is hereby granted, without written agreement and without
30  * license or royalty fees, to use, copy, modify, and distribute this
31  * software and its documentation for any purpose, provided that
32  * (1) The above copyright notice and the following two paragraphs
33  * appear in all copies of the source code and (2) redistributions
34  * including binaries reproduces these notices in the supporting
35  * documentation. Substantial modifications to this software may be
36  * copyrighted by their authors and need not follow the licensing terms
37  * described here, provided that the new terms are clearly indicated in
38  * all files where they apply.
39  *
40  * IN NO EVENT SHALL THE AUTHOR, ROADNARROWS LLC, OR ANY MEMBERS/EMPLOYEES
41  * OF ROADNARROW LLC OR DISTRIBUTORS OF THIS SOFTWARE BE LIABLE TO ANY
42  * PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
43  * DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION,
44  * EVEN IF THE AUTHORS OR ANY OF THE ABOVE PARTIES HAVE BEEN ADVISED OF
45  * THE POSSIBILITY OF SUCH DAMAGE.
46  *
47  * THE AUTHOR AND ROADNARROWS LLC SPECIFICALLY DISCLAIM ANY WARRANTIES,
48  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
49  * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN
50  * "AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE NO OBLIGATION TO
51  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
52  *
53  * @EulaEnd@
54  */
55 ////////////////////////////////////////////////////////////////////////////////
56 
57 #ifndef _RNR_SESSION_H
58 #define _RNR_SESSION_H
59 
60 #include <stdio.h>
61 
62 #include <string>
63 
64 #include "rnr/rnrconfig.h"
65 
66 #include "rnr/appkit/State.h"
67 #include "rnr/appkit/StateMach.h"
68 
69 /*!
70  * \brief RoadNarrows Robotics
71  */
72 namespace rnr
73 {
74  //----------------------------------------------------------------------------
75  // Session Class
76  //----------------------------------------------------------------------------
77 
78  /*!
79  * \brief Session Class
80  *
81  * Session state data persist throughout the lifetime of an application or
82  * user session. Applications may support multiple sessions simultaneously.
83  */
84  class Session
85  {
86  public:
87  /*!
88  * \brief Default initialization contructor.
89  *
90  * \param nSessionId Session id.
91  * \param strSessionName Session name.
92  */
93  Session(int nSessionId = 0, const std::string &strSessionName = "Session") :
94  m_nSessionId(nSessionId), m_strSessionName(strSessionName),
95  m_sm(nSessionId, strSessionName)
96  {
97  }
98 
99  /*!
100  * \brief Destructor.
101  */
103  {
104  }
105 
106  /*!
107  * \brief Session's state machine.
108  *
109  * \return State machine reference.
110  */
112  {
113  return m_sm;
114  }
115 
116  /*!
117  * \brief Get session id.
118  *
119  * \return Session id.
120  */
121  int getSessionId() const
122  {
123  return m_nSessionId;
124  }
125 
126  /*!
127  * \brief Get session name.
128  *
129  * \return String.
130  */
131  std::string getSessionName() const
132  {
133  return m_strSessionName;
134  }
135 
136  /*!
137  * \brief Set state relevant context.
138  *
139  * \param pContext State specific context data.
140  */
141  void setContext(void *pContext)
142  {
143  m_pContext = pContext;
144  }
145 
146  /*!
147  * \brief Get state relevant context.
148  *
149  * \return State specific context data.
150  */
151  void *getContext()
152  {
153  return m_pContext;
154  }
155 
156  /*!
157  * \brief Set session error.
158  *
159  * \param ecode Application specific error code.
160  * \param sFmt Format string.
161  * \param ... Formatted variable arguments.
162  */
163  void setError(int ecode, const char *sFmt, ...);
164 
165  /*!
166  * \brief Set session fatal error.
167  *
168  * \param ecode Application specific error code.
169  * \param sFmt Format string.
170  * \param ... Formatted variable arguments.
171  */
172  void setFatal(int ecode, const char *sFmt, ...);
173 
174  /*!
175  * \brief Get the last error code.
176  *
177  * \return Application specific error code.
178  */
180  {
181  return m_ecode;
182  }
183 
184  /*!
185  * \brief Get the last error message.
186  *
187  * \return Error message.
188  */
189  std::string getErrorMsg()
190  {
191  return m_bufErrorMsg;
192  }
193 
194  /*!
195  * \brief Test if session is in fatal condition.
196  *
197  * \return Returns true or false.
198  */
199  bool isFatal()
200  {
201  return m_bHasFatal;
202  }
203 
204  protected:
205  int m_nSessionId; ///< session id
206  std::string m_strSessionName; ///< session name
207  StateMach m_sm; ///< session state machine
208  void *m_pContext; ///< state specific data
209  int m_ecode; ///< last error code
210  char m_bufErrorMsg[256]; ///< error message
211  bool m_bHasFatal; ///< does [not] have fatal condition
212  };
213 
214 } // namespace rnr
215 
216 
217 #endif // _RNR_SESSION_H
void setError(int ecode, const char *sFmt,...)
Set session error.
Definition: Session.cxx:72
std::string getErrorMsg()
Get the last error message.
Definition: Session.h:189
int m_nSessionId
session id
Definition: Session.h:205
int getErrorCode()
Get the last error code.
Definition: Session.h:179
StateMach & sm()
Session&#39;s state machine.
Definition: Session.h:111
~Session()
Destructor.
Definition: Session.h:102
int m_ecode
last error code
Definition: Session.h:209
Finite State Machine interface.
void setFatal(int ecode, const char *sFmt,...)
Set session fatal error.
Definition: Session.cxx:85
std::string m_strSessionName
session name
Definition: Session.h:206
void * getContext()
Get state relevant context.
Definition: Session.h:151
bool m_bHasFatal
does [not] have fatal condition
Definition: Session.h:211
Session(int nSessionId=0, const std::string &strSessionName="Session")
Default initialization contructor.
Definition: Session.h:93
void * m_pContext
state specific data
Definition: Session.h:208
char m_bufErrorMsg[256]
error message
Definition: Session.h:210
void setContext(void *pContext)
Set state relevant context.
Definition: Session.h:141
Session Class.
Definition: Session.h:84
std::string getSessionName() const
Get session name.
Definition: Session.h:131
State base class interface.
Finite State Machine Class.
Definition: StateMach.h:83
bool isFatal()
Test if session is in fatal condition.
Definition: Session.h:199
StateMach m_sm
session state machine
Definition: Session.h:207
RoadNarrows Robotics.
Definition: Camera.h:74
int getSessionId() const
Get session id.
Definition: Session.h:121