/
mlm_client_engine.inc
2051 lines (1873 loc) · 75.5 KB
/
mlm_client_engine.inc
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* =========================================================================
mlm_client_engine - Malamute Client engine
** WARNING *************************************************************
THIS SOURCE FILE IS 100% GENERATED. If you edit this file, you will lose
your changes at the next build cycle. This is great for temporary printf
statements. DO NOT MAKE ANY CHANGES YOU WISH TO KEEP. The correct places
for commits are:
* The XML model used for this code generation: mlm_client.xml, or
* The code generation script that built this file: zproto_client_c
************************************************************************
Copyright (c) the Contributors as noted in the AUTHORS file.
This file is part of the Malamute Project.
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
=========================================================================
*/
// ---------------------------------------------------------------------------
// State machine constants
typedef enum {
start_state = 1,
connecting_state = 2,
connected_state = 3,
confirming_state = 4,
terminating_state = 5,
reconnecting_state = 6,
disconnected_state = 7,
defaults_state = 8,
have_error_state = 9
} state_t;
typedef enum {
NULL_event = 0,
set_plain_auth_event = 1,
connect_event = 2,
bad_endpoint_event = 3,
destructor_event = 4,
set_producer_event = 5,
set_consumer_event = 6,
set_worker_event = 7,
ok_event = 8,
expired_event = 9,
bad_pattern_event = 10,
stream_deliver_event = 11,
mailbox_deliver_event = 12,
service_deliver_event = 13,
error_event = 14,
connection_pong_event = 15,
replay_ready_event = 16,
heartbeat_event = 17,
command_invalid_event = 18,
failed_event = 19,
other_event = 20
} event_t;
// Names for state machine logging and error reporting
static char *
s_state_name [] = {
"(NONE)",
"start",
"connecting",
"connected",
"confirming",
"terminating",
"reconnecting",
"disconnected",
"defaults",
"have error"
};
static char *
s_event_name [] = {
"(NONE)",
"set_plain_auth",
"connect",
"bad_endpoint",
"destructor",
"set_producer",
"set_consumer",
"set_worker",
"OK",
"expired",
"bad_pattern",
"STREAM_DELIVER",
"MAILBOX_DELIVER",
"SERVICE_DELIVER",
"ERROR",
"CONNECTION_PONG",
"replay_ready",
"heartbeat",
"command_invalid",
"failed",
"other"
};
// ---------------------------------------------------------------------------
// Context for the client. This embeds the application-level client context
// at its start (the entire structure, not a reference), so we can cast a
// pointer between client_t and s_client_t arbitrarily.
// These are the different method arguments we manage automatically
struct _client_args_t {
char *username;
char *password;
char *endpoint;
uint32_t timeout;
char *address;
char *stream;
char *pattern;
};
typedef struct {
client_t client; // Application-level client context
zsock_t *cmdpipe; // Get/send commands from caller API
zsock_t *msgpipe; // Get/send messages from caller API
zsock_t *dealer; // Socket to talk to server
zloop_t *loop; // Listen to pipe and dealer
mlm_proto_t *message; // Message received or sent
client_args_t args; // Method arguments structure
bool verbose; // True if we're tracing state machine
bool connected; // True if client is connected
bool terminated; // True if client is shutdown
bool fsm_stopped; // "terminate" action called
size_t expiry; // Expiry timer, msecs
size_t heartbeat; // Heartbeat timer, msecs
state_t state; // Current state
event_t event; // Current event
event_t next_event; // The next event
event_t exception; // Exception event, if any
int expiry_timer; // zloop timer for expiry
int wakeup_timer; // zloop timer for alarms
int heartbeat_timer; // zloop timer for heartbeat
event_t wakeup_event; // Wake up with this event
char log_prefix [41]; // Log prefix string
} s_client_t;
static int
client_initialize (client_t *self);
static void
client_terminate (client_t *self);
static void
s_client_destroy (s_client_t **self_p);
static void
s_client_execute (s_client_t *self, event_t event);
static int
s_client_handle_wakeup (zloop_t *loop, int timer_id, void *argument);
static int
s_client_handle_heartbeat (zloop_t *loop, int timer_id, void *argument);
static int
s_client_handle_expiry (zloop_t *loop, int timer_id, void *argument);
static void
s_satisfy_pedantic_compilers (void);
static void
use_plain_security_mechanism (client_t *self);
static void
signal_success (client_t *self);
static void
remember_client_address (client_t *self);
static void
connect_to_server_endpoint (client_t *self);
static void
set_client_address (client_t *self);
static void
use_connect_timeout (client_t *self);
static void
signal_bad_endpoint (client_t *self);
static void
signal_failure (client_t *self);
static void
client_is_connected (client_t *self);
static void
signal_server_not_present (client_t *self);
static void
prepare_stream_write_command (client_t *self);
static void
prepare_stream_read_command (client_t *self);
static void
prepare_service_offer_command (client_t *self);
static void
signal_bad_pattern (client_t *self);
static void
pass_stream_message_to_app (client_t *self);
static void
pass_mailbox_message_to_app (client_t *self);
static void
pass_service_message_to_app (client_t *self);
static void
server_has_gone_offline (client_t *self);
static void
check_status_code (client_t *self);
static void
get_first_replay_command (client_t *self);
static void
get_next_replay_command (client_t *self);
static void
announce_unhandled_error (client_t *self);
// Create a new client connection
static s_client_t *
s_client_new (zsock_t *cmdpipe, zsock_t *msgpipe)
{
s_client_t *self = (s_client_t *) zmalloc (sizeof (s_client_t));
if (self) {
assert ((s_client_t *) &self->client == self);
self->cmdpipe = cmdpipe;
self->msgpipe = msgpipe;
self->state = start_state;
self->event = NULL_event;
snprintf (self->log_prefix, sizeof (self->log_prefix),
"%6d:%-33s", randof (1000000), "mlm_client");
self->dealer = zsock_new (ZMQ_DEALER);
if (self->dealer)
self->message = mlm_proto_new ();
if (self->message)
self->loop = zloop_new ();
if (self->loop) {
// Give application chance to initialize and set next event
self->client.cmdpipe = self->cmdpipe;
self->client.msgpipe = self->msgpipe;
self->client.dealer = self->dealer;
self->client.message = self->message;
self->client.args = &self->args;
if (client_initialize (&self->client))
s_client_destroy (&self);
}
else
s_client_destroy (&self);
}
s_satisfy_pedantic_compilers ();
return self;
}
// Destroy the client connection
static void
s_client_destroy (s_client_t **self_p)
{
assert (self_p);
if (*self_p) {
s_client_t *self = *self_p;
zstr_free (&self->args.username);
zstr_free (&self->args.password);
zstr_free (&self->args.endpoint);
zstr_free (&self->args.address);
zstr_free (&self->args.stream);
zstr_free (&self->args.pattern);
client_terminate (&self->client);
mlm_proto_destroy (&self->message);
zsock_destroy (&self->msgpipe);
zsock_destroy (&self->dealer);
zloop_destroy (&self->loop);
free (self);
*self_p = NULL;
}
}
// ---------------------------------------------------------------------------
// These methods are an internal API for actions
// Set the next event, needed in at least one action in an internal
// state; otherwise the state machine will wait for a message on the
// dealer socket and treat that as the event.
static void
engine_set_next_event (client_t *client, event_t event)
{
if (client) {
s_client_t *self = (s_client_t *) client;
self->next_event = event;
}
}
// Raise an exception with 'event', halting any actions in progress.
// Continues execution of actions defined for the exception event.
static void
engine_set_exception (client_t *client, event_t event)
{
if (client) {
s_client_t *self = (s_client_t *) client;
self->exception = event;
}
}
// Set wakeup alarm after 'delay' msecs. The next state should handle the
// wakeup event. The alarm is cancelled on any other event.
static void
engine_set_wakeup_event (client_t *client, size_t delay, event_t event)
{
if (client) {
s_client_t *self = (s_client_t *) client;
if (self->wakeup_timer) {
zloop_timer_end (self->loop, self->wakeup_timer);
self->wakeup_timer = 0;
}
self->wakeup_timer = zloop_timer (
self->loop, delay, 1, s_client_handle_wakeup, self);
self->wakeup_event = event;
}
}
// Set a heartbeat timer. The interval is in msecs and must be
// non-zero. The state machine must handle the "heartbeat" event.
// The heartbeat happens every interval no matter what traffic the
// client is sending or receiving.
static void
engine_set_heartbeat (client_t *client, size_t heartbeat)
{
if (client) {
s_client_t *self = (s_client_t *) client;
self->heartbeat = heartbeat;
if (self->heartbeat_timer) {
zloop_timer_end (self->loop, self->heartbeat_timer);
self->heartbeat_timer = 0;
}
if (self->heartbeat)
self->heartbeat_timer = zloop_timer (
self->loop, self->heartbeat, 1, s_client_handle_heartbeat, self);
}
}
// Set expiry timer. Setting a non-zero expiry causes the state machine
// to receive an "expired" event if there is no incoming traffic for that many
// milliseconds. This cycles over and over until/unless the code sets a
// zero expiry. The state machine must handle the "expired" event.
// Macro to support deprecated name: remove after 2016-07-31
#define engine_set_timeout engine_set_expiry
static void
engine_set_expiry (client_t *client, size_t expiry)
{
if (client) {
s_client_t *self = (s_client_t *) client;
self->expiry = expiry;
if (self->expiry_timer) {
zloop_timer_end (self->loop, self->expiry_timer);
self->expiry_timer = 0;
}
if (self->expiry)
self->expiry_timer = zloop_timer (
self->loop, self->expiry, 1, s_client_handle_expiry, self);
}
}
// Poll socket for activity, invoke handler on any received message.
// Handler must be a CZMQ zloop_fn function; receives client as arg.
static void
engine_handle_socket (client_t *client, zsock_t *sock, zloop_reader_fn handler)
{
if (client && sock) {
s_client_t *self = (s_client_t *) client;
if (handler != NULL) {
int rc = zloop_reader (self->loop, sock, handler, self);
assert (rc == 0);
zloop_reader_set_tolerant (self->loop, sock);
}
else
zloop_reader_end (self->loop, sock);
}
}
// Set connected to true/false. The client must call this if it wants
// to provide the API with the connected status.
static void
engine_set_connected (client_t *client, bool connected)
{
if (client) {
s_client_t *self = (s_client_t *) client;
self->connected = connected;
}
}
// Pedantic compilers don't like unused functions, so we call the whole
// API, passing null references. It's nasty and horrid and sufficient.
static void
s_satisfy_pedantic_compilers (void)
{
engine_set_next_event (NULL, NULL_event);
engine_set_exception (NULL, NULL_event);
engine_set_heartbeat (NULL, 0);
engine_set_expiry (NULL, 0);
engine_set_wakeup_event (NULL, 0, NULL_event);
engine_handle_socket (NULL, 0, NULL);
engine_set_connected (NULL, 0);
}
// ---------------------------------------------------------------------------
// Generic methods on protocol messages
// TODO: replace with lookup table, since ID is one byte
static event_t
s_protocol_event (s_client_t *self, mlm_proto_t *message)
{
assert (message);
switch (mlm_proto_id (message)) {
case MLM_PROTO_CONNECTION_PONG:
return connection_pong_event;
break;
case MLM_PROTO_STREAM_DELIVER:
return stream_deliver_event;
break;
case MLM_PROTO_MAILBOX_DELIVER:
return mailbox_deliver_event;
break;
case MLM_PROTO_SERVICE_DELIVER:
return service_deliver_event;
break;
case MLM_PROTO_OK:
return ok_event;
break;
case MLM_PROTO_ERROR:
return error_event;
break;
default:
zsys_error ("%s: unknown command %s, halting",
self->log_prefix, mlm_proto_command (message));
self->terminated = true;
return NULL_event;
}
}
// Execute state machine as long as we have events; if event is NULL_event,
// or state machine is stopped, do nothing.
static void
s_client_execute (s_client_t *self, event_t event)
{
self->next_event = event;
// Cancel wakeup timer, if any was pending
if (self->wakeup_timer) {
zloop_timer_end (self->loop, self->wakeup_timer);
self->wakeup_timer = 0;
}
while (!self->terminated // Actor is dying
&& !self->fsm_stopped // FSM has finished
&& self->next_event != NULL_event) {
self->event = self->next_event;
self->next_event = NULL_event;
self->exception = NULL_event;
if (self->verbose) {
zsys_debug ("%s: %s:",
self->log_prefix, s_state_name [self->state]);
zsys_debug ("%s: %s",
self->log_prefix, s_event_name [self->event]);
}
switch (self->state) {
case start_state:
if (self->event == set_plain_auth_event) {
if (!self->exception) {
// use plain security mechanism
if (self->verbose)
zsys_debug ("%s: $ use plain security mechanism", self->log_prefix);
use_plain_security_mechanism (&self->client);
}
if (!self->exception) {
// signal success
if (self->verbose)
zsys_debug ("%s: $ signal success", self->log_prefix);
signal_success (&self->client);
}
}
else
if (self->event == connect_event) {
if (!self->exception) {
// remember client address
if (self->verbose)
zsys_debug ("%s: $ remember client address", self->log_prefix);
remember_client_address (&self->client);
}
if (!self->exception) {
// connect to server endpoint
if (self->verbose)
zsys_debug ("%s: $ connect to server endpoint", self->log_prefix);
connect_to_server_endpoint (&self->client);
}
if (!self->exception) {
// set client address
if (self->verbose)
zsys_debug ("%s: $ set client address", self->log_prefix);
set_client_address (&self->client);
}
if (!self->exception) {
// use connect timeout
if (self->verbose)
zsys_debug ("%s: $ use connect timeout", self->log_prefix);
use_connect_timeout (&self->client);
}
if (!self->exception) {
// send CONNECTION_OPEN
if (self->verbose)
zsys_debug ("%s: $ send CONNECTION_OPEN",
self->log_prefix);
mlm_proto_set_id (self->message, MLM_PROTO_CONNECTION_OPEN);
mlm_proto_send (self->message, self->dealer);
}
if (!self->exception)
self->state = connecting_state;
}
else
if (self->event == bad_endpoint_event) {
if (!self->exception) {
// signal bad endpoint
if (self->verbose)
zsys_debug ("%s: $ signal bad endpoint", self->log_prefix);
signal_bad_endpoint (&self->client);
}
}
else
if (self->event == destructor_event) {
if (!self->exception) {
// signal success
if (self->verbose)
zsys_debug ("%s: $ signal success", self->log_prefix);
signal_success (&self->client);
}
if (!self->exception) {
// terminate
if (self->verbose)
zsys_debug ("%s: $ terminate", self->log_prefix);
self->fsm_stopped = true;
}
}
else
if (self->event == set_producer_event) {
if (!self->exception) {
// signal failure
if (self->verbose)
zsys_debug ("%s: $ signal failure", self->log_prefix);
signal_failure (&self->client);
}
}
else
if (self->event == set_consumer_event) {
if (!self->exception) {
// signal failure
if (self->verbose)
zsys_debug ("%s: $ signal failure", self->log_prefix);
signal_failure (&self->client);
}
}
else
if (self->event == set_worker_event) {
if (!self->exception) {
// signal failure
if (self->verbose)
zsys_debug ("%s: $ signal failure", self->log_prefix);
signal_failure (&self->client);
}
}
else {
// Handle unexpected protocol events
}
break;
case connecting_state:
if (self->event == ok_event) {
if (!self->exception) {
// client is connected
if (self->verbose)
zsys_debug ("%s: $ client is connected", self->log_prefix);
client_is_connected (&self->client);
}
if (!self->exception) {
// signal success
if (self->verbose)
zsys_debug ("%s: $ signal success", self->log_prefix);
signal_success (&self->client);
}
if (!self->exception)
self->state = connected_state;
}
else
if (self->event == expired_event) {
if (!self->exception) {
// signal server not present
if (self->verbose)
zsys_debug ("%s: $ signal server not present", self->log_prefix);
signal_server_not_present (&self->client);
}
if (!self->exception)
self->state = start_state;
}
else
if (self->event == heartbeat_event) {
if (!self->exception) {
// send CONNECTION_PING
if (self->verbose)
zsys_debug ("%s: $ send CONNECTION_PING",
self->log_prefix);
mlm_proto_set_id (self->message, MLM_PROTO_CONNECTION_PING);
mlm_proto_send (self->message, self->dealer);
}
}
else
if (self->event == connection_pong_event) {
if (!self->exception) {
// client is connected
if (self->verbose)
zsys_debug ("%s: $ client is connected", self->log_prefix);
client_is_connected (&self->client);
}
if (!self->exception)
self->state = connected_state;
}
else
if (self->event == error_event) {
if (!self->exception) {
// check status code
if (self->verbose)
zsys_debug ("%s: $ check status code", self->log_prefix);
check_status_code (&self->client);
}
if (!self->exception)
self->state = have_error_state;
}
else {
// Handle unexpected protocol events
}
break;
case connected_state:
if (self->event == set_producer_event) {
if (!self->exception) {
// prepare stream write command
if (self->verbose)
zsys_debug ("%s: $ prepare stream write command", self->log_prefix);
prepare_stream_write_command (&self->client);
}
if (!self->exception) {
// send STREAM_WRITE
if (self->verbose)
zsys_debug ("%s: $ send STREAM_WRITE",
self->log_prefix);
mlm_proto_set_id (self->message, MLM_PROTO_STREAM_WRITE);
mlm_proto_send (self->message, self->dealer);
}
if (!self->exception)
self->state = confirming_state;
}
else
if (self->event == set_consumer_event) {
if (!self->exception) {
// prepare stream read command
if (self->verbose)
zsys_debug ("%s: $ prepare stream read command", self->log_prefix);
prepare_stream_read_command (&self->client);
}
if (!self->exception) {
// send STREAM_READ
if (self->verbose)
zsys_debug ("%s: $ send STREAM_READ",
self->log_prefix);
mlm_proto_set_id (self->message, MLM_PROTO_STREAM_READ);
mlm_proto_send (self->message, self->dealer);
}
if (!self->exception)
self->state = confirming_state;
}
else
if (self->event == set_worker_event) {
if (!self->exception) {
// prepare service offer command
if (self->verbose)
zsys_debug ("%s: $ prepare service offer command", self->log_prefix);
prepare_service_offer_command (&self->client);
}
if (!self->exception) {
// send SERVICE_OFFER
if (self->verbose)
zsys_debug ("%s: $ send SERVICE_OFFER",
self->log_prefix);
mlm_proto_set_id (self->message, MLM_PROTO_SERVICE_OFFER);
mlm_proto_send (self->message, self->dealer);
}
if (!self->exception)
self->state = confirming_state;
}
else
if (self->event == destructor_event) {
if (!self->exception) {
// send CONNECTION_CLOSE
if (self->verbose)
zsys_debug ("%s: $ send CONNECTION_CLOSE",
self->log_prefix);
mlm_proto_set_id (self->message, MLM_PROTO_CONNECTION_CLOSE);
mlm_proto_send (self->message, self->dealer);
}
if (!self->exception)
self->state = terminating_state;
}
else
if (self->event == bad_pattern_event) {
if (!self->exception) {
// signal bad pattern
if (self->verbose)
zsys_debug ("%s: $ signal bad pattern", self->log_prefix);
signal_bad_pattern (&self->client);
}
}
else
if (self->event == stream_deliver_event) {
if (!self->exception) {
// pass stream message to app
if (self->verbose)
zsys_debug ("%s: $ pass stream message to app", self->log_prefix);
pass_stream_message_to_app (&self->client);
}
}
else
if (self->event == mailbox_deliver_event) {
if (!self->exception) {
// pass mailbox message to app
if (self->verbose)
zsys_debug ("%s: $ pass mailbox message to app", self->log_prefix);
pass_mailbox_message_to_app (&self->client);
}
}
else
if (self->event == service_deliver_event) {
if (!self->exception) {
// pass service message to app
if (self->verbose)
zsys_debug ("%s: $ pass service message to app", self->log_prefix);
pass_service_message_to_app (&self->client);
}
}
else
if (self->event == heartbeat_event) {
if (!self->exception) {
// send CONNECTION_PING
if (self->verbose)
zsys_debug ("%s: $ send CONNECTION_PING",
self->log_prefix);
mlm_proto_set_id (self->message, MLM_PROTO_CONNECTION_PING);
mlm_proto_send (self->message, self->dealer);
}
}
else
if (self->event == connection_pong_event) {
if (!self->exception) {
// client is connected
if (self->verbose)
zsys_debug ("%s: $ client is connected", self->log_prefix);
client_is_connected (&self->client);
}
if (!self->exception)
self->state = connected_state;
}
else
if (self->event == expired_event) {
if (!self->exception) {
// server has gone offline
if (self->verbose)
zsys_debug ("%s: $ server has gone offline", self->log_prefix);
server_has_gone_offline (&self->client);
}
if (!self->exception)
self->state = disconnected_state;
}
else
if (self->event == error_event) {
if (!self->exception) {
// check status code
if (self->verbose)
zsys_debug ("%s: $ check status code", self->log_prefix);
check_status_code (&self->client);
}
if (!self->exception)
self->state = have_error_state;
}
else {
// Handle unexpected protocol events
}
break;
case confirming_state:
if (self->event == expired_event) {
if (!self->exception) {
// server has gone offline
if (self->verbose)
zsys_debug ("%s: $ server has gone offline", self->log_prefix);
server_has_gone_offline (&self->client);
}
if (!self->exception) {
// signal failure
if (self->verbose)
zsys_debug ("%s: $ signal failure", self->log_prefix);
signal_failure (&self->client);
}
if (!self->exception)
self->state = disconnected_state;
}
else
if (self->event == ok_event) {
if (!self->exception) {
// signal success
if (self->verbose)
zsys_debug ("%s: $ signal success", self->log_prefix);
signal_success (&self->client);
}
if (!self->exception)
self->state = connected_state;
}
else
if (self->event == error_event) {
if (!self->exception) {
// signal failure
if (self->verbose)
zsys_debug ("%s: $ signal failure", self->log_prefix);
signal_failure (&self->client);
}
if (!self->exception) {
// check status code
if (self->verbose)
zsys_debug ("%s: $ check status code", self->log_prefix);
check_status_code (&self->client);
}
if (!self->exception)
self->state = have_error_state;
}
else
if (self->event == connection_pong_event) {
}
else
if (self->event == heartbeat_event) {
if (!self->exception) {
// send CONNECTION_PING
if (self->verbose)
zsys_debug ("%s: $ send CONNECTION_PING",
self->log_prefix);
mlm_proto_set_id (self->message, MLM_PROTO_CONNECTION_PING);
mlm_proto_send (self->message, self->dealer);
}
}
else {
// Handle unexpected protocol events
}
break;
case terminating_state:
if (self->event == ok_event) {
if (!self->exception) {
// signal success
if (self->verbose)
zsys_debug ("%s: $ signal success", self->log_prefix);
signal_success (&self->client);
}
if (!self->exception) {
// terminate
if (self->verbose)
zsys_debug ("%s: $ terminate", self->log_prefix);
self->fsm_stopped = true;
}
}
else
if (self->event == expired_event) {
if (!self->exception) {
// signal failure
if (self->verbose)
zsys_debug ("%s: $ signal failure", self->log_prefix);
signal_failure (&self->client);
}
if (!self->exception) {
// terminate
if (self->verbose)
zsys_debug ("%s: $ terminate", self->log_prefix);
self->fsm_stopped = true;
}
}
else
if (self->event == connection_pong_event) {
}
else
if (self->event == error_event) {
if (!self->exception) {
// signal failure
if (self->verbose)
zsys_debug ("%s: $ signal failure", self->log_prefix);
signal_failure (&self->client);
}
if (!self->exception) {
// terminate
if (self->verbose)
zsys_debug ("%s: $ terminate", self->log_prefix);
self->fsm_stopped = true;
}
}
else
if (self->event == heartbeat_event) {
if (!self->exception) {
// send CONNECTION_PING
if (self->verbose)
zsys_debug ("%s: $ send CONNECTION_PING",
self->log_prefix);
mlm_proto_set_id (self->message, MLM_PROTO_CONNECTION_PING);
mlm_proto_send (self->message, self->dealer);
}
}
else {
// Handle unexpected protocol events
}
break;
case reconnecting_state:
if (self->event == ok_event) {
if (!self->exception) {
// client is connected
if (self->verbose)
zsys_debug ("%s: $ client is connected", self->log_prefix);
client_is_connected (&self->client);
}
if (!self->exception) {
// get first replay command
if (self->verbose)
zsys_debug ("%s: $ get first replay command", self->log_prefix);
get_first_replay_command (&self->client);
}
}
else
if (self->event == error_event) {
}
else
if (self->event == set_producer_event) {
if (!self->exception) {
// send STREAM_WRITE
if (self->verbose)
zsys_debug ("%s: $ send STREAM_WRITE",
self->log_prefix);
mlm_proto_set_id (self->message, MLM_PROTO_STREAM_WRITE);
mlm_proto_send (self->message, self->dealer);
}
if (!self->exception) {
// get next replay command
if (self->verbose)
zsys_debug ("%s: $ get next replay command", self->log_prefix);
get_next_replay_command (&self->client);
}
}
else
if (self->event == set_consumer_event) {
if (!self->exception) {
// send STREAM_READ
if (self->verbose)
zsys_debug ("%s: $ send STREAM_READ",
self->log_prefix);
mlm_proto_set_id (self->message, MLM_PROTO_STREAM_READ);
mlm_proto_send (self->message, self->dealer);
}
if (!self->exception) {
// get next replay command
if (self->verbose)
zsys_debug ("%s: $ get next replay command", self->log_prefix);
get_next_replay_command (&self->client);
}
}
else
if (self->event == set_worker_event) {
if (!self->exception) {
// send SERVICE_OFFER
if (self->verbose)
zsys_debug ("%s: $ send SERVICE_OFFER",
self->log_prefix);
mlm_proto_set_id (self->message, MLM_PROTO_SERVICE_OFFER);
mlm_proto_send (self->message, self->dealer);
}
if (!self->exception) {
// get next replay command
if (self->verbose)
zsys_debug ("%s: $ get next replay command", self->log_prefix);
get_next_replay_command (&self->client);
}
}
else
if (self->event == replay_ready_event) {
if (!self->exception) {
// client is connected
if (self->verbose)
zsys_debug ("%s: $ client is connected", self->log_prefix);
client_is_connected (&self->client);
}
if (!self->exception)
self->state = connected_state;
}