1 /*
2 Copyright (c) 2012 Martin Sustrik All rights reserved.
3 Copyright (c) 2013 GoPivotal, Inc. All rights reserved.
4 Copyright 2016 Garrett D'Amore <garrett@damore.org>
5
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"),
8 to deal in the Software without restriction, including without limitation
9 the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 and/or sell copies of the Software, and to permit persons to whom
11 the Software is furnished to do so, subject to the following conditions:
12
13 The above copyright notice and this permission notice shall be included
14 in all copies or substantial portions of the Software.
15
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22 IN THE SOFTWARE.
23 */
24
25 #include "../transport.h"
26
27 #include "ep.h"
28 #include "sock.h"
29
30 #include "../utils/err.h"
31 #include "../utils/cont.h"
32 #include "../utils/fast.h"
33 #include "../utils/attr.h"
34
35 #include <string.h>
36
37 #define NN_EP_STATE_IDLE 1
38 #define NN_EP_STATE_ACTIVE 2
39 #define NN_EP_STATE_STOPPING 3
40
41 #define NN_EP_ACTION_STOPPED 1
42
43 /* Private functions. */
44 static void nn_ep_handler (struct nn_fsm *self, int src, int type,
45 void *srcptr);
46 static void nn_ep_shutdown (struct nn_fsm *self, int src, int type,
47 void *srcptr);
48
nn_ep_init(struct nn_ep * self,int src,struct nn_sock * sock,int eid,const struct nn_transport * transport,int bind,const char * addr)49 int nn_ep_init (struct nn_ep *self, int src, struct nn_sock *sock, int eid,
50 const struct nn_transport *transport, int bind, const char *addr)
51 {
52 int rc;
53
54 nn_fsm_init (&self->fsm, nn_ep_handler, nn_ep_shutdown,
55 src, self, &sock->fsm);
56 self->state = NN_EP_STATE_IDLE;
57
58 self->sock = sock;
59 self->eid = eid;
60 self->last_errno = 0;
61 nn_list_item_init (&self->item);
62 memcpy (&self->options, &sock->ep_template, sizeof(struct nn_ep_options));
63
64 /* Store the textual form of the address. */
65 nn_assert (strlen (addr) <= NN_SOCKADDR_MAX);
66 strcpy (self->addr, addr);
67
68 /* Create transport-specific part of the endpoint. */
69 if (bind)
70 rc = transport->bind (self);
71 else
72 rc = transport->connect (self);
73
74 /* Endpoint creation failed. */
75 if (rc < 0) {
76 nn_list_item_term (&self->item);
77 nn_fsm_term (&self->fsm);
78 return rc;
79 }
80
81 return 0;
82 }
83
nn_ep_term(struct nn_ep * self)84 void nn_ep_term (struct nn_ep *self)
85 {
86 nn_assert_state (self, NN_EP_STATE_IDLE);
87
88 self->ops.destroy (self->tran);
89 nn_list_item_term (&self->item);
90 nn_fsm_term (&self->fsm);
91 }
92
nn_ep_start(struct nn_ep * self)93 void nn_ep_start (struct nn_ep *self)
94 {
95 nn_fsm_start (&self->fsm);
96 }
97
nn_ep_stop(struct nn_ep * self)98 void nn_ep_stop (struct nn_ep *self)
99 {
100 nn_fsm_stop (&self->fsm);
101 }
102
nn_ep_stopped(struct nn_ep * self)103 void nn_ep_stopped (struct nn_ep *self)
104 {
105 /* TODO: Do the following in a more sane way. */
106 self->fsm.stopped.fsm = &self->fsm;
107 self->fsm.stopped.src = NN_FSM_ACTION;
108 self->fsm.stopped.srcptr = NULL;
109 self->fsm.stopped.type = NN_EP_ACTION_STOPPED;
110 nn_ctx_raise (self->fsm.ctx, &self->fsm.stopped);
111 }
112
nn_ep_getctx(struct nn_ep * self)113 struct nn_ctx *nn_ep_getctx (struct nn_ep *self)
114 {
115 return nn_sock_getctx (self->sock);
116 }
117
nn_ep_getaddr(struct nn_ep * self)118 const char *nn_ep_getaddr (struct nn_ep *self)
119 {
120 return self->addr;
121 }
122
nn_ep_getopt(struct nn_ep * self,int level,int option,void * optval,size_t * optvallen)123 void nn_ep_getopt (struct nn_ep *self, int level, int option,
124 void *optval, size_t *optvallen)
125 {
126 int rc;
127
128 rc = nn_sock_getopt_inner (self->sock, level, option, optval, optvallen);
129 errnum_assert (rc == 0, -rc);
130 }
131
nn_ep_ispeer(struct nn_ep * self,int socktype)132 int nn_ep_ispeer (struct nn_ep *self, int socktype)
133 {
134 return nn_sock_ispeer (self->sock, socktype);
135 }
136
137
nn_ep_shutdown(struct nn_fsm * self,int src,int type,NN_UNUSED void * srcptr)138 static void nn_ep_shutdown (struct nn_fsm *self, int src, int type,
139 NN_UNUSED void *srcptr)
140 {
141 struct nn_ep *ep;
142
143 ep = nn_cont (self, struct nn_ep, fsm);
144
145 if (nn_slow (src == NN_FSM_ACTION && type == NN_FSM_STOP)) {
146 ep->ops.stop (ep->tran);
147 ep->state = NN_EP_STATE_STOPPING;
148 return;
149 }
150 if (nn_slow (ep->state == NN_EP_STATE_STOPPING)) {
151 if (src != NN_FSM_ACTION || type != NN_EP_ACTION_STOPPED)
152 return;
153 ep->state = NN_EP_STATE_IDLE;
154 nn_fsm_stopped (&ep->fsm, NN_EP_STOPPED);
155 return;
156 }
157
158 nn_fsm_bad_state (ep->state, src, type);
159 }
160
161
nn_ep_handler(struct nn_fsm * self,int src,int type,NN_UNUSED void * srcptr)162 static void nn_ep_handler (struct nn_fsm *self, int src, int type,
163 NN_UNUSED void *srcptr)
164 {
165 struct nn_ep *ep;
166
167 ep = nn_cont (self, struct nn_ep, fsm);
168
169 switch (ep->state) {
170
171 /******************************************************************************/
172 /* IDLE state. */
173 /******************************************************************************/
174 case NN_EP_STATE_IDLE:
175 switch (src) {
176
177 case NN_FSM_ACTION:
178 switch (type) {
179 case NN_FSM_START:
180 ep->state = NN_EP_STATE_ACTIVE;
181 return;
182 default:
183 nn_fsm_bad_action (ep->state, src, type);
184 }
185
186 default:
187 nn_fsm_bad_source (ep->state, src, type);
188 }
189
190 /******************************************************************************/
191 /* ACTIVE state. */
192 /* We don't expect any events in this state. The only thing that can be done */
193 /* is closing the endpoint. */
194 /******************************************************************************/
195 case NN_EP_STATE_ACTIVE:
196 nn_fsm_bad_source (ep->state, src, type);
197
198 /******************************************************************************/
199 /* Invalid state. */
200 /******************************************************************************/
201 default:
202 nn_fsm_bad_state (ep->state, src, type);
203 }
204 }
205
nn_ep_set_error(struct nn_ep * self,int errnum)206 void nn_ep_set_error(struct nn_ep *self, int errnum)
207 {
208 if (self->last_errno == errnum)
209 /* Error is still there, no need to report it again */
210 return;
211 if (self->last_errno == 0)
212 nn_sock_stat_increment (self->sock, NN_STAT_CURRENT_EP_ERRORS, 1);
213 self->last_errno = errnum;
214 nn_sock_report_error (self->sock, self, errnum);
215 }
216
nn_ep_clear_error(struct nn_ep * self)217 void nn_ep_clear_error (struct nn_ep *self)
218 {
219 if (self->last_errno == 0)
220 /* Error is already clear, no need to report it */
221 return;
222 nn_sock_stat_increment (self->sock, NN_STAT_CURRENT_EP_ERRORS, -1);
223 self->last_errno = 0;
224 nn_sock_report_error (self->sock, self, 0);
225 }
226
nn_ep_stat_increment(struct nn_ep * self,int name,int increment)227 void nn_ep_stat_increment (struct nn_ep *self, int name, int increment)
228 {
229 nn_sock_stat_increment (self->sock, name, increment);
230 }
231
nn_ep_ispeer_ep(struct nn_ep * self,struct nn_ep * other)232 int nn_ep_ispeer_ep (struct nn_ep *self, struct nn_ep *other)
233 {
234 return nn_ep_ispeer (self, other->sock->socktype->protocol);
235 }
236
237 /* Set up an ep for use by a transport. Note that the core will already have
238 done most of the initialization steps. The tran is passed as the argument
239 to the ops. */
nn_ep_tran_setup(struct nn_ep * ep,const struct nn_ep_ops * ops,void * tran)240 void nn_ep_tran_setup (struct nn_ep *ep, const struct nn_ep_ops *ops,
241 void *tran)
242 {
243 ep->ops = *ops;
244 ep->tran = tran;
245 }
246