gwenhywfar  5.14.1
request.c
Go to the documentation of this file.
1 /****************************************************************************
2  * This file is part of the project Gwenhywfar.
3  * Gwenhywfar (c) by 2024 Martin Preuss, all rights reserved.
4  *
5  * The license for this file can be found in the file COPYING which you
6  * should have received along with this file.
7  ****************************************************************************/
8 
9 #ifdef HAVE_CONFIG_H
10 # include <config.h>
11 #endif
12 
13 /*#define DISABLE_DEBUGLOG*/
14 
15 
16 #include "msgio/request_p.h"
17 
18 #include <gwenhywfar/error.h>
19 #include <gwenhywfar/misc.h>
20 
21 
22 
24 GWEN_TREE2_FUNCTIONS(GWEN_MSG_REQUEST, GWEN_MsgRequest)
25 
26 
27 
28 
30 {
31  GWEN_MSG_REQUEST *rq;
32 
35  GWEN_TREE2_INIT(GWEN_MSG_REQUEST, rq, GWEN_MsgRequest);
36 
37  return rq;
38 }
39 
40 
41 
43 {
44  if (rq) {
45  GWEN_TREE2_FINI(GWEN_MSG_REQUEST, rq, GWEN_MsgRequest);
47 
48  GWEN_Timestamp_free(rq->expiresAt);
49  GWEN_Timestamp_free(rq->createdAt);
50  GWEN_Msg_free(rq->requestMsg);
51  GWEN_Msg_List_free(rq->msgList);
52  GWEN_MsgEndpoint_free(rq->endpoint);
53 
54  GWEN_FREE_OBJECT(rq);
55  }
56 }
57 
58 
59 
61 {
62  return rq?rq->requestType:0;
63 }
64 
65 
66 
68 {
69  if (rq)
70  rq->requestType=t;
71 }
72 
73 
74 
76 {
77  return rq?rq->requestMsg:NULL;
78 }
79 
80 
81 
83 {
84  if (rq) {
85  GWEN_Msg_free(rq->requestMsg);
86  rq->requestMsg=msg;
87  }
88 }
89 
90 
91 
93 {
94  return rq?rq->endpoint:NULL;
95 }
96 
97 
98 
100 {
101  if (rq) {
102  if (ep)
104  if (rq->endpoint)
106  rq->endpoint=ep;
107  }
108 }
109 
110 
111 
113 {
114  return rq?rq->requestMsgId:0;
115 }
116 
117 
118 
120 {
121  if (rq)
122  rq->requestMsgId=id;
123 }
124 
125 
126 
128 {
129  return rq?rq->msgList:NULL;
130 }
131 
132 
133 
135 {
136  return (rq && rq->msgList)?GWEN_Msg_List_First(rq->msgList):NULL;
137 }
138 
139 
140 
142 {
143  if (rq && msg) {
144  if (rq->msgList==NULL)
145  rq->msgList=GWEN_Msg_List_new();
146  GWEN_Msg_List_Add(msg, rq->msgList);
147  }
148 }
149 
150 
151 
153 {
154  return rq?rq->createdAt:NULL;
155 }
156 
157 
158 
160 {
161  if (rq) {
162  GWEN_Timestamp_free(rq->createdAt);
163  rq->createdAt=ts;
164  }
165 }
166 
167 
168 
170 {
171  return rq?rq->expiresAt:NULL;
172 }
173 
174 
175 
177 {
178  if (rq) {
179  GWEN_Timestamp_free(rq->expiresAt);
180  rq->expiresAt=ts;
181  }
182 }
183 
184 
185 
187 {
188  if (rq) {
189  GWEN_TIMESTAMP *ts;
190 
192  GWEN_Timestamp_free(rq->createdAt);
193  rq->createdAt=GWEN_Timestamp_dup(ts);
194  GWEN_Timestamp_AddSeconds(ts, expiresInSecs);
195  GWEN_Timestamp_free(rq->expiresAt);
196  rq->expiresAt=ts;
197  }
198 }
199 
200 
201 
203 {
204  if (rq && rq->handleResponseFn)
205  return (rq->handleResponseFn)(rq, msg);
207 }
208 
209 
210 
212 {
213  if (rq && rq->subRequestFinishedFn)
214  rq->subRequestFinishedFn(rq, subRq, reason);
215 }
216 
217 
218 
220 {
221  if (rq && rq->abortFn) {
222  rq->abortFn(rq, reason);
224  }
225  else {
226  GWEN_MSG_REQUEST *rqParent;
227 
229  rqParent=GWEN_MsgRequest_Tree2_GetParent(rq);
230  if (rqParent)
232  }
233 }
234 
235 
236 
239 {
240  if (rq) {
242 
243  oldFn=rq->handleResponseFn;
244  rq->handleResponseFn=f;
245  return oldFn;
246  }
247  return NULL;
248 }
249 
250 
251 
254 {
255  if (rq) {
257 
258  oldFn=rq->subRequestFinishedFn;
259  rq->subRequestFinishedFn=f;
260  return oldFn;
261  }
262 
263  return NULL;
264 }
265 
266 
267 
269 {
270  if (rq) {
272 
273  oldFn=rq->abortFn;
274  rq->abortFn=f;
275  return oldFn;
276  }
277  return NULL;
278 }
279 
280 
281 
282 
283 
284 
286 {
287  return rq?rq->privateData:NULL;
288 }
289 
290 
291 
293 {
294  if (rq)
295  rq->privateData=p;
296 }
297 
298 
299 
301 {
302  return rq?rq->result:0;
303 }
304 
305 
306 
308 {
309  if (rq)
310  rq->result=result;
311 }
312 
313 
314 
316 {
317  return rq?rq->state:0;
318 }
319 
320 
321 
323 {
324  if (rq)
325  rq->state=i;
326 }
327 
328 
329 
330 
331 
332 
333 
334 
336 {
337  if (rootRq) {
338  GWEN_MSG_REQUEST *rq;
339 
340  rq=GWEN_MsgRequest_Tree2_GetFirstChild(rootRq);
341  while(rq) {
342  if (rq->endpoint==ep && rq->requestMsgId==refMsgId)
343  return rq;
344  rq=GWEN_MsgRequest_Tree2_GetBelow(rq);
345  } /* while */
346  }
347 
348  return NULL;
349 }
350 
351 
352 
353 
GWEN_MSG_REQUEST * GWEN_MsgRequest_new()
Definition: request.c:29
void GWEN_MsgRequest_free(GWEN_MSG_REQUEST *rq)
Definition: request.c:42
const GWEN_TIMESTAMP * GWEN_MsgRequest_GetCreatedAt(const GWEN_MSG_REQUEST *rq)
Definition: request.c:152
int(* GWEN_MSG_REQUEST_HANDLERESPONSE_FN)(GWEN_MSG_REQUEST *rq, GWEN_MSG *msg)
Definition: request.h:39
#define GWEN_INHERIT_FINI(t, element)
Definition: inherit.h:238
struct GWEN_MSG_ENDPOINT GWEN_MSG_ENDPOINT
Object which can send and receive messages (base class).
Definition: endpoint.h:37
const GWEN_TIMESTAMP * GWEN_MsgRequest_GetExpiresAt(const GWEN_MSG_REQUEST *rq)
Definition: request.c:169
int GWEN_MsgRequest_GetState(const GWEN_MSG_REQUEST *rq)
Definition: request.c:315
void GWEN_MsgRequest_SetRequestMsgId(GWEN_MSG_REQUEST *rq, uint32_t id)
Definition: request.c:119
void GWEN_MsgRequest_SetResult(GWEN_MSG_REQUEST *rq, int result)
Definition: request.c:307
void GWEN_MsgRequest_SetExpiresAt(GWEN_MSG_REQUEST *rq, GWEN_TIMESTAMP *ts)
Definition: request.c:176
void GWEN_Msg_free(GWEN_MSG *msg)
Definition: msg.c:78
#define GWEN_MSG_REQUEST_RESULT_NOT_HANDLED
Definition: request.h:21
#define GWEN_FREE_OBJECT(varname)
Definition: memory.h:61
#define NULL
Definition: binreloc.c:300
GWEN_MSG_LIST * GWEN_Msg_List_new()
int GWEN_MsgRequest_GetRequestType(const GWEN_MSG_REQUEST *rq)
Definition: request.c:60
void GWEN_Timestamp_free(GWEN_TIMESTAMP *tstamp)
Definition: timestamp.c:81
void GWEN_MsgEndpoint_Attach(GWEN_MSG_ENDPOINT *ep)
Definition: endpoint.c:53
GWEN_MSG * GWEN_Msg_List_First(const GWEN_MSG_LIST *l)
GWEN_MSG_REQUEST * GWEN_MsgRequest_Tree2_FindByEndpointAndMsgId(GWEN_MSG_REQUEST *rootRq, GWEN_MSG_ENDPOINT *ep, uint32_t refMsgId)
Definition: request.c:335
struct GWEN_MSG_REQUEST GWEN_MSG_REQUEST
Definition: request.h:34
GWEN_MSG_LIST * GWEN_MsgRequest_GetMsgList(const GWEN_MSG_REQUEST *rq)
Definition: request.c:127
GWEN_TIMESTAMP * GWEN_Timestamp_dup(const GWEN_TIMESTAMP *tstampSrc)
Definition: timestamp.c:67
void GWEN_MsgRequest_Abort(GWEN_MSG_REQUEST *rq, int reason)
Definition: request.c:219
#define GWEN_TREE2_FINI(t, element, pr)
Definition: tree2.h:457
void GWEN_MsgRequest_AddMsgToList(GWEN_MSG_REQUEST *rq, GWEN_MSG *msg)
Definition: request.c:141
GWEN_MSG_REQUEST_HANDLERESPONSE_FN GWEN_MsgRequest_SetHandleResponseFn(GWEN_MSG_REQUEST *rq, GWEN_MSG_REQUEST_HANDLERESPONSE_FN f)
Definition: request.c:237
void GWEN_MsgRequest_SetTimestamps(GWEN_MSG_REQUEST *rq, int expiresInSecs)
Definition: request.c:186
#define GWEN_NEW_OBJECT(typ, varname)
Definition: memory.h:55
GWEN_MSG * GWEN_MsgRequest_GetRequestMsg(const GWEN_MSG_REQUEST *rq)
Definition: request.c:75
void GWEN_MsgRequest_SetEndpoint(GWEN_MSG_REQUEST *rq, GWEN_MSG_ENDPOINT *ep)
Definition: request.c:99
void GWEN_MsgEndpoint_free(GWEN_MSG_ENDPOINT *ep)
Definition: endpoint.c:61
#define GWEN_MSG_REQUEST_STATE_DONE
Definition: request.h:30
void GWEN_MsgRequest_SetRequestMsg(GWEN_MSG_REQUEST *rq, GWEN_MSG *msg)
Definition: request.c:82
GWEN_MSG_REQUEST_SUBREQUESTFINISHED_FN GWEN_MsgRequest_SetSubRequestFinishedFn(GWEN_MSG_REQUEST *rq, GWEN_MSG_REQUEST_SUBREQUESTFINISHED_FN f)
Definition: request.c:252
#define GWEN_INHERIT_INIT(t, element)
Definition: inherit.h:223
GWEN_TREE2_FUNCTIONS(GWEN_JSON_ELEM, GWEN_JsonElement)
#define GWEN_MSG_REQUEST_REASON_ABORTED
Definition: request.h:25
void GWEN_MsgRequest_SetState(GWEN_MSG_REQUEST *rq, int i)
Definition: request.c:322
int GWEN_MsgRequest_HandleResponse(GWEN_MSG_REQUEST *rq, GWEN_MSG *msg)
Definition: request.c:202
struct GWEN_MSG GWEN_MSG
Definition: msg.h:24
void GWEN_MsgRequest_SetRequestType(GWEN_MSG_REQUEST *rq, int t)
Definition: request.c:67
GWEN_MSG_REQUEST_ABORT_FN GWEN_MsgRequest_SetAbortFn(GWEN_MSG_REQUEST *rq, GWEN_MSG_REQUEST_ABORT_FN f)
Definition: request.c:268
void(* GWEN_MSG_REQUEST_SUBREQUESTFINISHED_FN)(GWEN_MSG_REQUEST *rq, GWEN_MSG_REQUEST *subRq, int reason)
Definition: request.h:40
void GWEN_MsgRequest_SetPrivateData(GWEN_MSG_REQUEST *rq, void *p)
Definition: request.c:292
void GWEN_Msg_List_free(GWEN_MSG_LIST *l)
#define GWEN_TREE2_INIT(t, element, pr)
Definition: tree2.h:445
int GWEN_MsgRequest_GetResult(const GWEN_MSG_REQUEST *rq)
Definition: request.c:300
void GWEN_MsgRequest_SetCreatedAt(GWEN_MSG_REQUEST *rq, GWEN_TIMESTAMP *ts)
Definition: request.c:159
void GWEN_MsgRequest_SubRequestFinished(GWEN_MSG_REQUEST *rq, GWEN_MSG_REQUEST *subRq, int reason)
Definition: request.c:211
uint32_t GWEN_MsgRequest_GetRequestMsgId(const GWEN_MSG_REQUEST *rq)
Definition: request.c:112
GWEN_TIMESTAMP * GWEN_Timestamp_NowInLocalTime()
Definition: timestamp.c:194
GWEN_MSG_ENDPOINT * GWEN_MsgRequest_GetEndpoint(const GWEN_MSG_REQUEST *rq)
Definition: request.c:92
GWEN_MSG * GWEN_MsgRequest_GetFirstMsgFromList(const GWEN_MSG_REQUEST *rq)
Definition: request.c:134
void * GWEN_MsgRequest_GetPrivateData(const GWEN_MSG_REQUEST *rq)
Definition: request.c:285
void(* GWEN_MSG_REQUEST_ABORT_FN)(GWEN_MSG_REQUEST *rq, int reason)
Definition: request.h:41
#define GWEN_INHERIT_FUNCTIONS(t)
Definition: inherit.h:163
void GWEN_Msg_List_Add(GWEN_MSG *element, GWEN_MSG_LIST *list)
void GWEN_Timestamp_AddSeconds(GWEN_TIMESTAMP *tstamp, int seconds)
Definition: timestamp.c:497
struct GWEN_TIMESTAMP GWEN_TIMESTAMP
Definition: timestamp.h:36