1 /*
2 
3   Copyright (c) 2016 Martin Sustrik
4 
5   Permission is hereby granted, free of charge, to any person obtaining a copy
6   of this software and associated documentation files (the "Software"),
7   to deal in the Software without restriction, including without limitation
8   the rights to use, copy, modify, merge, publish, distribute, sublicense,
9   and/or sell copies of the Software, and to permit persons to whom
10   the Software is furnished to do so, subject to the following conditions:
11 
12   The above copyright notice and this permission notice shall be included
13   in all copies or substantial portions of the Software.
14 
15   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18   THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20   FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21   IN THE SOFTWARE.
22 
23 */
24 
25 #include <assert.h>
26 #include <errno.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <unistd.h>
30 
31 #include "../../libdill.h"
32 
33 #define CONN_ESTABLISHED 1
34 #define CONN_SUCCEEDED 2
35 #define CONN_FAILED 3
36 
statistics(int ch)37 coroutine void statistics(int ch) {
38     int active = 0;
39     int succeeded = 0;
40     int failed = 0;
41 
42     while(1) {
43         int op;
44         int rc = chrecv(ch, &op, sizeof(op), -1);
45         if(rc < 0 && errno == ECANCELED) return;
46         assert(rc == 0);
47 
48         switch(op) {
49         case CONN_ESTABLISHED:
50             ++active;
51             break;
52         case CONN_SUCCEEDED:
53             --active;
54             ++succeeded;
55             break;
56         case CONN_FAILED:
57             --active;
58             ++failed;
59             break;
60         }
61 
62         printf("active: %-5d  succeeded: %-5d  failed: %-5d\n",
63             active, succeeded, failed);
64     }
65 }
66 
dialogue(int s,int ch)67 coroutine void dialogue(int s, int ch) {
68     int op = CONN_ESTABLISHED;
69     int rc = chsend(ch, &op, sizeof(op), -1);
70     assert(rc == 0);
71     int64_t deadline = now() + 60000;
72     rc = msend(s, "What's your name?", 17, deadline);
73     if(rc != 0) goto cleanup;
74     char inbuf[256];
75     ssize_t sz = mrecv(s, inbuf, sizeof(inbuf), deadline);
76     if(sz < 0) goto cleanup;
77     inbuf[sz] = 0;
78     char outbuf[256];
79     rc = snprintf(outbuf, sizeof(outbuf), "Hello, %s!", inbuf);
80     rc = msend(s, outbuf, rc, deadline);
81     if(rc != 0) goto cleanup;
82 cleanup:
83     op = errno == 0 ? CONN_SUCCEEDED : CONN_FAILED;
84     rc = chsend(ch, &op, sizeof(op), -1);
85     assert(rc == 0 || errno == ECANCELED);
86     rc = hclose(s);
87     assert(rc == 0);
88 }
89 
main(int argc,char * argv[])90 int main(int argc, char *argv[]) {
91 
92     int port = 5555;
93     if(argc > 1)
94         port = atoi(argv[1]);
95 
96     struct ipaddr addr;
97     int rc = ipaddr_local(&addr, NULL, port, 0);
98     assert(rc == 0);
99     int ls = tcp_listen(&addr, 10);
100     if(ls < 0) {
101         perror("Can't open listening socket");
102         return 1;
103     }
104 
105     int ch[2];
106     rc = chmake(ch);
107     assert(rc == 0);
108     int cr = go(statistics(ch[0]));
109     assert(cr >= 0);
110 
111     int b = bundle();
112     assert(b >= 0);
113 
114     int i;
115     for(i = 0; i != 3; i++) {
116         int s = tcp_accept(ls, NULL, -1);
117         assert(s >= 0);
118         s = suffix_attach(s, "\r\n", 2);
119         assert(s >= 0);
120         rc = bundle_go(b, dialogue(s, ch[1]));
121         assert(rc == 0);
122     }
123 
124     rc = bundle_wait(b, now() + 10000);
125     assert(rc == 0 || (rc < 0 && errno == ETIMEDOUT));
126 
127     rc = hclose(b);
128     assert(rc == 0);
129     rc = hclose(cr);
130     assert(rc == 0);
131     rc = hclose(ch[0]);
132     assert(rc == 0);
133     rc = hclose(ch[1]);
134     assert(rc == 0);
135     rc = hclose(ls);
136     assert(rc == 0);
137 
138     return 0;
139 }
140 
141