~mshadle/php-fpm/trunk

« back to all changes in this revision

Viewing changes to libevent/test/regress.gen.c

  • Committer: root@local
  • Date: 2009-07-10 01:43:03 UTC
  • Revision ID: root@local-20090710014303-434wsx93fg1kl7b1
initial import, based off 0.6.0.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Automatically generated from ./regress.rpc
 
3
 * by event_rpcgen.py/0.1.  DO NOT EDIT THIS FILE.
 
4
 */
 
5
 
 
6
#include <sys/types.h>
 
7
#include <sys/time.h>
 
8
#include <stdlib.h>
 
9
#include <string.h>
 
10
#include <assert.h>
 
11
#include <event.h>
 
12
 
 
13
 
 
14
#include "./regress.gen.h"
 
15
 
 
16
void event_err(int eval, const char *fmt, ...);
 
17
void event_warn(const char *fmt, ...);
 
18
void event_errx(int eval, const char *fmt, ...);
 
19
void event_warnx(const char *fmt, ...);
 
20
 
 
21
 
 
22
/*
 
23
 * Implementation of msg
 
24
 */
 
25
 
 
26
static struct msg_access_ __msg_base = {
 
27
  msg_from_name_assign,
 
28
  msg_from_name_get,
 
29
  msg_to_name_assign,
 
30
  msg_to_name_get,
 
31
  msg_attack_assign,
 
32
  msg_attack_get,
 
33
  msg_run_assign,
 
34
  msg_run_get,
 
35
  msg_run_add,
 
36
};
 
37
 
 
38
struct msg *
 
39
msg_new(void)
 
40
{
 
41
  struct msg *tmp;
 
42
  if ((tmp = malloc(sizeof(struct msg))) == NULL) {
 
43
    event_warn("%s: malloc", __func__);
 
44
    return (NULL);
 
45
  }
 
46
  tmp->base = &__msg_base;
 
47
 
 
48
  tmp->from_name_data = NULL;
 
49
  tmp->from_name_set = 0;
 
50
 
 
51
  tmp->to_name_data = NULL;
 
52
  tmp->to_name_set = 0;
 
53
 
 
54
  tmp->attack_data = NULL;
 
55
  tmp->attack_set = 0;
 
56
 
 
57
  tmp->run_data = NULL;
 
58
  tmp->run_length = 0;
 
59
  tmp->run_num_allocated = 0;
 
60
  tmp->run_set = 0;
 
61
 
 
62
  return (tmp);
 
63
}
 
64
 
 
65
 
 
66
 
 
67
 
 
68
struct run *
 
69
msg_run_add(struct msg *msg)
 
70
{
 
71
  if (++msg->run_length >= msg->run_num_allocated) {
 
72
    int tobe_allocated = msg->run_num_allocated;
 
73
    struct run ** new_data = NULL;
 
74
    tobe_allocated = !tobe_allocated ? 1 : tobe_allocated << 1;
 
75
    new_data = (struct run **) realloc(msg->run_data,
 
76
        tobe_allocated * sizeof(struct run *));
 
77
    if (new_data == NULL)
 
78
      goto error;
 
79
    msg->run_data = new_data;
 
80
    msg->run_num_allocated = tobe_allocated;
 
81
  }
 
82
  msg->run_data[msg->run_length - 1] = run_new();
 
83
  if (msg->run_data[msg->run_length - 1] == NULL)
 
84
    goto error;
 
85
  msg->run_set = 1;
 
86
  return (msg->run_data[msg->run_length - 1]);
 
87
error:
 
88
  --msg->run_length;
 
89
  return (NULL);
 
90
}
 
91
        
 
92
 
 
93
int
 
94
msg_from_name_assign(struct msg *msg,
 
95
    const char * value)
 
96
{
 
97
  if (msg->from_name_data != NULL)
 
98
    free(msg->from_name_data);
 
99
  if ((msg->from_name_data = strdup(value)) == NULL)
 
100
    return (-1);
 
101
  msg->from_name_set = 1;
 
102
  return (0);
 
103
}
 
104
 
 
105
int
 
106
msg_to_name_assign(struct msg *msg,
 
107
    const char * value)
 
108
{
 
109
  if (msg->to_name_data != NULL)
 
110
    free(msg->to_name_data);
 
111
  if ((msg->to_name_data = strdup(value)) == NULL)
 
112
    return (-1);
 
113
  msg->to_name_set = 1;
 
114
  return (0);
 
115
}
 
116
 
 
117
int
 
118
msg_attack_assign(struct msg *msg,
 
119
    const struct kill* value)
 
120
{
 
121
   struct evbuffer *tmp = NULL;
 
122
   if (msg->attack_set) {
 
123
     kill_clear(msg->attack_data);
 
124
     msg->attack_set = 0;
 
125
   } else {
 
126
     msg->attack_data = kill_new();
 
127
     if (msg->attack_data == NULL) {
 
128
       event_warn("%s: kill_new()", __func__);
 
129
       goto error;
 
130
     }
 
131
   }
 
132
   if ((tmp = evbuffer_new()) == NULL) {
 
133
     event_warn("%s: evbuffer_new()", __func__);
 
134
     goto error;
 
135
   }
 
136
   kill_marshal(tmp, value);
 
137
   if (kill_unmarshal(msg->attack_data, tmp) == -1) {
 
138
     event_warnx("%s: kill_unmarshal", __func__);
 
139
     goto error;
 
140
   }
 
141
   msg->attack_set = 1;
 
142
   evbuffer_free(tmp);
 
143
   return (0);
 
144
 error:
 
145
   if (tmp != NULL)
 
146
     evbuffer_free(tmp);
 
147
   if (msg->attack_data != NULL) {
 
148
     kill_free(msg->attack_data);
 
149
     msg->attack_data = NULL;
 
150
   }
 
151
   return (-1);
 
152
}
 
153
 
 
154
int
 
155
msg_run_assign(struct msg *msg, int off,
 
156
    const struct run * value)
 
157
{
 
158
  struct evbuffer *tmp = NULL;
 
159
  if (!msg->run_set || off < 0 || off >= msg->run_length)
 
160
    return (-1);
 
161
  run_clear(msg->run_data[off]);
 
162
  if ((tmp = evbuffer_new()) == NULL) {
 
163
    event_warn("%s: evbuffer_new()", __func__);
 
164
    goto error;
 
165
  }
 
166
  run_marshal(tmp, value);
 
167
  if (run_unmarshal(msg->run_data[off], tmp) == -1) {
 
168
    event_warnx("%s: run_unmarshal", __func__);
 
169
    goto error;
 
170
  }
 
171
  evbuffer_free(tmp);
 
172
  return (0);
 
173
error:
 
174
  if (tmp != NULL)
 
175
    evbuffer_free(tmp);
 
176
  run_clear(msg->run_data[off]);
 
177
  return (-1);
 
178
}
 
179
 
 
180
int
 
181
msg_from_name_get(struct msg *msg, char * *value)
 
182
{
 
183
  if (msg->from_name_set != 1)
 
184
    return (-1);
 
185
  *value = msg->from_name_data;
 
186
  return (0);
 
187
}
 
188
 
 
189
int
 
190
msg_to_name_get(struct msg *msg, char * *value)
 
191
{
 
192
  if (msg->to_name_set != 1)
 
193
    return (-1);
 
194
  *value = msg->to_name_data;
 
195
  return (0);
 
196
}
 
197
 
 
198
int
 
199
msg_attack_get(struct msg *msg, struct kill* *value)
 
200
{
 
201
  if (msg->attack_set != 1) {
 
202
    msg->attack_data = kill_new();
 
203
    if (msg->attack_data == NULL)
 
204
      return (-1);
 
205
    msg->attack_set = 1;
 
206
  }
 
207
  *value = msg->attack_data;
 
208
  return (0);
 
209
}
 
210
 
 
211
int
 
212
msg_run_get(struct msg *msg, int offset,
 
213
    struct run * *value)
 
214
{
 
215
  if (!msg->run_set || offset < 0 || offset >= msg->run_length)
 
216
    return (-1);
 
217
  *value = msg->run_data[offset];
 
218
  return (0);
 
219
}
 
220
 
 
221
void
 
222
msg_clear(struct msg *tmp)
 
223
{
 
224
  if (tmp->from_name_set == 1) {
 
225
    free (tmp->from_name_data);
 
226
    tmp->from_name_data = NULL;
 
227
    tmp->from_name_set = 0;
 
228
  }
 
229
  if (tmp->to_name_set == 1) {
 
230
    free (tmp->to_name_data);
 
231
    tmp->to_name_data = NULL;
 
232
    tmp->to_name_set = 0;
 
233
  }
 
234
  if (tmp->attack_set == 1) {
 
235
    kill_free(tmp->attack_data);
 
236
    tmp->attack_data = NULL;
 
237
    tmp->attack_set = 0;
 
238
  }
 
239
  if (tmp->run_set == 1) {
 
240
    int i;
 
241
    for (i = 0; i < tmp->run_length; ++i) {
 
242
      run_free(tmp->run_data[i]);
 
243
    }
 
244
    free(tmp->run_data);
 
245
    tmp->run_data = NULL;
 
246
    tmp->run_set = 0;
 
247
    tmp->run_length = 0;
 
248
    tmp->run_num_allocated = 0;
 
249
  }
 
250
}
 
251
 
 
252
void
 
253
msg_free(struct msg *tmp)
 
254
{
 
255
  if (tmp->from_name_data != NULL)
 
256
      free (tmp->from_name_data); 
 
257
  if (tmp->to_name_data != NULL)
 
258
      free (tmp->to_name_data); 
 
259
  if (tmp->attack_data != NULL)
 
260
      kill_free(tmp->attack_data); 
 
261
  if (tmp->run_data != NULL) {
 
262
    int i;
 
263
    for (i = 0; i < tmp->run_length; ++i) {
 
264
      run_free(tmp->run_data[i]); 
 
265
      tmp->run_data[i] = NULL;
 
266
    }
 
267
    free(tmp->run_data);
 
268
    tmp->run_data = NULL;
 
269
    tmp->run_length = 0;
 
270
    tmp->run_num_allocated = 0;
 
271
  }
 
272
  free(tmp);
 
273
}
 
274
 
 
275
void
 
276
msg_marshal(struct evbuffer *evbuf, const struct msg *tmp){
 
277
  evtag_marshal_string(evbuf, MSG_FROM_NAME, tmp->from_name_data);
 
278
  evtag_marshal_string(evbuf, MSG_TO_NAME, tmp->to_name_data);
 
279
  if (tmp->attack_set) {
 
280
    evtag_marshal_kill(evbuf, MSG_ATTACK, tmp->attack_data);
 
281
  }
 
282
  {
 
283
    int i;
 
284
    for (i = 0; i < tmp->run_length; ++i) {
 
285
      evtag_marshal_run(evbuf, MSG_RUN, tmp->run_data[i]);
 
286
    }
 
287
  }
 
288
}
 
289
 
 
290
int
 
291
msg_unmarshal(struct msg *tmp,  struct evbuffer *evbuf)
 
292
{
 
293
  uint32_t tag;
 
294
  while (EVBUFFER_LENGTH(evbuf) > 0) {
 
295
    if (evtag_peek(evbuf, &tag) == -1)
 
296
      return (-1);
 
297
    switch (tag) {
 
298
 
 
299
      case MSG_FROM_NAME:
 
300
 
 
301
        if (tmp->from_name_set)
 
302
          return (-1);
 
303
        if (evtag_unmarshal_string(evbuf, MSG_FROM_NAME, &tmp->from_name_data) == -1) {
 
304
          event_warnx("%s: failed to unmarshal from_name", __func__);
 
305
          return (-1);
 
306
        }
 
307
        tmp->from_name_set = 1;
 
308
        break;
 
309
 
 
310
      case MSG_TO_NAME:
 
311
 
 
312
        if (tmp->to_name_set)
 
313
          return (-1);
 
314
        if (evtag_unmarshal_string(evbuf, MSG_TO_NAME, &tmp->to_name_data) == -1) {
 
315
          event_warnx("%s: failed to unmarshal to_name", __func__);
 
316
          return (-1);
 
317
        }
 
318
        tmp->to_name_set = 1;
 
319
        break;
 
320
 
 
321
      case MSG_ATTACK:
 
322
 
 
323
        if (tmp->attack_set)
 
324
          return (-1);
 
325
        tmp->attack_data = kill_new();
 
326
        if (tmp->attack_data == NULL)
 
327
          return (-1);
 
328
        if (evtag_unmarshal_kill(evbuf, MSG_ATTACK, tmp->attack_data) == -1) {
 
329
          event_warnx("%s: failed to unmarshal attack", __func__);
 
330
          return (-1);
 
331
        }
 
332
        tmp->attack_set = 1;
 
333
        break;
 
334
 
 
335
      case MSG_RUN:
 
336
 
 
337
        if (msg_run_add(tmp) == NULL)
 
338
          return (-1);
 
339
        if (evtag_unmarshal_run(evbuf, MSG_RUN,
 
340
          tmp->run_data[tmp->run_length - 1]) == -1) {
 
341
          --tmp->run_length;
 
342
          event_warnx("%s: failed to unmarshal run", __func__);
 
343
          return (-1);
 
344
        }
 
345
        tmp->run_set = 1;
 
346
        break;
 
347
 
 
348
      default:
 
349
        return -1;
 
350
    }
 
351
  }
 
352
 
 
353
  if (msg_complete(tmp) == -1)
 
354
    return (-1);
 
355
  return (0);
 
356
}
 
357
 
 
358
int
 
359
msg_complete(struct msg *msg)
 
360
{
 
361
  if (!msg->from_name_set)
 
362
    return (-1);
 
363
  if (!msg->to_name_set)
 
364
    return (-1);
 
365
  if (msg->attack_set && kill_complete(msg->attack_data) == -1)
 
366
    return (-1);
 
367
  {
 
368
    int i;
 
369
    for (i = 0; i < msg->run_length; ++i) {
 
370
      if (run_complete(msg->run_data[i]) == -1)
 
371
        return (-1);
 
372
    }
 
373
  }
 
374
  return (0);
 
375
}
 
376
 
 
377
int
 
378
evtag_unmarshal_msg(struct evbuffer *evbuf, uint32_t need_tag, struct msg *msg)
 
379
{
 
380
  uint32_t tag;
 
381
  int res = -1;
 
382
 
 
383
  struct evbuffer *tmp = evbuffer_new();
 
384
 
 
385
  if (evtag_unmarshal(evbuf, &tag, tmp) == -1 || tag != need_tag)
 
386
    goto error;
 
387
 
 
388
  if (msg_unmarshal(msg, tmp) == -1)
 
389
    goto error;
 
390
 
 
391
  res = 0;
 
392
 
 
393
 error:
 
394
  evbuffer_free(tmp);
 
395
  return (res);
 
396
}
 
397
 
 
398
void
 
399
evtag_marshal_msg(struct evbuffer *evbuf, uint32_t tag, const struct msg *msg)
 
400
{
 
401
  struct evbuffer *_buf = evbuffer_new();
 
402
  assert(_buf != NULL);
 
403
  evbuffer_drain(_buf, -1);
 
404
  msg_marshal(_buf, msg);
 
405
  evtag_marshal(evbuf, tag, EVBUFFER_DATA(_buf), EVBUFFER_LENGTH(_buf));
 
406
  evbuffer_free(_buf);
 
407
}
 
408
 
 
409
/*
 
410
 * Implementation of kill
 
411
 */
 
412
 
 
413
static struct kill_access_ __kill_base = {
 
414
  kill_weapon_assign,
 
415
  kill_weapon_get,
 
416
  kill_action_assign,
 
417
  kill_action_get,
 
418
  kill_how_often_assign,
 
419
  kill_how_often_get,
 
420
};
 
421
 
 
422
struct kill *
 
423
kill_new(void)
 
424
{
 
425
  struct kill *tmp;
 
426
  if ((tmp = malloc(sizeof(struct kill))) == NULL) {
 
427
    event_warn("%s: malloc", __func__);
 
428
    return (NULL);
 
429
  }
 
430
  tmp->base = &__kill_base;
 
431
 
 
432
  tmp->weapon_data = NULL;
 
433
  tmp->weapon_set = 0;
 
434
 
 
435
  tmp->action_data = NULL;
 
436
  tmp->action_set = 0;
 
437
 
 
438
  tmp->how_often_data = 0;
 
439
  tmp->how_often_set = 0;
 
440
 
 
441
  return (tmp);
 
442
}
 
443
 
 
444
 
 
445
 
 
446
 
 
447
int
 
448
kill_weapon_assign(struct kill *msg,
 
449
    const char * value)
 
450
{
 
451
  if (msg->weapon_data != NULL)
 
452
    free(msg->weapon_data);
 
453
  if ((msg->weapon_data = strdup(value)) == NULL)
 
454
    return (-1);
 
455
  msg->weapon_set = 1;
 
456
  return (0);
 
457
}
 
458
 
 
459
int
 
460
kill_action_assign(struct kill *msg,
 
461
    const char * value)
 
462
{
 
463
  if (msg->action_data != NULL)
 
464
    free(msg->action_data);
 
465
  if ((msg->action_data = strdup(value)) == NULL)
 
466
    return (-1);
 
467
  msg->action_set = 1;
 
468
  return (0);
 
469
}
 
470
 
 
471
int
 
472
kill_how_often_assign(struct kill *msg, const uint32_t value)
 
473
{
 
474
  msg->how_often_set = 1;
 
475
  msg->how_often_data = value;
 
476
  return (0);
 
477
}
 
478
 
 
479
int
 
480
kill_weapon_get(struct kill *msg, char * *value)
 
481
{
 
482
  if (msg->weapon_set != 1)
 
483
    return (-1);
 
484
  *value = msg->weapon_data;
 
485
  return (0);
 
486
}
 
487
 
 
488
int
 
489
kill_action_get(struct kill *msg, char * *value)
 
490
{
 
491
  if (msg->action_set != 1)
 
492
    return (-1);
 
493
  *value = msg->action_data;
 
494
  return (0);
 
495
}
 
496
 
 
497
int
 
498
kill_how_often_get(struct kill *msg, uint32_t *value)
 
499
{
 
500
  if (msg->how_often_set != 1)
 
501
    return (-1);
 
502
  *value = msg->how_often_data;
 
503
  return (0);
 
504
}
 
505
 
 
506
void
 
507
kill_clear(struct kill *tmp)
 
508
{
 
509
  if (tmp->weapon_set == 1) {
 
510
    free (tmp->weapon_data);
 
511
    tmp->weapon_data = NULL;
 
512
    tmp->weapon_set = 0;
 
513
  }
 
514
  if (tmp->action_set == 1) {
 
515
    free (tmp->action_data);
 
516
    tmp->action_data = NULL;
 
517
    tmp->action_set = 0;
 
518
  }
 
519
  tmp->how_often_set = 0;
 
520
}
 
521
 
 
522
void
 
523
kill_free(struct kill *tmp)
 
524
{
 
525
  if (tmp->weapon_data != NULL)
 
526
      free (tmp->weapon_data); 
 
527
  if (tmp->action_data != NULL)
 
528
      free (tmp->action_data); 
 
529
  free(tmp);
 
530
}
 
531
 
 
532
void
 
533
kill_marshal(struct evbuffer *evbuf, const struct kill *tmp){
 
534
  evtag_marshal_string(evbuf, KILL_WEAPON, tmp->weapon_data);
 
535
  evtag_marshal_string(evbuf, KILL_ACTION, tmp->action_data);
 
536
  if (tmp->how_often_set) {
 
537
    evtag_marshal_int(evbuf, KILL_HOW_OFTEN, tmp->how_often_data);
 
538
  }
 
539
}
 
540
 
 
541
int
 
542
kill_unmarshal(struct kill *tmp,  struct evbuffer *evbuf)
 
543
{
 
544
  uint32_t tag;
 
545
  while (EVBUFFER_LENGTH(evbuf) > 0) {
 
546
    if (evtag_peek(evbuf, &tag) == -1)
 
547
      return (-1);
 
548
    switch (tag) {
 
549
 
 
550
      case KILL_WEAPON:
 
551
 
 
552
        if (tmp->weapon_set)
 
553
          return (-1);
 
554
        if (evtag_unmarshal_string(evbuf, KILL_WEAPON, &tmp->weapon_data) == -1) {
 
555
          event_warnx("%s: failed to unmarshal weapon", __func__);
 
556
          return (-1);
 
557
        }
 
558
        tmp->weapon_set = 1;
 
559
        break;
 
560
 
 
561
      case KILL_ACTION:
 
562
 
 
563
        if (tmp->action_set)
 
564
          return (-1);
 
565
        if (evtag_unmarshal_string(evbuf, KILL_ACTION, &tmp->action_data) == -1) {
 
566
          event_warnx("%s: failed to unmarshal action", __func__);
 
567
          return (-1);
 
568
        }
 
569
        tmp->action_set = 1;
 
570
        break;
 
571
 
 
572
      case KILL_HOW_OFTEN:
 
573
 
 
574
        if (tmp->how_often_set)
 
575
          return (-1);
 
576
        if (evtag_unmarshal_int(evbuf, KILL_HOW_OFTEN, &tmp->how_often_data) == -1) {
 
577
          event_warnx("%s: failed to unmarshal how_often", __func__);
 
578
          return (-1);
 
579
        }
 
580
        tmp->how_often_set = 1;
 
581
        break;
 
582
 
 
583
      default:
 
584
        return -1;
 
585
    }
 
586
  }
 
587
 
 
588
  if (kill_complete(tmp) == -1)
 
589
    return (-1);
 
590
  return (0);
 
591
}
 
592
 
 
593
int
 
594
kill_complete(struct kill *msg)
 
595
{
 
596
  if (!msg->weapon_set)
 
597
    return (-1);
 
598
  if (!msg->action_set)
 
599
    return (-1);
 
600
  return (0);
 
601
}
 
602
 
 
603
int
 
604
evtag_unmarshal_kill(struct evbuffer *evbuf, uint32_t need_tag, struct kill *msg)
 
605
{
 
606
  uint32_t tag;
 
607
  int res = -1;
 
608
 
 
609
  struct evbuffer *tmp = evbuffer_new();
 
610
 
 
611
  if (evtag_unmarshal(evbuf, &tag, tmp) == -1 || tag != need_tag)
 
612
    goto error;
 
613
 
 
614
  if (kill_unmarshal(msg, tmp) == -1)
 
615
    goto error;
 
616
 
 
617
  res = 0;
 
618
 
 
619
 error:
 
620
  evbuffer_free(tmp);
 
621
  return (res);
 
622
}
 
623
 
 
624
void
 
625
evtag_marshal_kill(struct evbuffer *evbuf, uint32_t tag, const struct kill *msg)
 
626
{
 
627
  struct evbuffer *_buf = evbuffer_new();
 
628
  assert(_buf != NULL);
 
629
  evbuffer_drain(_buf, -1);
 
630
  kill_marshal(_buf, msg);
 
631
  evtag_marshal(evbuf, tag, EVBUFFER_DATA(_buf), EVBUFFER_LENGTH(_buf));
 
632
  evbuffer_free(_buf);
 
633
}
 
634
 
 
635
/*
 
636
 * Implementation of run
 
637
 */
 
638
 
 
639
static struct run_access_ __run_base = {
 
640
  run_how_assign,
 
641
  run_how_get,
 
642
  run_some_bytes_assign,
 
643
  run_some_bytes_get,
 
644
  run_fixed_bytes_assign,
 
645
  run_fixed_bytes_get,
 
646
};
 
647
 
 
648
struct run *
 
649
run_new(void)
 
650
{
 
651
  struct run *tmp;
 
652
  if ((tmp = malloc(sizeof(struct run))) == NULL) {
 
653
    event_warn("%s: malloc", __func__);
 
654
    return (NULL);
 
655
  }
 
656
  tmp->base = &__run_base;
 
657
 
 
658
  tmp->how_data = NULL;
 
659
  tmp->how_set = 0;
 
660
 
 
661
  tmp->some_bytes_data = NULL;
 
662
  tmp->some_bytes_length = 0;
 
663
  tmp->some_bytes_set = 0;
 
664
 
 
665
  memset(tmp->fixed_bytes_data, 0, sizeof(tmp->fixed_bytes_data));
 
666
  tmp->fixed_bytes_set = 0;
 
667
 
 
668
  return (tmp);
 
669
}
 
670
 
 
671
 
 
672
 
 
673
 
 
674
int
 
675
run_how_assign(struct run *msg,
 
676
    const char * value)
 
677
{
 
678
  if (msg->how_data != NULL)
 
679
    free(msg->how_data);
 
680
  if ((msg->how_data = strdup(value)) == NULL)
 
681
    return (-1);
 
682
  msg->how_set = 1;
 
683
  return (0);
 
684
}
 
685
 
 
686
int
 
687
run_some_bytes_assign(struct run *msg, const uint8_t * value, uint32_t len)
 
688
{
 
689
  if (msg->some_bytes_data != NULL)
 
690
    free (msg->some_bytes_data);
 
691
  msg->some_bytes_data = malloc(len);
 
692
  if (msg->some_bytes_data == NULL)
 
693
    return (-1);
 
694
  msg->some_bytes_set = 1;
 
695
  msg->some_bytes_length = len;
 
696
  memcpy(msg->some_bytes_data, value, len);
 
697
  return (0);
 
698
}
 
699
 
 
700
int
 
701
run_fixed_bytes_assign(struct run *msg, const uint8_t *value)
 
702
{
 
703
  msg->fixed_bytes_set = 1;
 
704
  memcpy(msg->fixed_bytes_data, value, 24);
 
705
  return (0);
 
706
}
 
707
 
 
708
int
 
709
run_how_get(struct run *msg, char * *value)
 
710
{
 
711
  if (msg->how_set != 1)
 
712
    return (-1);
 
713
  *value = msg->how_data;
 
714
  return (0);
 
715
}
 
716
 
 
717
int
 
718
run_some_bytes_get(struct run *msg, uint8_t * *value, uint32_t *plen)
 
719
{
 
720
  if (msg->some_bytes_set != 1)
 
721
    return (-1);
 
722
  *value = msg->some_bytes_data;
 
723
  *plen = msg->some_bytes_length;
 
724
  return (0);
 
725
}
 
726
 
 
727
int
 
728
run_fixed_bytes_get(struct run *msg, uint8_t **value)
 
729
{
 
730
  if (msg->fixed_bytes_set != 1)
 
731
    return (-1);
 
732
  *value = msg->fixed_bytes_data;
 
733
  return (0);
 
734
}
 
735
 
 
736
void
 
737
run_clear(struct run *tmp)
 
738
{
 
739
  if (tmp->how_set == 1) {
 
740
    free (tmp->how_data);
 
741
    tmp->how_data = NULL;
 
742
    tmp->how_set = 0;
 
743
  }
 
744
  if (tmp->some_bytes_set == 1) {
 
745
    free (tmp->some_bytes_data);
 
746
    tmp->some_bytes_data = NULL;
 
747
    tmp->some_bytes_length = 0;
 
748
    tmp->some_bytes_set = 0;
 
749
  }
 
750
  tmp->fixed_bytes_set = 0;
 
751
  memset(tmp->fixed_bytes_data, 0, sizeof(tmp->fixed_bytes_data));
 
752
}
 
753
 
 
754
void
 
755
run_free(struct run *tmp)
 
756
{
 
757
  if (tmp->how_data != NULL)
 
758
      free (tmp->how_data); 
 
759
  if (tmp->some_bytes_data != NULL)
 
760
      free (tmp->some_bytes_data); 
 
761
  free(tmp);
 
762
}
 
763
 
 
764
void
 
765
run_marshal(struct evbuffer *evbuf, const struct run *tmp){
 
766
  evtag_marshal_string(evbuf, RUN_HOW, tmp->how_data);
 
767
  if (tmp->some_bytes_set) {
 
768
    evtag_marshal(evbuf, RUN_SOME_BYTES, tmp->some_bytes_data, tmp->some_bytes_length);
 
769
  }
 
770
  evtag_marshal(evbuf, RUN_FIXED_BYTES, tmp->fixed_bytes_data, sizeof(tmp->fixed_bytes_data));
 
771
}
 
772
 
 
773
int
 
774
run_unmarshal(struct run *tmp,  struct evbuffer *evbuf)
 
775
{
 
776
  uint32_t tag;
 
777
  while (EVBUFFER_LENGTH(evbuf) > 0) {
 
778
    if (evtag_peek(evbuf, &tag) == -1)
 
779
      return (-1);
 
780
    switch (tag) {
 
781
 
 
782
      case RUN_HOW:
 
783
 
 
784
        if (tmp->how_set)
 
785
          return (-1);
 
786
        if (evtag_unmarshal_string(evbuf, RUN_HOW, &tmp->how_data) == -1) {
 
787
          event_warnx("%s: failed to unmarshal how", __func__);
 
788
          return (-1);
 
789
        }
 
790
        tmp->how_set = 1;
 
791
        break;
 
792
 
 
793
      case RUN_SOME_BYTES:
 
794
 
 
795
        if (tmp->some_bytes_set)
 
796
          return (-1);
 
797
        if (evtag_payload_length(evbuf, &tmp->some_bytes_length) == -1)
 
798
          return (-1);
 
799
        if (tmp->some_bytes_length > EVBUFFER_LENGTH(evbuf))
 
800
          return (-1);
 
801
        if ((tmp->some_bytes_data = malloc(tmp->some_bytes_length)) == NULL)
 
802
          return (-1);
 
803
        if (evtag_unmarshal_fixed(evbuf, RUN_SOME_BYTES, tmp->some_bytes_data, tmp->some_bytes_length) == -1) {
 
804
          event_warnx("%s: failed to unmarshal some_bytes", __func__);
 
805
          return (-1);
 
806
        }
 
807
        tmp->some_bytes_set = 1;
 
808
        break;
 
809
 
 
810
      case RUN_FIXED_BYTES:
 
811
 
 
812
        if (tmp->fixed_bytes_set)
 
813
          return (-1);
 
814
        if (evtag_unmarshal_fixed(evbuf, RUN_FIXED_BYTES, tmp->fixed_bytes_data, sizeof(tmp->fixed_bytes_data)) == -1) {
 
815
          event_warnx("%s: failed to unmarshal fixed_bytes", __func__);
 
816
          return (-1);
 
817
        }
 
818
        tmp->fixed_bytes_set = 1;
 
819
        break;
 
820
 
 
821
      default:
 
822
        return -1;
 
823
    }
 
824
  }
 
825
 
 
826
  if (run_complete(tmp) == -1)
 
827
    return (-1);
 
828
  return (0);
 
829
}
 
830
 
 
831
int
 
832
run_complete(struct run *msg)
 
833
{
 
834
  if (!msg->how_set)
 
835
    return (-1);
 
836
  if (!msg->fixed_bytes_set)
 
837
    return (-1);
 
838
  return (0);
 
839
}
 
840
 
 
841
int
 
842
evtag_unmarshal_run(struct evbuffer *evbuf, uint32_t need_tag, struct run *msg)
 
843
{
 
844
  uint32_t tag;
 
845
  int res = -1;
 
846
 
 
847
  struct evbuffer *tmp = evbuffer_new();
 
848
 
 
849
  if (evtag_unmarshal(evbuf, &tag, tmp) == -1 || tag != need_tag)
 
850
    goto error;
 
851
 
 
852
  if (run_unmarshal(msg, tmp) == -1)
 
853
    goto error;
 
854
 
 
855
  res = 0;
 
856
 
 
857
 error:
 
858
  evbuffer_free(tmp);
 
859
  return (res);
 
860
}
 
861
 
 
862
void
 
863
evtag_marshal_run(struct evbuffer *evbuf, uint32_t tag, const struct run *msg)
 
864
{
 
865
  struct evbuffer *_buf = evbuffer_new();
 
866
  assert(_buf != NULL);
 
867
  evbuffer_drain(_buf, -1);
 
868
  run_marshal(_buf, msg);
 
869
  evtag_marshal(evbuf, tag, EVBUFFER_DATA(_buf), EVBUFFER_LENGTH(_buf));
 
870
  evbuffer_free(_buf);
 
871
}
 
872