1 /*
2     Copyright (c) 2013 Martin Sustrik  All rights reserved.
3     Copyright 2016 Garrett D'Amore <garrett@damore.org>
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 #include "xbus.h"
25 
26 #include "../../nn.h"
27 #include "../../bus.h"
28 
29 #include "../../utils/cont.h"
30 #include "../../utils/alloc.h"
31 #include "../../utils/err.h"
32 
33 struct nn_bus {
34     struct nn_xbus xbus;
35 };
36 
37 /*  Private functions. */
38 static void nn_bus_init (struct nn_bus *self,
39     const struct nn_sockbase_vfptr *vfptr, void *hint);
40 static void nn_bus_term (struct nn_bus *self);
41 
42 /*  Implementation of nn_sockbase's virtual functions. */
43 static void nn_bus_destroy (struct nn_sockbase *self);
44 static int nn_bus_send (struct nn_sockbase *self, struct nn_msg *msg);
45 static int nn_bus_recv (struct nn_sockbase *self, struct nn_msg *msg);
46 static const struct nn_sockbase_vfptr nn_bus_sockbase_vfptr = {
47     NULL,
48     nn_bus_destroy,
49     nn_xbus_add,
50     nn_xbus_rm,
51     nn_xbus_in,
52     nn_xbus_out,
53     nn_xbus_events,
54     nn_bus_send,
55     nn_bus_recv,
56     NULL,
57     NULL
58 };
59 
nn_bus_init(struct nn_bus * self,const struct nn_sockbase_vfptr * vfptr,void * hint)60 static void nn_bus_init (struct nn_bus *self,
61     const struct nn_sockbase_vfptr *vfptr, void *hint)
62 {
63     nn_xbus_init (&self->xbus, vfptr, hint);
64 }
65 
nn_bus_term(struct nn_bus * self)66 static void nn_bus_term (struct nn_bus *self)
67 {
68     nn_xbus_term (&self->xbus);
69 }
70 
nn_bus_destroy(struct nn_sockbase * self)71 static void nn_bus_destroy (struct nn_sockbase *self)
72 {
73     struct nn_bus *bus;
74 
75     bus = nn_cont (self, struct nn_bus, xbus.sockbase);
76 
77     nn_bus_term (bus);
78     nn_free (bus);
79 }
80 
nn_bus_send(struct nn_sockbase * self,struct nn_msg * msg)81 static int nn_bus_send (struct nn_sockbase *self, struct nn_msg *msg)
82 {
83     int rc;
84     struct nn_bus *bus;
85 
86     bus = nn_cont (self, struct nn_bus, xbus.sockbase);
87 
88     /*  Check for malformed messages. */
89     if (nn_chunkref_size (&msg->sphdr))
90         return -EINVAL;
91 
92     /*  Send the message. */
93     rc = nn_xbus_send (&bus->xbus.sockbase, msg);
94     errnum_assert (rc == 0, -rc);
95 
96     return 0;
97 }
98 
nn_bus_recv(struct nn_sockbase * self,struct nn_msg * msg)99 static int nn_bus_recv (struct nn_sockbase *self, struct nn_msg *msg)
100 {
101     int rc;
102     struct nn_bus *bus;
103 
104     bus = nn_cont (self, struct nn_bus, xbus.sockbase);
105 
106     /*  Get next message. */
107     rc = nn_xbus_recv (&bus->xbus.sockbase, msg);
108     if (nn_slow (rc == -EAGAIN))
109         return -EAGAIN;
110     errnum_assert (rc == 0, -rc);
111     nn_assert (nn_chunkref_size (&msg->sphdr) == sizeof (uint64_t));
112 
113     /*  Discard the header. */
114     nn_chunkref_term (&msg->sphdr);
115     nn_chunkref_init (&msg->sphdr, 0);
116 
117     return 0;
118 }
119 
nn_bus_create(void * hint,struct nn_sockbase ** sockbase)120 static int nn_bus_create (void *hint, struct nn_sockbase **sockbase)
121 {
122     struct nn_bus *self;
123 
124     self = nn_alloc (sizeof (struct nn_bus), "socket (bus)");
125     alloc_assert (self);
126     nn_bus_init (self, &nn_bus_sockbase_vfptr, hint);
127     *sockbase = &self->xbus.sockbase;
128 
129     return 0;
130 }
131 
132 struct nn_socktype nn_bus_socktype = {
133     AF_SP,
134     NN_BUS,
135     0,
136     nn_bus_create,
137     nn_xbus_ispeer,
138 };
139