HIPC  0.5
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros
session.c
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 
5 #include <hipc/session.h>
6 
17 void hipcERRCHECK(const hipc_session * const sess_p,
18  const char *const filename, const int lineno)
19 {
20  int std_errno;
21  const char *errdtlstr;
22 
23  if (NULL == sess_p) {
24  fprintf(stderr, "%s:%d:sess_p is NULL.\n", filename, lineno);
25  exit(EXIT_FAILURE);
26  }
27 
28  if (HIPC_SUCCESS != hipcClientGetHipcErrno(&(sess_p->cl))) {
29  fprintf(stderr, "%s:%d:%s\n",
30  filename, lineno, hipcClientGetErrStr(&(sess_p->cl)));
31 
32  std_errno = hipcClientGetStdErrno(&(sess_p->cl));
33  if (0 != std_errno) {
34  fprintf(stderr, "std_errno:%d:%s\n", std_errno,
35  strerror(std_errno));
36  }
37 
38  errdtlstr = hipcClientGetErrDtlStr(&(sess_p->cl));
39  if ('\0' != errdtlstr[0]) {
40  fprintf(stderr, "ErrorDetail:%s\n", errdtlstr);
41  }
42 
43  exit(EXIT_FAILURE);
44  }
45 
46  return;
47 }
48 
56 enum HIPC_errno hipcRecvMsg(hipc_session * const sess_p, hipc_msg msgR)
57 {
58  enum HIPC_errno retval;
59 
60  retval = sess_p->rfnc_p(sess_p->rarg, msgR);
61  if (HIPC_SUCCESS != retval) {
62  hipcClientSetStdErrno(&sess_p->cl);
63  hipcClientSetError(&sess_p->cl, retval,
64  "Failed to receive a message");
65  }
66  return retval;
67 }
68 
76 enum HIPC_errno hipcSendMsg(hipc_session * const sess_p, hipc_msg msgS)
77 {
78  enum HIPC_errno retval;
79 
80  retval = sess_p->wfnc_p(sess_p->warg, msgS);
81  if (HIPC_SUCCESS != retval) {
82  hipcClientSetStdErrno(&sess_p->cl);
83  hipcClientSetError(&sess_p->cl, retval,
84  "Failed to send a message");
85  }
86  return retval;
87 }
88 
98 enum HIPC_errno hipcWaitReturn(hipc_session * const sess_p, hipc_msg msgR)
99 {
100  int f_end;
101  enum HIPC_errno retval;
102 
103  if (HIPC_SESS_ESTABLISHED != sess_p->state) {
104  return hipcClientSetError(&sess_p->cl,
106  "Not established session");
107  }
108 
109  f_end = 0;
110  do {
111  retval = hipcRecvMsg(sess_p, msgR);
112  if (HIPC_SUCCESS != retval) {
113  return retval;
114  }
115  /* fprintf(stderr, "MsgType: %d\n", hipcMsgType(msgR)); */
116  switch (hipcMsgType(msgR)) {
118  if (HIPC_UNSTRU == hipcMsgStruno(msgR)) {
119  retval = HIPC_SUCCESS;
120  } else {
121  retval = hipcClientFeedStrumsg(&sess_p->cl, msgR);
122  }
123  f_end = 1;
124  break;
126  sess_p->state = HIPC_SESS_QUITTED;
127  retval = hipcClientFeedUnstrumsgAsError(&sess_p->cl, msgR);
128  f_end = 1;
129  break;
131  retval = hipcClientFeedStrumsg(&sess_p->cl, msgR);
132  if (HIPC_SUCCESS != retval) {
133  return retval;
134  }
135  retval = hipcClientUnpack(&sess_p->cl,
136  hipcMsgStruno(msgR),
137  hipcMsgOffset(msgR),
138  hipcMsgRngsize(msgR), NULL, NULL);
139  if (HIPC_SUCCESS != retval) {
140  return retval;
141  }
142  retval = hipcClientCallHandler(&sess_p->cl,
143  hipcMsgStruno(msgR), msgR);
144  if (HIPC_SUCCESS != retval) {
145  if (HIPC_SUCCESS == hipcClientGetHipcErrno(&sess_p->cl)) {
146  hipcClientSetError(&sess_p->cl, retval,
147  "Handler returned an error.");
148  }
149  return retval;
150  }
151  break;
152  default:
153  return hipcClientSetError(&sess_p->cl,
155  "INVALID_MSG_TYPE: Type must be SUCEESS, CAST, or QUIT");
156  }
157  } while (0 == f_end);
158 
159  return retval;
160 }
161 
173 enum HIPC_errno
175  char const *const str,
176  void *const cimg,
177  enum HIPC_errno (*hndlr_p) (struct HIPC_client *,
178  const hipc_msg, void *, void *),
179  void *const arg)
180 {
181  unsigned char struno;
182  size_t offset;
183  size_t size;
184  enum HIPC_errno retval;
185 
186  if (HIPC_SESS_ESTABLISHED != sess_p->state) {
187  return hipcClientSetError(&sess_p->cl,
189  "Not established session");
190  }
191 
192  retval = hipcClientGetStrutpl(&sess_p->cl, str,
193  &struno, &offset, &size);
194  if (HIPC_SUCCESS != retval) {
195  return retval;
196  }
197  hipcClientSetCimg(&sess_p->cl, struno, cimg);
198  hipcClientSetHandler(&sess_p->cl, struno, hndlr_p, arg);
199 
200  return HIPC_SUCCESS;
201 }
202 
216 enum HIPC_errno
217 hipcOpen(hipc_session * const sess_p,
218  struct HIPC_client const *const clbase_p,
219  enum HIPC_errno (*rfnc_p) (void *, hipc_msg), void *rarg,
220  enum HIPC_errno (*wfnc_p) (void *, const hipc_msg), void *warg)
221 {
222  int i;
223  hipc_msg msgS;
224  hipc_msg msgR;
225  enum HIPC_errno retval;
226 
227  sess_p->rfnc_p = rfnc_p;
228  sess_p->rarg = rarg;
229  sess_p->wfnc_p = wfnc_p;
230  sess_p->warg = warg;
231  sess_p->state = HIPC_SESS_QUITTED;
232  retval = hipcClientInit(&sess_p->cl, clbase_p);
233  if (HIPC_SUCCESS != retval) {
234  return retval;
235  }
236 
237  hipcGenmsgHello(msgS, &sess_p->cl);
238  retval = hipcSendMsg(sess_p, msgS);
239  if (HIPC_SUCCESS != retval) {
240  return retval;
241  }
242 
243  for (i = 0; i < hipcClientGetNsysmsg(&sess_p->cl); ++i) {
244  retval = hipcRecvMsg(sess_p, msgR);
245  if (HIPC_SUCCESS != retval) {
246  return retval;
247  }
248  if (HIPC_MESSAGE_TYPE_QUIT == hipcMsgType(msgR)) {
249  /* the state has been already HIPC_SESS_QUITTED
250  because it was set at the begging of this function */
251  return hipcClientFeedUnstrumsgAsError(&sess_p->cl, msgR);
252  }
253  retval = hipcClientFeedSysmsg(&sess_p->cl, msgR);
254  if (HIPC_SUCCESS != retval) {
255  return retval;
256  }
257  }
258  retval = hipcClientCheckMbrSize(&sess_p->cl);
259  if (HIPC_SUCCESS != retval) {
260  return retval;
261  }
262 
263  sess_p->state = HIPC_SESS_ESTABLISHED;
264 
265  return HIPC_SUCCESS;
266 }
267 
276 enum HIPC_errno hipcClose(hipc_session * const sess_p)
277 {
278  hipc_msg msgS;
279  hipc_msg msgR;
280  enum HIPC_errno retval;
281 
282  retval = HIPC_SUCCESS;
283  if (HIPC_SESS_ESTABLISHED == sess_p->state) {
284  hipcGenmsgBye(msgS);
285  retval = hipcSendMsg(sess_p, msgS);
286  if (HIPC_SUCCESS == retval) {
287  retval = hipcWaitReturn(sess_p, msgR);
288  }
289  }
290 
291  hipcClientTerminate(&sess_p->cl);
292 
293  sess_p->rfnc_p = NULL;
294  sess_p->rarg = NULL;
295  sess_p->wfnc_p = NULL;
296  sess_p->warg = NULL;
297  sess_p->state = HIPC_SESS_TERMINATED;
298 
299  return retval;
300 }
301 
311 enum HIPC_errno
312 hipcGet(hipc_session * const sess_p,
313  char const *const str, void *const cimg)
314 {
315  hipc_msg msgS;
316  hipc_msg msgR;
317  unsigned char struno;
318  size_t offset;
319  size_t size;
320  int retval;
321 
322  if (HIPC_SESS_ESTABLISHED != sess_p->state) {
323  return hipcClientSetError(&sess_p->cl,
325  "Not established session");
326  }
327 
328  retval = hipcClientGetStrutpl(&sess_p->cl, str,
329  &struno, &offset, &size);
330  if (HIPC_SUCCESS != retval) {
331  return retval;
332  }
333  retval = hipcGenmsgGet(msgS, &sess_p->cl, struno, offset, size);
334  if (HIPC_SUCCESS != retval) {
335  return retval;
336  }
337  retval = hipcSendMsg(sess_p, msgS);
338  if (HIPC_SUCCESS != retval) {
339  return retval;
340  }
341  retval = hipcWaitReturn(sess_p, msgR);
342  if (HIPC_SUCCESS != retval) {
343  return retval;
344  }
345  retval =
346  hipcClientUnpack(&sess_p->cl, struno, offset, size, NULL, cimg);
347 
348  return retval;
349 }
350 
360 enum HIPC_errno
361 hipcPut(hipc_session * const sess_p,
362  char const *const str, void *const cimg)
363 {
364  hipc_msg msgS;
365  hipc_msg msgR;
366  unsigned char struno;
367  size_t offset;
368  size_t size;
369  int retval;
370 
371  if (HIPC_SESS_ESTABLISHED != sess_p->state) {
372  return hipcClientSetError(&sess_p->cl,
374  "Not established session");
375  }
376 
377  retval = hipcClientGetStrutpl(&sess_p->cl, str,
378  &struno, &offset, &size);
379  if (HIPC_SUCCESS != retval) {
380  return retval;
381  }
382  retval = hipcClientPack(&sess_p->cl, struno, offset, size, NULL, cimg);
383  if (HIPC_SUCCESS != retval) {
384  return retval;
385  }
386  retval = hipcGenmsgPut(msgS, &sess_p->cl, struno, offset, size);
387  if (HIPC_SUCCESS != retval) {
388  return retval;
389  }
390  retval = hipcSendMsg(sess_p, msgS);
391  if (HIPC_SUCCESS != retval) {
392  return retval;
393  }
394  retval = hipcWaitReturn(sess_p, msgR);
395 
396  return retval;
397 }
enum HIPC_errno hipcClientInit(struct HIPC_client *const cl_p, struct HIPC_client const *const clbase_p)
Initializes a HIPC client with allocating memory space if necessary.
Definition: client.c:339
void * warg
Definition: session.h:20
void hipcClientSetStdErrno(struct HIPC_client *const cl_p)
Sets errno value to a HIPC client.
Definition: client.c:119
unsigned char hipc_msg[HIPC_MSG_ALLOCSIZE]
Definition: msg.h:8
void hipcGenmsgHello(hipc_msg msg, const struct HIPC_client *const cl_p)
Generates HELLO message.
Definition: client.c:1201
enum HIPC_errno hipcClientFeedSysmsg(struct HIPC_client *const cl_p, const hipc_msg msgR)
Feeds a system message and incorporates it to the internal data of the client.
Definition: client.c:1087
enum HIPC_errno hipcClientCallHandler(struct HIPC_client *const cl_p, const unsigned char struno, const hipc_msg msgR)
Calls a handler that corresponds to struno.
Definition: client.c:303
enum HIPC_errno hipcRecvMsg(hipc_session *const sess_p, hipc_msg msgR)
Receives a HIPC message.
Definition: session.c:56
enum HIPC_errno hipcSendMsg(hipc_session *const sess_p, hipc_msg msgS)
Sends a HIPC message.
Definition: session.c:76
enum HIPC_errno hipcClientCheckMbrSize(struct HIPC_client *const cl_p)
Checks whether sizes of members in structures in a server are the same as sizes in a client...
Definition: client.c:458
enum HIPC_errno hipcOpen(hipc_session *const sess_p, struct HIPC_client const *const clbase_p, enum HIPC_errno(*rfnc_p)(void *, hipc_msg), void *rarg, enum HIPC_errno(*wfnc_p)(void *, const hipc_msg), void *warg)
Open a session to a HIPC server.
Definition: session.c:217
enum HIPC_errno hipcClientGetStrutpl(struct HIPC_client *const cl_p, char const *const str, unsigned char *struno_p, size_t *offset_p, size_t *size_p)
Gets structure number, offset, and size from ID string.
Definition: client.c:698
enum HIPC_errno(* wfnc_p)(void *, const hipc_msg)
Definition: session.h:19
enum HIPC_errno hipcGet(hipc_session *const sess_p, char const *const str, void *const cimg)
Gets data specified by str from a HIPC server.
Definition: session.c:312
enum HIPC_errno hipcGenmsgPut(hipc_msg msg, struct HIPC_client *const cl_p, const unsigned char struno, const size_t offset, const size_t size)
Generates PUT message.
Definition: client.c:1253
int hipcClientGetStdErrno(const struct HIPC_client *const cl_p)
Gets an errno value saved by hipcClientSetStdErrno().
Definition: client.c:131
enum HIPC_errno hipcPut(hipc_session *const sess_p, char const *const str, void *const cimg)
Puts data specified by str from a HIPC server.
Definition: session.c:361
HIPC_errno
Definition: hipc.h:4
void hipcGenmsgBye(hipc_msg msg)
Initializes a HIPC message as a BYE message.
Definition: client.c:1279
unsigned char hipcMsgOffset(const hipc_msg msg)
Returns offset of a HIPC message.
Definition: msg.c:53
enum HIPC_errno hipcClose(hipc_session *const sess_p)
Closes a session.
Definition: session.c:276
void hipcClientSetHandler(struct HIPC_client *const cl_p, const unsigned char struno, enum HIPC_errno(*hndlr_p)(struct HIPC_client *, const hipc_msg, void *, void *), void *const arg)
Sets an handler hndlr_p to a structure number struno.
Definition: client.c:279
void * rarg
Definition: session.h:18
enum HIPC_errno hipcClientFeedStrumsg(struct HIPC_client *const cl_p, const hipc_msg msgR)
Feeds a message and incorporates it to the internal data of a client.
Definition: client.c:1174
enum HIPC_errno hipcClientSetError(struct HIPC_client *const cl_p, enum HIPC_errno hipc_errno, char const *const str)
Sets an error to a client with error detail.
Definition: client.c:240
enum HIPC_errno hipcGenmsgGet(hipc_msg msg, struct HIPC_client *const cl_p, const unsigned char struno, const size_t offset, const size_t size)
Generates GET message.
Definition: client.c:1223
enum HIPC_errno hipcClientUnpack(struct HIPC_client *const cl_p, const unsigned char struno, const size_t offset, const size_t size, void *simg, void *cimg)
Unpacks a range of contents of simg into cimg.
Definition: client.c:908
void hipcClientSetCimg(struct HIPC_client *const cl_p, const unsigned char struno, void *cimg_p)
Sets a client-side image that is a structure instance of a structure.
Definition: client.c:260
enum HIPC_errno hipcWaitReturn(hipc_session *const sess_p, hipc_msg msgR)
Waits a return message from a HIPC server.
Definition: session.c:98
enum HIPC_errno hipcSetHandler(hipc_session *const sess_p, char const *const str, void *const cimg, enum HIPC_errno(*hndlr_p)(struct HIPC_client *, const hipc_msg, void *, void *), void *const arg)
Set a handler to a structure type.
Definition: session.c:174
unsigned char hipcMsgStruno(const hipc_msg msg)
Returns structure number of a HIPC message.
Definition: msg.c:64
void hipcERRCHECK(const hipc_session *const sess_p, const char *const filename, const int lineno)
Checks whether an error has occurred.
Definition: session.c:17
enum HIPC_session_state state
Definition: session.h:16
struct HIPC_client cl
Definition: session.h:15
const char * hipcClientGetErrStr(const struct HIPC_client *const cl_p)
Gets a string describing a HIPC error number that has been set to a HIPC client.
Definition: client.c:155
unsigned char hipcClientGetNsysmsg(struct HIPC_client const *const cl_p)
Gets the number of system messages.
Definition: client.c:326
enum HIPC_errno hipcClientPack(struct HIPC_client *const cl_p, const unsigned char struno, const size_t offset, const size_t size, void *simg, void *cimg)
Packs a range of contents of cimg into simg.
Definition: client.c:962
enum HIPC_errno hipcClientFeedUnstrumsgAsError(struct HIPC_client *const cl_p, const hipc_msg msgR)
Feeds a message and sets an error specified by the message.
Definition: client.c:1011
void hipcClientTerminate(struct HIPC_client *const cl_p)
Terminates a HIPC client with releasing the memory space that the client has.
Definition: client.c:438
enum HIPC_errno hipcClientGetHipcErrno(const struct HIPC_client *const cl_p)
Gets a HIPC error number that has been set.
Definition: client.c:143
enum HIPC_errno(* rfnc_p)(void *, hipc_msg)
Definition: session.h:17
const char * hipcClientGetErrDtlStr(const struct HIPC_client *const cl_p)
Gets a string describing details of an error that has been set to a HIPC client.
Definition: client.c:171
unsigned char hipcMsgType(const hipc_msg msg)
Returns type of a HIPC message.
Definition: msg.c:42
unsigned char hipcMsgRngsize(const hipc_msg msg)
Returns range size of a HIPC message.
Definition: msg.c:75
#define HIPC_UNSTRU
Definition: hipc.h:44