-
Notifications
You must be signed in to change notification settings - Fork 172
/
chat.c
123 lines (112 loc) · 4.12 KB
/
chat.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
// --------------------------------------------------------------------------
// Example Zyre distributed chat application
//
// Copyright (c) 2010-2014 The Authors
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
// --------------------------------------------------------------------------
#include "zyre.h"
// This actor will listen and publish anything received
// on the CHAT group
static void
chat_actor (zsock_t *pipe, void *args)
{
zyre_t *node = zyre_new ((char *) args);
if (!node)
return; // Could not create new node
zyre_start (node);
zyre_join (node, "CHAT");
zsock_signal (pipe, 0); // Signal "ready" to caller
bool terminated = false;
zpoller_t *poller = zpoller_new (pipe, zyre_socket (node), NULL);
while (!terminated) {
void *which = zpoller_wait (poller, -1);
if (which == pipe) {
zmsg_t *msg = zmsg_recv (which);
if (!msg)
break; // Interrupted
char *command = zmsg_popstr (msg);
if (streq (command, "$TERM"))
terminated = true;
else
if (streq (command, "SHOUT")) {
char *string = zmsg_popstr (msg);
zyre_shouts (node, "CHAT", "%s", string);
}
else {
puts ("E: invalid message to actor");
assert (false);
}
free (command);
zmsg_destroy (&msg);
}
else
if (which == zyre_socket (node)) {
zmsg_t *msg = zmsg_recv (which);
char *event = zmsg_popstr (msg);
char *peer = zmsg_popstr (msg);
char *name = zmsg_popstr (msg);
char *group = zmsg_popstr (msg);
char *message = zmsg_popstr (msg);
if (streq (event, "ENTER"))
printf ("%s has joined the chat\n", name);
else
if (streq (event, "EXIT"))
printf ("%s has left the chat\n", name);
else
if (streq (event, "SHOUT"))
printf ("%s: %s\n", name, message);
else
if (streq (event, "EVASIVE"))
printf ("%s is being evasive\n", name);
else
if (streq (event, "SILENT"))
printf ("%s is being silent\n", name);
free (event);
free (peer);
free (name);
free (group);
free (message);
zmsg_destroy (&msg);
}
}
zpoller_destroy (&poller);
zyre_stop (node);
zclock_sleep (100);
zyre_destroy (&node);
}
int
main (int argc, char *argv [])
{
if (argc < 2) {
puts ("syntax: ./chat myname");
exit (0);
}
zactor_t *actor = zactor_new (chat_actor, argv [1]);
assert (actor);
while (!zsys_interrupted) {
char message [1024];
if (!fgets (message, 1024, stdin))
break;
message [strlen (message) - 1] = 0; // Drop the trailing linefeed
zstr_sendx (actor, "SHOUT", message, NULL);
}
zactor_destroy (&actor);
return 0;
}