-
Notifications
You must be signed in to change notification settings - Fork 77
/
mlm_server_engine.inc
1615 lines (1517 loc) · 65 KB
/
mlm_server_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_server_engine - mlm_server 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_server.xml, or
* The code generation script that built this file: zproto_server_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/.
=========================================================================
*/
#ifdef NDEBUG
#undef NDEBUG
#endif
#define ZPROTO_UNUSED(object) (void)object
// ---------------------------------------------------------------------------
// State machine constants
typedef enum {
start_state = 1,
connected_state = 2,
defaults_state = 3,
// Internal state to mark zombie clients
zombie_state_ = 4
} state_t;
typedef enum {
NULL_event = 0,
terminate_event = 1,
connection_open_event = 2,
stream_write_event = 3,
stream_read_event = 4,
stream_cancel_event = 5,
stream_send_event = 6,
mailbox_send_event = 7,
service_send_event = 8,
service_offer_event = 9,
confirm_event = 10,
credit_event = 11,
connection_ping_event = 12,
connection_close_event = 13,
stream_message_event = 14,
mailbox_message_event = 15,
service_message_event = 16,
expired_event = 17,
exception_event = 18
} event_t;
// Names for state machine logging and error reporting
static char *
s_state_name [] = {
"(NONE)",
"start",
"connected",
"defaults"
};
static char *
s_event_name [] = {
"(NONE)",
"terminate",
"CONNECTION_OPEN",
"STREAM_WRITE",
"STREAM_READ",
"STREAM_CANCEL",
"STREAM_SEND",
"MAILBOX_SEND",
"SERVICE_SEND",
"SERVICE_OFFER",
"CONFIRM",
"CREDIT",
"CONNECTION_PING",
"CONNECTION_CLOSE",
"stream_message",
"mailbox_message",
"service_message",
"expired",
"exception"
};
// ---------------------------------------------------------------------------
// Context for the whole server task. This embeds the application-level
// server context at its start (the entire structure, not a reference),
// so we can cast a pointer between server_t and s_server_t arbitrarily.
typedef struct {
server_t server; // Application-level server context
zsock_t *pipe; // Socket to back to caller API
zsock_t *router; // Socket to talk to clients
int port; // Server port bound to
zloop_t *loop; // Reactor for server sockets
mlm_proto_t *message; // Message received or sent
zhash_t *clients; // Clients we're connected to
zhash_t *zombies; // Zombie clients waiting to be free()d
zconfig_t *config; // Configuration tree
uint client_id; // Client identifier counter
size_t timeout; // Default client expiry timeout
bool verbose; // Verbose logging enabled?
char *log_prefix; // Default log prefix
} s_server_t;
// ---------------------------------------------------------------------------
// Context for each connected 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.
typedef struct {
client_t client; // Application-level client context
s_server_t *server; // Parent server context
size_t refcount; // Optional reference counter
char *hashkey; // Key into server->clients hash
zframe_t *routing_id; // Routing_id back to client
uint unique_id; // Client identifier in server
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 wakeup; // zloop timer for client alarms
void *ticket; // zloop ticket for client timeouts
event_t wakeup_event; // Wake up with this event
char log_prefix [41]; // Log prefix string
} s_client_t;
static int
server_initialize (server_t *self);
static void
server_terminate (server_t *self);
static zmsg_t *
server_method (server_t *self, const char *method, zmsg_t *msg);
static void
server_configuration (server_t *self, zconfig_t *config);
static int
client_initialize (client_t *self);
static void
client_terminate (client_t *self);
static void
s_server_config_global (s_server_t *server);
static void
s_client_execute (s_client_t *client, event_t event);
static int
s_client_handle_wakeup (zloop_t *loop, int timer_id, void *argument);
static int
s_client_handle_ticket (zloop_t *loop, int timer_id, void *argument);
static void
s_client_finalize (s_client_t **self_p);
static void
register_new_client (client_t *self);
static void
check_for_mailbox_messages (client_t *self);
static void
signal_command_invalid (client_t *self);
static void
store_stream_writer (client_t *self);
static void
store_stream_reader (client_t *self);
static void
cancel_stream_reader (client_t *self);
static void
write_message_to_stream (client_t *self);
static void
write_message_to_mailbox (client_t *self);
static void
write_message_to_service (client_t *self);
static void
store_service_offer (client_t *self);
static void
dispatch_the_service (client_t *self);
static void
have_message_confirmation (client_t *self);
static void
credit_the_client (client_t *self);
static void
client_closed_connection (client_t *self);
static void
deregister_the_client (client_t *self);
static void
get_message_to_deliver (client_t *self);
static void
client_expired (client_t *self);
static void
signal_operation_failed (client_t *self);
static void
client_had_exception (client_t *self);
// ---------------------------------------------------------------------------
// These methods are an internal API for actions
// Optional reference counting. The engine itself does not need this, but if
// the application wishes to delay the final destruction of a client, it
// can do so by using these functions
static void engine_client_get (client_t *client)
{
if (!client)
return;
s_client_t *self = (s_client_t *) client;
++self->refcount;
}
// Relese the application reference to client
static void engine_client_put (client_t *client)
{
if (!client)
return;
s_client_t *self = (s_client_t *) client;
if (--self->refcount == 0 && self->state == zombie_state_) {
zhash_delete (self->server->zombies, self->hashkey);
s_client_finalize (&self);
}
}
#define engine_client_is_valid(c) ((c) && ((s_client_t *)(c))->state != zombie_state_)
// 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
// router socket and treat that as the event.
static void
engine_set_next_event (client_t *client, event_t event)
{
if (engine_client_is_valid (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 (engine_client_is_valid (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 (engine_client_is_valid (client)) {
s_client_t *self = (s_client_t *) client;
if (self->wakeup) {
zloop_timer_end (self->server->loop, self->wakeup);
self->wakeup = 0;
}
self->wakeup = zloop_timer (
self->server->loop, delay, 1, s_client_handle_wakeup, self);
self->wakeup_event = event;
}
}
// Execute 'event' on specified client. Use this to send events to
// other clients. Cancels any wakeup alarm on that client.
static void
engine_send_event (client_t *client, event_t event)
{
if (engine_client_is_valid (client)) {
s_client_t *self = (s_client_t *) client;
s_client_execute (self, event);
}
}
// Execute 'event' on all clients known to the server. If you pass a
// client argument, that client will not receive the broadcast. If you
// want to pass any arguments, store them in the server context.
static void
engine_broadcast_event (server_t *server, client_t *client, event_t event)
{
if (server) {
s_server_t *self = (s_server_t *) server;
zlist_t *keys = zhash_keys (self->clients);
char *key = (char *) zlist_first (keys);
while (key) {
s_client_t *target = (s_client_t *) zhash_lookup (self->clients, key);
if (target != (s_client_t *) client)
s_client_execute (target, event);
key = (char *) zlist_next (keys);
}
zlist_destroy (&keys);
}
}
// Poll actor or zsock for activity, invoke handler on any received
// message. Handler must be a CZMQ zloop_fn function; receives server
// as arg.
static void
engine_handle_socket (server_t *server, void *sock, zloop_reader_fn handler)
{
if (server) {
s_server_t *self = (s_server_t *) server;
// Resolve zactor_t -> zsock_t
if (zactor_is (sock))
sock = zactor_sock ((zactor_t *) sock);
else
assert (zsock_is (sock));
if (handler != NULL) {
int rc = zloop_reader (self->loop, (zsock_t *) sock, handler, self);
assert (rc == 0);
zloop_reader_set_tolerant (self->loop, (zsock_t *) sock);
}
else
zloop_reader_end (self->loop, (zsock_t *) sock);
}
}
// Register monitor function that will be called at regular intervals
// by the server engine. Returns an identifier that can be used to cancel it.
static int
engine_set_monitor (server_t *server, size_t interval, zloop_timer_fn monitor)
{
if (server) {
s_server_t *self = (s_server_t *) server;
int rc = zloop_timer (self->loop, interval, 0, monitor, self);
assert (rc >= 0);
return rc;
}
return -1;
}
// Cancel the monitor function with the given identifier.
static void
engine_cancel_monitor (server_t *server, int identifier)
{
if (server) {
s_server_t *self = (s_server_t *) server;
int rc = zloop_timer_end (self->loop, identifier);
assert (rc >= 0);
}
}
// Set log file prefix; this string will be added to log data, to make
// log data more searchable. The string is truncated to ~20 chars.
static void
engine_set_log_prefix (client_t *client, const char *string)
{
if (engine_client_is_valid (client)) {
s_client_t *self = (s_client_t *) client;
snprintf (self->log_prefix, sizeof (self->log_prefix),
"%6d:%-33s", self->unique_id, string);
}
}
// Set a configuration value in the server's configuration tree. The
// properties this engine uses are: server/verbose, server/timeout, and
// server/background. You can also configure other abitrary properties.
static void
engine_configure (server_t *server, const char *path, const char *value)
{
if (server) {
s_server_t *self = (s_server_t *) server;
zconfig_put (self->config, path, value);
s_server_config_global (self);
}
}
// Return true if server is running in verbose mode, else return false.
static bool
engine_verbose (server_t *server)
{
if (server) {
s_server_t *self = (s_server_t *) server;
return self->verbose;
}
return false;
}
// 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_client_get (NULL);
engine_client_put (NULL);
engine_set_next_event (NULL, NULL_event);
engine_set_exception (NULL, NULL_event);
engine_set_wakeup_event (NULL, 0, NULL_event);
engine_send_event (NULL, NULL_event);
engine_broadcast_event (NULL, NULL, NULL_event);
engine_handle_socket (NULL, 0, NULL);
engine_set_monitor (NULL, 0, NULL);
engine_cancel_monitor (NULL, 0);
engine_set_log_prefix (NULL, NULL);
engine_configure (NULL, NULL, NULL);
engine_verbose (NULL);
}
// ---------------------------------------------------------------------------
// Generic methods on protocol messages
// TODO: replace with lookup table, since ID is one byte
static event_t
s_protocol_event (mlm_proto_t *message)
{
assert (message);
switch (mlm_proto_id (message)) {
case MLM_PROTO_CONNECTION_OPEN:
return connection_open_event;
break;
case MLM_PROTO_CONNECTION_PING:
return connection_ping_event;
break;
case MLM_PROTO_CONNECTION_CLOSE:
return connection_close_event;
break;
case MLM_PROTO_STREAM_WRITE:
return stream_write_event;
break;
case MLM_PROTO_STREAM_READ:
return stream_read_event;
break;
case MLM_PROTO_STREAM_SEND:
return stream_send_event;
break;
case MLM_PROTO_MAILBOX_SEND:
return mailbox_send_event;
break;
case MLM_PROTO_SERVICE_SEND:
return service_send_event;
break;
case MLM_PROTO_SERVICE_OFFER:
return service_offer_event;
break;
case MLM_PROTO_CREDIT:
return credit_event;
break;
case MLM_PROTO_CONFIRM:
return confirm_event;
break;
case MLM_PROTO_STREAM_CANCEL:
return stream_cancel_event;
break;
default:
// Invalid mlm_proto_t
return terminate_event;
}
}
// ---------------------------------------------------------------------------
// Client methods
static s_client_t *
s_client_new (s_server_t *server, zframe_t *routing_id)
{
s_client_t *self = (s_client_t *) zmalloc (sizeof (s_client_t));
assert (self);
assert ((s_client_t *) &self->client == self);
self->server = server;
self->hashkey = zframe_strhex (routing_id);
self->routing_id = zframe_dup (routing_id);
self->unique_id = server->client_id++;
engine_set_log_prefix (&self->client, server->log_prefix);
self->client.server = (server_t *) server;
self->client.message = server->message;
// propagate client identifier
self->client.unique_id = self->unique_id;
// If expiry timers are being used, create client ticket
if (server->timeout)
self->ticket = zloop_ticket (server->loop, s_client_handle_ticket, self);
// Give application chance to initialize and set next event
self->state = start_state;
self->event = NULL_event;
client_initialize (&self->client);
return self;
}
static void
s_client_destroy (s_client_t **self_p)
{
assert (self_p);
if (*self_p) {
s_client_t *self = *self_p;
if (self->wakeup)
zloop_timer_end (self->server->loop, self->wakeup);
if (self->ticket)
zloop_ticket_delete (self->server->loop, self->ticket);
zframe_destroy (&self->routing_id);
// Provide visual clue if application misuses client reference
engine_set_log_prefix (&self->client, "*** TERMINATED ***");
if (!self->refcount) {
s_client_finalize (self_p);
} else {
zhash_insert (self->server->zombies, self->hashkey, self);
self->state = zombie_state_;
}
}
}
static void
s_client_finalize (s_client_t **self_p)
{
s_client_t *self = *self_p;
free (self->hashkey);
client_terminate (&self->client);
free (self);
*self_p = NULL;
}
// Callback when we remove client from 'clients' hash table
static void
s_client_free (void *argument)
{
s_client_t *client = (s_client_t *) argument;
s_client_destroy (&client);
}
// Execute state machine as long as we have events
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) {
zloop_timer_end (self->server->loop, self->wakeup);
self->wakeup = 0;
}
while (self->next_event > 0) {
self->event = self->next_event;
self->next_event = NULL_event;
self->exception = NULL_event;
if (self->server->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 == connection_open_event) {
if (!self->exception) {
// register new client
if (self->server->verbose)
zsys_debug ("%s: $ register new client", self->log_prefix);
register_new_client (&self->client);
}
if (!self->exception) {
// send OK
if (self->server->verbose)
zsys_debug ("%s: $ send OK",
self->log_prefix);
mlm_proto_set_id (self->server->message, MLM_PROTO_OK);
mlm_proto_set_routing_id (self->server->message, self->routing_id);
mlm_proto_send (self->server->message, self->server->router);
}
if (!self->exception) {
// check for mailbox messages
if (self->server->verbose)
zsys_debug ("%s: $ check for mailbox messages", self->log_prefix);
check_for_mailbox_messages (&self->client);
}
if (!self->exception)
self->state = connected_state;
}
else
if (self->event == connection_close_event) {
if (!self->exception) {
// send OK
if (self->server->verbose)
zsys_debug ("%s: $ send OK",
self->log_prefix);
mlm_proto_set_id (self->server->message, MLM_PROTO_OK);
mlm_proto_set_routing_id (self->server->message, self->routing_id);
mlm_proto_send (self->server->message, self->server->router);
}
if (!self->exception) {
// client closed connection
if (self->server->verbose)
zsys_debug ("%s: $ client closed connection", self->log_prefix);
client_closed_connection (&self->client);
}
if (!self->exception) {
// deregister the client
if (self->server->verbose)
zsys_debug ("%s: $ deregister the client", self->log_prefix);
deregister_the_client (&self->client);
}
if (!self->exception) {
// terminate
if (self->server->verbose)
zsys_debug ("%s: $ terminate", self->log_prefix);
self->next_event = terminate_event;
}
}
else
if (self->event == stream_message_event) {
if (!self->exception) {
// get message to deliver
if (self->server->verbose)
zsys_debug ("%s: $ get message to deliver", self->log_prefix);
get_message_to_deliver (&self->client);
}
if (!self->exception) {
// send STREAM_DELIVER
if (self->server->verbose)
zsys_debug ("%s: $ send STREAM_DELIVER",
self->log_prefix);
mlm_proto_set_id (self->server->message, MLM_PROTO_STREAM_DELIVER);
mlm_proto_set_routing_id (self->server->message, self->routing_id);
mlm_proto_send (self->server->message, self->server->router);
}
if (!self->exception)
self->state = connected_state;
}
else
if (self->event == mailbox_message_event) {
if (!self->exception) {
// get message to deliver
if (self->server->verbose)
zsys_debug ("%s: $ get message to deliver", self->log_prefix);
get_message_to_deliver (&self->client);
}
if (!self->exception) {
// send MAILBOX_DELIVER
if (self->server->verbose)
zsys_debug ("%s: $ send MAILBOX_DELIVER",
self->log_prefix);
mlm_proto_set_id (self->server->message, MLM_PROTO_MAILBOX_DELIVER);
mlm_proto_set_routing_id (self->server->message, self->routing_id);
mlm_proto_send (self->server->message, self->server->router);
}
if (!self->exception) {
// check for mailbox messages
if (self->server->verbose)
zsys_debug ("%s: $ check for mailbox messages", self->log_prefix);
check_for_mailbox_messages (&self->client);
}
if (!self->exception)
self->state = connected_state;
}
else
if (self->event == service_message_event) {
if (!self->exception) {
// get message to deliver
if (self->server->verbose)
zsys_debug ("%s: $ get message to deliver", self->log_prefix);
get_message_to_deliver (&self->client);
}
if (!self->exception) {
// send SERVICE_DELIVER
if (self->server->verbose)
zsys_debug ("%s: $ send SERVICE_DELIVER",
self->log_prefix);
mlm_proto_set_id (self->server->message, MLM_PROTO_SERVICE_DELIVER);
mlm_proto_set_routing_id (self->server->message, self->routing_id);
mlm_proto_send (self->server->message, self->server->router);
}
if (!self->exception)
self->state = connected_state;
}
else
if (self->event == expired_event) {
if (!self->exception) {
// client expired
if (self->server->verbose)
zsys_debug ("%s: $ client expired", self->log_prefix);
client_expired (&self->client);
}
if (!self->exception) {
// deregister the client
if (self->server->verbose)
zsys_debug ("%s: $ deregister the client", self->log_prefix);
deregister_the_client (&self->client);
}
if (!self->exception) {
// terminate
if (self->server->verbose)
zsys_debug ("%s: $ terminate", self->log_prefix);
self->next_event = terminate_event;
}
}
else
if (self->event == exception_event) {
if (!self->exception) {
// signal operation failed
if (self->server->verbose)
zsys_debug ("%s: $ signal operation failed", self->log_prefix);
signal_operation_failed (&self->client);
}
if (!self->exception) {
// send ERROR
if (self->server->verbose)
zsys_debug ("%s: $ send ERROR",
self->log_prefix);
mlm_proto_set_id (self->server->message, MLM_PROTO_ERROR);
mlm_proto_set_routing_id (self->server->message, self->routing_id);
mlm_proto_send (self->server->message, self->server->router);
}
if (!self->exception) {
// client had exception
if (self->server->verbose)
zsys_debug ("%s: $ client had exception", self->log_prefix);
client_had_exception (&self->client);
}
if (!self->exception) {
// deregister the client
if (self->server->verbose)
zsys_debug ("%s: $ deregister the client", self->log_prefix);
deregister_the_client (&self->client);
}
if (!self->exception) {
// terminate
if (self->server->verbose)
zsys_debug ("%s: $ terminate", self->log_prefix);
self->next_event = terminate_event;
}
}
else {
// Handle unexpected protocol events
if (!self->exception) {
// signal command invalid
if (self->server->verbose)
zsys_debug ("%s: $ signal command invalid", self->log_prefix);
signal_command_invalid (&self->client);
}
if (!self->exception) {
// send ERROR
if (self->server->verbose)
zsys_debug ("%s: $ send ERROR",
self->log_prefix);
mlm_proto_set_id (self->server->message, MLM_PROTO_ERROR);
mlm_proto_set_routing_id (self->server->message, self->routing_id);
mlm_proto_send (self->server->message, self->server->router);
}
}
break;
case connected_state:
if (self->event == stream_write_event) {
if (!self->exception) {
// store stream writer
if (self->server->verbose)
zsys_debug ("%s: $ store stream writer", self->log_prefix);
store_stream_writer (&self->client);
}
if (!self->exception) {
// send OK
if (self->server->verbose)
zsys_debug ("%s: $ send OK",
self->log_prefix);
mlm_proto_set_id (self->server->message, MLM_PROTO_OK);
mlm_proto_set_routing_id (self->server->message, self->routing_id);
mlm_proto_send (self->server->message, self->server->router);
}
}
else
if (self->event == stream_read_event) {
if (!self->exception) {
// store stream reader
if (self->server->verbose)
zsys_debug ("%s: $ store stream reader", self->log_prefix);
store_stream_reader (&self->client);
}
if (!self->exception) {
// send OK
if (self->server->verbose)
zsys_debug ("%s: $ send OK",
self->log_prefix);
mlm_proto_set_id (self->server->message, MLM_PROTO_OK);
mlm_proto_set_routing_id (self->server->message, self->routing_id);
mlm_proto_send (self->server->message, self->server->router);
}
}
else
if (self->event == stream_cancel_event) {
if (!self->exception) {
// cancel stream reader
if (self->server->verbose)
zsys_debug ("%s: $ cancel stream reader", self->log_prefix);
cancel_stream_reader (&self->client);
}
if (!self->exception) {
// send OK
if (self->server->verbose)
zsys_debug ("%s: $ send OK",
self->log_prefix);
mlm_proto_set_id (self->server->message, MLM_PROTO_OK);
mlm_proto_set_routing_id (self->server->message, self->routing_id);
mlm_proto_send (self->server->message, self->server->router);
}
}
else
if (self->event == stream_send_event) {
if (!self->exception) {
// write message to stream
if (self->server->verbose)
zsys_debug ("%s: $ write message to stream", self->log_prefix);
write_message_to_stream (&self->client);
}
}
else
if (self->event == mailbox_send_event) {
if (!self->exception) {
// write message to mailbox
if (self->server->verbose)
zsys_debug ("%s: $ write message to mailbox", self->log_prefix);
write_message_to_mailbox (&self->client);
}
}
else
if (self->event == service_send_event) {
if (!self->exception) {
// write message to service
if (self->server->verbose)
zsys_debug ("%s: $ write message to service", self->log_prefix);
write_message_to_service (&self->client);
}
}
else
if (self->event == service_offer_event) {
if (!self->exception) {
// store service offer
if (self->server->verbose)
zsys_debug ("%s: $ store service offer", self->log_prefix);
store_service_offer (&self->client);
}
if (!self->exception) {
// send OK
if (self->server->verbose)
zsys_debug ("%s: $ send OK",
self->log_prefix);
mlm_proto_set_id (self->server->message, MLM_PROTO_OK);
mlm_proto_set_routing_id (self->server->message, self->routing_id);
mlm_proto_send (self->server->message, self->server->router);
}
if (!self->exception) {
// dispatch the service
if (self->server->verbose)
zsys_debug ("%s: $ dispatch the service", self->log_prefix);
dispatch_the_service (&self->client);
}
}
else
if (self->event == confirm_event) {
if (!self->exception) {
// have message confirmation
if (self->server->verbose)
zsys_debug ("%s: $ have message confirmation", self->log_prefix);
have_message_confirmation (&self->client);
}
}
else
if (self->event == credit_event) {
if (!self->exception) {
// credit the client
if (self->server->verbose)
zsys_debug ("%s: $ credit the client", self->log_prefix);
credit_the_client (&self->client);
}
}
else
if (self->event == connection_ping_event) {
if (!self->exception) {
// send CONNECTION_PONG
if (self->server->verbose)
zsys_debug ("%s: $ send CONNECTION_PONG",
self->log_prefix);
mlm_proto_set_id (self->server->message, MLM_PROTO_CONNECTION_PONG);
mlm_proto_set_routing_id (self->server->message, self->routing_id);
mlm_proto_send (self->server->message, self->server->router);
}
}
else
if (self->event == connection_close_event) {
if (!self->exception) {
// send OK
if (self->server->verbose)
zsys_debug ("%s: $ send OK",
self->log_prefix);
mlm_proto_set_id (self->server->message, MLM_PROTO_OK);
mlm_proto_set_routing_id (self->server->message, self->routing_id);
mlm_proto_send (self->server->message, self->server->router);
}
if (!self->exception) {
// client closed connection
if (self->server->verbose)
zsys_debug ("%s: $ client closed connection", self->log_prefix);
client_closed_connection (&self->client);
}
if (!self->exception) {
// deregister the client
if (self->server->verbose)
zsys_debug ("%s: $ deregister the client", self->log_prefix);
deregister_the_client (&self->client);
}
if (!self->exception) {
// terminate
if (self->server->verbose)
zsys_debug ("%s: $ terminate", self->log_prefix);
self->next_event = terminate_event;
}
}
else
if (self->event == stream_message_event) {
if (!self->exception) {
// get message to deliver
if (self->server->verbose)
zsys_debug ("%s: $ get message to deliver", self->log_prefix);
get_message_to_deliver (&self->client);
}
if (!self->exception) {
// send STREAM_DELIVER
if (self->server->verbose)
zsys_debug ("%s: $ send STREAM_DELIVER",
self->log_prefix);
mlm_proto_set_id (self->server->message, MLM_PROTO_STREAM_DELIVER);
mlm_proto_set_routing_id (self->server->message, self->routing_id);
mlm_proto_send (self->server->message, self->server->router);
}
if (!self->exception)
self->state = connected_state;
}
else
if (self->event == mailbox_message_event) {
if (!self->exception) {
// get message to deliver
if (self->server->verbose)
zsys_debug ("%s: $ get message to deliver", self->log_prefix);
get_message_to_deliver (&self->client);
}
if (!self->exception) {
// send MAILBOX_DELIVER
if (self->server->verbose)
zsys_debug ("%s: $ send MAILBOX_DELIVER",
self->log_prefix);
mlm_proto_set_id (self->server->message, MLM_PROTO_MAILBOX_DELIVER);
mlm_proto_set_routing_id (self->server->message, self->routing_id);
mlm_proto_send (self->server->message, self->server->router);
}
if (!self->exception) {
// check for mailbox messages
if (self->server->verbose)
zsys_debug ("%s: $ check for mailbox messages", self->log_prefix);
check_for_mailbox_messages (&self->client);
}
if (!self->exception)
self->state = connected_state;
}
else
if (self->event == service_message_event) {
if (!self->exception) {
// get message to deliver
if (self->server->verbose)
zsys_debug ("%s: $ get message to deliver", self->log_prefix);
get_message_to_deliver (&self->client);
}
if (!self->exception) {
// send SERVICE_DELIVER
if (self->server->verbose)
zsys_debug ("%s: $ send SERVICE_DELIVER",
self->log_prefix);
mlm_proto_set_id (self->server->message, MLM_PROTO_SERVICE_DELIVER);
mlm_proto_set_routing_id (self->server->message, self->routing_id);
mlm_proto_send (self->server->message, self->server->router);
}
if (!self->exception)
self->state = connected_state;
}
else
if (self->event == expired_event) {
if (!self->exception) {
// client expired
if (self->server->verbose)
zsys_debug ("%s: $ client expired", self->log_prefix);
client_expired (&self->client);
}
if (!self->exception) {
// deregister the client