botsense  3.2.0
RoadNarrows Client-Server Proxied Services Framework
bsNullClient.c
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // Package: BotSense
4 //
5 // Module: bsNull
6 // Library: libbsclient_null
7 // File: bsNullClient.c
8 //
9 /*! \file
10  *
11  * $LastChangedDate: 2010-08-20 11:36:38 -0600 (Fri, 20 Aug 2010) $
12  * $Rev: 568 $
13  *
14  * \brief \h_botsense bsProxy client proxied /dev/null device used for testing.
15  *
16  * \author Robin Knight (robin.knight@roadnarrows.com)
17  *
18  * \copyright
19  * \h_copy 2010-2017. RoadNarrows LLC.\n
20  * http://www.roadnarrows.com\n
21  * All Rights Reserved
22  */
23 // Permission is hereby granted, without written agreement and without
24 // license or royalty fees, to use, copy, modify, and distribute this
25 // software and its documentation for any purpose, provided that
26 // (1) The above copyright notice and the following two paragraphs
27 // appear in all copies of the source code and (2) redistributions
28 // including binaries reproduces these notices in the supporting
29 // documentation. Substantial modifications to this software may be
30 // copyrighted by their authors and need not follow the licensing terms
31 // described here, provided that the new terms are clearly indicated in
32 // all files where they apply.
33 //
34 // IN NO EVENT SHALL THE AUTHOR, ROADNARROWS LLC, OR ANY MEMBERS/EMPLOYEES
35 // OF ROADNARROW LLC OR DISTRIBUTORS OF THIS SOFTWARE BE LIABLE TO ANY
36 // PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
37 // DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION,
38 // EVEN IF THE AUTHORS OR ANY OF THE ABOVE PARTIES HAVE BEEN ADVISED OF
39 // THE POSSIBILITY OF SUCH DAMAGE.
40 //
41 // THE AUTHOR AND ROADNARROWS LLC SPECIFICALLY DISCLAIM ANY WARRANTIES,
42 // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
43 // FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN
44 // "AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE NO OBLIGATION TO
45 // PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
46 //
47 ////////////////////////////////////////////////////////////////////////////////
48 
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <libgen.h>
52 #include <string.h>
53 
54 #include "rnr/rnrconfig.h"
55 #include "rnr/log.h"
56 #include "rnr/netmsgs.h"
57 
58 #include "botsense/BotSense.h"
59 #include "botsense/libBotSense.h"
60 #include "botsense/bsNull.h"
61 #include "botsense/bsNullMsgs.h"
62 
63 // ---------------------------------------------------------------------------
64 // Private Interface
65 // ---------------------------------------------------------------------------
66 
67 /*!
68  * \brief \h_botsense client application information.
69  */
71 {
72  .app_name = "libbsclient_null",
73  .brief = "The /dev/null proxied device.",
74  .version = "1.0.0",
75  .date = "2010.07.21",
76  .maintainer = "RaodNarrows LLC",
77  .license = "(C) 2010 RoadNarrows LLC. All rights reserved.",
78 
79  .fnGetMsgName = bsNullGetMsgName
80 };
81 
82 // ---------------------------------------------------------------------------
83 // Public Interface
84 // ---------------------------------------------------------------------------
85 
86 /*!
87  * \brief Get the DevNull message name.
88  *
89  * For each (virtual connection, message id) 2-tuple, there can be a known
90  * name string (provided the id is valid and an application provides the
91  * information).
92  *
93  * \param pClient \h_botsense client.
94  * \param hndVConn Virtual connection handle.
95  * \param uMsgId Message id.
96  *
97  * \return
98  * Returns message name if it can be determined. Otherwise returns "unknown".
99  */
100 const char *bsNullGetMsgName(BsClient_P pClient,
101  BsVConnHnd_T hndVConn,
102  uint_t uMsgId)
103 {
104  const NMMsgDef_T *pMsgDef;
105 
106  pMsgDef = BsNullLookupMsgDef((BsNullMsgId_T)uMsgId);
107 
108  return pMsgDef!=NULL? pMsgDef->m_sMsgName: "unknown";
109 }
110 
111 /*!
112  * \brief Request proxy server to establish a virtual connection to the
113  * /dev/null device.
114  *
115  * \param pClient \h_botsense client.
116  * \param bInitTrace Initial message tracing enable(true)/disable(false) state.
117  *
118  * \return
119  * On success, the virtual connection handle is returned.\n
120  * \copydoc doc_return_ecode
121  */
122 int bsNullReqOpen(BsClient_P pClient, bool_t bInitTrace)
123 {
124  int hnd; // vconn handle / return code
125 
126  //
127  // Execute server transaction (returns handle \h_ge 0 on success).
128  //
129  hnd = bsServerReqOpenDev(pClient, BS_NULL_DEV_NAME, BS_NULL_SERVER_MOD,
130  NULL, (size_t)0, &bsNullAppInfo, bInitTrace);
131 
132  // check transaction return code
133  BSCLIENT_TRY_ECODE(pClient, hnd, "bsServerReqOpenDev(dev='%s') failed.",
135 
136  // return handle
137  return hnd;
138 }
139 
140 /*!
141  * \brief Request proxy server to close client's proxied DevNull device vitual
142  * connection.
143  *
144  * \param pClient \h_botsense client.
145  * \param hndVConn Handle to virtual connection to close.
146  *
147  * \copydoc doc_return_std
148  */
149 int bsNullReqClose(BsClient_P pClient, BsVConnHnd_T hndVConn)
150 {
151  return bsServerReqCloseDev(pClient, hndVConn);
152 }
153 
154 /*!
155  * \brief Proxied request to write to /dev/null.
156  *
157  * \param pClient \h_botsense client.
158  * \param hndVConn Handle to virtual connection to close.
159  * \param [in] wbuf Write buffer.
160  * \param uWriteLen Number of bytes to write.
161  *
162  * \return
163  * Returns number of bytes written on success.\n
164  * \copydoc doc_return_ecode
165  */
167  BsVConnHnd_T hndVConn,
168  byte_t wbuf[],
169  size_t uWriteLen)
170 {
171  static BsNullMsgId_T msgIdReq = BsNullMsgIdReqWrite;
172  static BsNullMsgId_T msgIdRsp = BsNullMsgIdRspWrite;
173 
174  BsNullReqWrite_T msgReq; // request message
175  BsNullRspWrite_T msgRsp; // response message
176  byte_t buf[BSPROXY_MSG_MAX_LEN]; // req/rsp buffer
177  bool_t bTrace; // do [not] trace messages
178  int n; // number of bytes/return code
179 
180  //
181  // Parameter checks.
182  //
183  BSCLIENT_TRY_EXPR(pClient, BSCLIENT_HAS_VCONN(pClient, hndVConn),
184  BS_ECODE_BAD_VAL, "VConn=%d", hndVConn);
185 
186  BSCLIENT_TRY_EXPR(pClient,
187  (uWriteLen <= (size_t)BSNULL_REQWRITE_WRITEBUF_LEN), BS_ECODE_BAD_VAL,
188  "write_len=%zu > max_len=%zu",
189  uWriteLen, (size_t)BSNULL_REQWRITE_WRITEBUF_LEN);
190 
191  // trace state
192  bTrace = bsClientAttrGetTraceState(pClient, hndVConn);
193 
194  //
195  // Set request message values.
196  //
197  memcpy(msgReq.m_writebuf.u.m_buf, wbuf, uWriteLen);
198  msgReq.m_writebuf.m_count = uWriteLen;
199 
200  //
201  // Pack request.
202  //
203  n = BsNullPackReqWrite(&msgReq, BSPROXY_BUF_BODY(buf), bTrace);
204 
205  // check packing return code
206  BSCLIENT_TRY_NM_ECODE(pClient, n, "MsgId=%u", msgIdReq);
207 
208  //
209  // Execute request-response transaction.
210  //
211  n = bsClientTrans(pClient, hndVConn,
212  msgIdReq, buf, (size_t)n,
213  msgIdRsp, buf, sizeof(buf));
214 
215  // check transaction return code
216  BSCLIENT_TRY_ECODE(pClient, n, "MsgId=%u: Transaction failed.", msgIdReq);
217 
218  //
219  // Unpack response.
220  //
221  n = BsNullUnpackRspWrite(buf, (size_t)n, &msgRsp, bTrace);
222 
223  // check unpack return code
224  BSCLIENT_TRY_NM_ECODE(pClient, n, "MsgId=%u", msgIdRsp);
225 
226  //
227  // Set return values from response.
228  //
229  return (int)msgRsp.m_byteswritten;
230 }
#define BS_NULL_DEV_NAME
DevNull device.
Definition: bsNull.h:55
#define BSCLIENT_TRY_ECODE(pClient, ecode, efmt,...)
Check if <b><i>BotSense</i></b> return value is not an error ( &lt; 0).
Definition: libBotSense.h:214
#define BS_ECODE_BAD_VAL
bad value
Definition: BotSense.h:77
int bsNullReqWrite(BsClient_P pClient, BsVConnHnd_T hndVConn, byte_t wbuf[], size_t uWriteLen)
Proxied request to write to /dev/null.
Definition: bsNullClient.c:166
union BsNullReqWrite_T::@8::@9 u
aligned vector items
const char * bsNullGetMsgName(BsClient_P pClient, BsVConnHnd_T hndVConn, uint_t uMsgId)
Get the DevNull message name.
Definition: bsNullClient.c:100
int bsNullReqOpen(BsClient_P pClient, bool_t bInitTrace)
Request proxy server to establish a virtual connection to the /dev/null device.
Definition: bsNullClient.c:122
The Client Structure Type.
Definition: bsLibInternal.h:96
<b><i>BotSense</i></b> bsProxy client library /dev/null interface.
const NMMsgDef_T * BsNullLookupMsgDef(BsNullMsgId_T eMsgId)
Look up the message definition associated with the message id.
Definition: bsNullMsgs.c:186
INLINE_IN_H int BsNullUnpackRspWrite(byte_t buf[], size_t uMsgLen, BsNullRspWrite_T *pStruct, bool_t bTrace)
Unpack a BsNullRspWrite ITV message in big-endian byte order from the input buffer.
Definition: bsNullMsgs.h:202
<b><i>BotSense</i></b> client library declarations.
#define BS_NULL_SERVER_MOD
server plugin dll module
Definition: bsNull.h:57
<b><i>BotSense</i></b> client library information and callbacks to application-specific data...
Definition: libBotSense.h:86
#define BSCLIENT_TRY_EXPR(pClient, expr, ecode, efmt,...)
Check if expression evaluates to true.
Definition: libBotSense.h:259
#define BSNULL_REQWRITE_WRITEBUF_LEN
Definition: bsNullMsgs.h:54
struct BsNullReqWrite_T::@8 m_writebuf
vector
byte_t m_byteswritten
byteswritten
Definition: bsNullMsgs.h:83
#define BSPROXY_BUF_BODY(buf)
Convenience macro to produce a buffer (offset, size) 2-tuple.
Definition: BotSense.h:272
BsNullMsgId_T
Definition: bsNullMsgs.h:35
int bsNullReqClose(BsClient_P pClient, BsVConnHnd_T hndVConn)
Request proxy server to close client&#39;s proxied DevNull device vitual connection.
Definition: bsNullClient.c:149
#define BSCLIENT_HAS_VCONN(pClient, hnd)
Tests if the handle is valid and there is an established virtual connection.
Definition: libBotSense.h:285
#define BSCLIENT_TRY_NM_ECODE(pClient, nmecode, efmt,...)
Check if NetMsgs (un)packing return value is not an error ( &lt; 0).
Definition: libBotSense.h:236
#define BSPROXY_MSG_MAX_LEN
total message maximum length
Definition: BotSense.h:259
INLINE_IN_H int BsNullPackReqWrite(BsNullReqWrite_T *pStruct, byte_t buf[], size_t bufSize, bool_t bTrace)
Pack a BsNullReqWrite ITV message in big-endian byte order into the output buffer.
Definition: bsNullMsgs.h:138
static BsClientAppInfo_T bsNullAppInfo
<b><i>BotSense</i></b> client application information.
Definition: bsNullClient.c:70
const char * app_name
application name
Definition: libBotSense.h:88
<b><i>BotSense</i></b> package top-level, unifying header declarations.
size_t m_count
vector item count
Definition: bsNullMsgs.h:63
BotSense bsProxy server - client /dev/null NetMsgs XML Definition.
int BsVConnHnd_T
virtual connection handle type
Definition: BotSense.h:151