forked from cms-externals/frontier_client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrs-bin.c
345 lines (293 loc) · 6.92 KB
/
rs-bin.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/*
* frontier client response set handler
*
* Author: Sergey Kosyakov
*
* $Id$
*
* Copyright (c) 2009, FERMI NATIONAL ACCELERATOR LABORATORY
* All rights reserved.
*
* For details of the Fermitools (BSD) license see Fermilab-2009.txt or
* http://fermitools.fnal.gov/about/terms.html
*
*/
#include <frontier_client/frontier.h>
#include <sys/types.h>
#include <stdlib.h>
#include <strings.h>
#include "fn-internal.h"
#include "fn-endian.h"
#define TYPED_BLOB
union u_Buf32 { int v; float f; char b[4]; };
union u_Buf64 { long long v; double d; char b[8]; };
#if __BYTE_ORDER == __LITTLE_ENDIAN
static inline int n2h_i32(const unsigned char *p)
{
union u_Buf32 u;
u.b[3]=p[0];
u.b[2]=p[1];
u.b[1]=p[2];
u.b[0]=p[3];
return u.v;
}
static inline float n2h_f32(const unsigned char *p)
{
union u_Buf32 u;
u.b[3]=p[0];
u.b[2]=p[1];
u.b[1]=p[2];
u.b[0]=p[3];
return u.f;
}
static inline long long n2h_i64(const unsigned char *p)
{
union u_Buf64 u;
u.b[7]=p[0];
u.b[6]=p[1];
u.b[5]=p[2];
u.b[4]=p[3];
u.b[3]=p[4];
u.b[2]=p[5];
u.b[1]=p[6];
u.b[0]=p[7];
return u.v;
}
static inline double n2h_d64(const unsigned char *p)
{
union u_Buf64 u;
u.b[7]=p[0];
u.b[6]=p[1];
u.b[5]=p[2];
u.b[4]=p[3];
u.b[3]=p[4];
u.b[2]=p[5];
u.b[1]=p[6];
u.b[0]=p[7];
return u.d;
}
#else
#warning Big endian order
#include <strings.h>
static inline int n2h_i32(const unsigned char *p)
{
return *((int*)p);
}
static inline float n2h_f32(const unsigned char *p)
{
return *((float*)p);
}
static inline long long n2h_i64(const unsigned char *p)
{
return *((long long*)p);
}
static inline double n2h_d64(const unsigned char *p)
{
return *((double*)p);
}
#endif /*__BYTE_ORDER*/
int frontier_n2h_i32(const void* p){return n2h_i32(p);}
float frontier_n2h_f32(const void* p){return n2h_f32(p);}
long long frontier_n2h_i64(const void* p){return n2h_i64(p);}
double frontier_n2h_d64(const void* p){return n2h_d64(p);}
/*this function is for backward compatibility*/
FrontierRSBlob *frontierRSBlob_get(FrontierChannel u_channel,int n,int *ec)
{
return frontierRSBlob_open(u_channel,0,n,ec);
}
FrontierRSBlob *frontierRSBlob_open(FrontierChannel u_channel,FrontierRSBlob *oldfrs,int n,int *ec)
{
Channel *chn=(Channel*)u_channel;
FrontierResponse *resp;
FrontierPayload *fp;
FrontierRSBlob *frs=(void*)0;
RSBlob *rs=(void*)0;
RSBlob *oldrs=(RSBlob *)oldfrs;
if(oldrs&&(oldrs->resp))
{
/*move the response from the old RSblob to here*/
resp=oldrs->resp;
oldrs->resp=0;
}
else
{
/*move the response from the channel to here*/
resp=chn->resp;
chn->resp=0;
}
if(n>resp->payload_num)
{
*ec=FRONTIER_EIARG;
frontier_setErrorMsg(__FILE__,__LINE__,"no such payload - total %d, requested %d",resp->payload_num,n);
if(resp)frontierResponse_delete(resp);
return frs;
}
frontier_log(FRONTIER_LOGLEVEL_DEBUG,__FILE__,__LINE__,"parsing chan %d response %d payload %d",chn->seqnum,resp->seqnum,n);
fp=resp->payload[n-1];
rs=frontier_mem_alloc(sizeof(RSBlob));
frs=(FrontierRSBlob *)rs;
if(!frs)
{
*ec=FRONTIER_EMEM;
FRONTIER_MSG(*ec);
if(resp)frontierResponse_delete(resp);
return frs;
}
rs->resp=resp;
rs->buf=fp->blob;
rs->size=fp->blob_size;
rs->pos=0;
rs->nrec=fp->nrec;
rs->respnum=resp->seqnum; // for debugging
rs->payload_error=fp->error_code;
rs->payload_msg=fp->error_msg;
*ec=FRONTIER_OK;
return frs;
}
void frontierRSBlob_close(FrontierRSBlob *frs,int *ec)
{
RSBlob *rs=(RSBlob *)frs;
*ec=FRONTIER_OK;
if(!rs) return;
if(rs->resp)frontierResponse_delete(rs->resp);
frontier_mem_free(rs);
}
void frontierRSBlob_rsctl(FrontierRSBlob *frs,int ctl_fn,void *data,int size,int *ec)
{
*ec=FRONTIER_OK;
}
void frontierRSBlob_start(FrontierRSBlob *frs,int *ec)
{
RSBlob* rs=(RSBlob *)frs;
*ec=FRONTIER_OK;
rs->pos=0;
}
char frontierRSBlob_getByte(FrontierRSBlob *frs,int *ec)
{
RSBlob* rs=(RSBlob *)frs;
char ret;
if(rs->pos>=rs->size)
{
*ec=FRONTIER_EIARG;
frontier_setErrorMsg(__FILE__,__LINE__,"resultset size (%d bytes) violation",rs->size);
return (char)-1;
}
ret=rs->buf[rs->pos];
rs->pos++;
*ec=FRONTIER_OK;
return ret;
}
char frontierRSBlob_checkByte(FrontierRSBlob *frs,int *ec)
{
RSBlob* rs=(RSBlob *)frs;
char ret;
if(rs->pos>=rs->size)
{
*ec=FRONTIER_EIARG;
frontier_setErrorMsg(__FILE__,__LINE__,"resultset size (%d bytes) violation",rs->size);
return (char)-1;
}
ret=rs->buf[rs->pos];
*ec=FRONTIER_OK;
return ret;
}
char *frontierRSBlob_getByteArray(FrontierRSBlob *frs,unsigned int len,int *ec)
{
RSBlob* rs=(RSBlob *)frs;
const unsigned char *buf;
if(rs->pos>=rs->size-(len-1))
{
*ec=FRONTIER_EIARG;
frontier_setErrorMsg(__FILE__,__LINE__,"resultset size (%d bytes) violation",rs->size);
return 0;
}
buf=rs->buf+rs->pos;
rs->pos+=len;
*ec=FRONTIER_OK;
return (char *)buf;
}
int frontierRSBlob_getInt(FrontierRSBlob *frs,int *ec)
{
RSBlob* rs=(RSBlob *)frs;
int ret;
if(rs->pos>=rs->size-3)
{
*ec=FRONTIER_EIARG;
frontier_setErrorMsg(__FILE__,__LINE__,"resultset size (%d bytes) violation",rs->size);
return -1;
}
ret=n2h_i32(rs->buf+rs->pos);
rs->pos+=4;
*ec=FRONTIER_OK;
return ret;
}
long long frontierRSBlob_getLong(FrontierRSBlob *frs,int *ec)
{
RSBlob* rs=(RSBlob *)frs;
long long ret;
if(rs->pos>=rs->size-7)
{
*ec=FRONTIER_EIARG;
frontier_setErrorMsg(__FILE__,__LINE__,"resultset size (%d bytes) violation",rs->size);
return -1;
}
ret=n2h_i64(rs->buf+rs->pos);
rs->pos+=8;
*ec=FRONTIER_OK;
return ret;
}
double frontierRSBlob_getDouble(FrontierRSBlob *frs,int *ec)
{
RSBlob* rs=(RSBlob *)frs;
double ret;
if(rs->pos>=rs->size-7)
{
*ec=FRONTIER_EIARG;
frontier_setErrorMsg(__FILE__,__LINE__,"resultset size (%d bytes) violation",rs->size);
return -1;
}
ret=n2h_d64(rs->buf+rs->pos);
rs->pos+=8;
*ec=FRONTIER_OK;
return ret;
}
float frontierRSBlob_getFloat(FrontierRSBlob *frs,int *ec)
{
RSBlob* rs=(RSBlob *)frs;
float ret;
if(rs->pos>=rs->size-3)
{
*ec=FRONTIER_EIARG;
frontier_setErrorMsg(__FILE__,__LINE__,"resultset size (%d bytes) violation",rs->size);
return -1;
}
ret=n2h_f32(rs->buf+rs->pos);
rs->pos+=4;
*ec=FRONTIER_OK;
return ret;
}
unsigned int frontierRSBlob_getRecNum(FrontierRSBlob *frs)
{
RSBlob* rs=(RSBlob *)frs;
return rs->nrec;
}
unsigned int frontierRSBlob_getPos(FrontierRSBlob *frs)
{
RSBlob* rs=(RSBlob *)frs;
return rs->pos;
}
unsigned int frontierRSBlob_getSize(FrontierRSBlob *frs)
{
RSBlob* rs=(RSBlob *)frs;
return rs->size;
}
int frontierRSBlob_payload_error(FrontierRSBlob *frs)
{
RSBlob* rs=(RSBlob *)frs;
return rs->payload_error;
}
const char *frontierRSBlob_payload_msg(FrontierRSBlob *frs)
{
RSBlob* rs=(RSBlob *)frs;
return rs->payload_msg;
}