Skip to content

Commit 32ffb01

Browse files
committed
Update multicast demo for TIPC 2.0
Make minor changes to update the multicast demo for TIPC 2.0, including: - adding support for endianness in topology server messages - standardizing comments and formatting to match other TIPC applications Signed-off-by: Allan Stephens <allan.stephens@windriver.com>
1 parent 24e371b commit 32ffb01

File tree

2 files changed

+148
-161
lines changed

2 files changed

+148
-161
lines changed

demos/multicast_demo/client_tipc.c

Lines changed: 92 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -1,108 +1,101 @@
11
/* ------------------------------------------------------------------------
2-
//
3-
// client_tipc.c
4-
//
5-
// Short description: Client side of multicast demo.
6-
//
7-
// ------------------------------------------------------------------------
8-
//
9-
// Copyright (c) 2003, Ericsson Research Canada
10-
* Copyright (c) 2005, Wind River Systems
11-
// All rights reserved.
12-
// Redistribution and use in source and binary forms, with or without
13-
// modification, are permitted provided that the following conditions are met:
14-
//
15-
// Redistributions of source code must retain the above copyright notice, this
16-
// list of conditions and the following disclaimer.
17-
// Redistributions in binary form must reproduce the above copyright notice,
18-
// this list of conditions and the following disclaimer in the documentation
19-
// and/or other materials provided with the distribution.
20-
// Neither the name of the copyright holders nor the names of its
21-
// contributors may be used to endorse or promote products derived from this
22-
// software without specific prior written permission.
23-
//
24-
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25-
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26-
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27-
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
28-
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29-
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30-
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31-
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32-
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33-
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34-
// POSSIBILITY OF SUCH DAMAGE.
35-
//
36-
// ------------------------------------------------------------------------
37-
//
38-
// Created 2000-09-30 by Jon Maloy
39-
//
40-
// ------------------------------------------------------------------------
41-
//
42-
// $Id: client_tipc.c,v 1.6 2005/09/20 20:23:15 ajstephens Exp $
43-
//
44-
// Revision history:
45-
// ----------------
46-
// Rev Date Rev by Reason
47-
// --- ---- ------ ------
48-
//
49-
// PA1 2000-09-30 Jon Maloy Created
50-
// PA2 2004-03-25 M. Pourzandi Simplified to support a simple hello message
51-
//
52-
// ------------------------------------------------------------------------
53-
*/
2+
*
3+
* client_tipc.c
4+
*
5+
* Short description: TIPC multicast demo (client side)
6+
*
7+
* ------------------------------------------------------------------------
8+
*
9+
* Copyright (c) 2003, Ericsson Research Canada
10+
* Copyright (c) 2005,2010 Wind River Systems
11+
* All rights reserved.
12+
* Redistribution and use in source and binary forms, with or without
13+
* modification, are permitted provided that the following conditions are met:
14+
*
15+
* Redistributions of source code must retain the above copyright notice, this
16+
* list of conditions and the following disclaimer.
17+
* Redistributions in binary form must reproduce the above copyright notice,
18+
* this list of conditions and the following disclaimer in the documentation
19+
* and/or other materials provided with the distribution.
20+
* Neither the name of the copyright holders nor the names of its
21+
* contributors may be used to endorse or promote products derived from this
22+
* software without specific prior written permission.
23+
*
24+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
28+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34+
* POSSIBILITY OF SUCH DAMAGE.
35+
*
36+
* ------------------------------------------------------------------------
37+
*/
38+
5439
#include <sys/types.h>
5540
#include <sys/socket.h>
5641
#include <stdlib.h>
5742
#include <stdio.h>
58-
#include <assert.h>
5943
#include <string.h>
6044
#include <sys/param.h>
61-
#include <netdb.h>
6245
#include <errno.h>
6346
#include <unistd.h>
64-
#include <sys/time.h>
65-
#include <ctype.h>
47+
#include <netinet/in.h>
6648
#include <linux/tipc.h>
6749

6850
#define SERVER_TYPE 18888
6951

70-
void wait_for_server(int name_type, int name_inst, int wait)
52+
void wait_for_server(__u32 name_type, __u32 name_instance, int wait)
7153
{
72-
struct sockaddr_tipc topsrv;
73-
struct tipc_subscr subscr = {{name_type, name_inst, ~0}, wait,
74-
TIPC_SUB_SERVICE, {}};
75-
struct tipc_event event;
54+
struct sockaddr_tipc topsrv;
55+
struct tipc_subscr subscr;
56+
struct tipc_event event;
7657

77-
int sd = socket (AF_TIPC, SOCK_SEQPACKET, 0);
78-
assert(sd > 0);
58+
int sd = socket(AF_TIPC, SOCK_SEQPACKET, 0);
7959

80-
memset(&topsrv, 0, sizeof(topsrv));
60+
memset(&topsrv, 0, sizeof(topsrv));
8161
topsrv.family = AF_TIPC;
82-
topsrv.addrtype = TIPC_ADDR_NAME;
83-
topsrv.addr.name.name.type = TIPC_TOP_SRV;
84-
topsrv.addr.name.name.instance = TIPC_TOP_SRV;
62+
topsrv.addrtype = TIPC_ADDR_NAME;
63+
topsrv.addr.name.name.type = TIPC_TOP_SRV;
64+
topsrv.addr.name.name.instance = TIPC_TOP_SRV;
8565

86-
if (0 > connect(sd, (struct sockaddr *)&topsrv, sizeof(topsrv))) {
87-
perror("failed to connect to topology server");
88-
exit(1);
89-
}
90-
if (send(sd, &subscr, sizeof(subscr), 0) != sizeof(subscr)) {
91-
perror("failed to send subscription");
92-
exit(1);
93-
}
94-
if (recv(sd, &event, sizeof(event), 0) != sizeof(event)) {
95-
perror("Failed to receive event");
96-
exit(1);
97-
}
98-
if (event.event != TIPC_PUBLISHED) {
99-
printf("Server %u not published within %u [s]\n",
100-
name_type, wait/1000);
101-
exit(1);
102-
}
103-
close(sd);
66+
/* Connect to topology server */
67+
68+
if (0 > connect(sd, (struct sockaddr *)&topsrv, sizeof(topsrv))) {
69+
perror("Client: failed to connect to topology server");
70+
exit(1);
71+
}
72+
73+
subscr.seq.type = htonl(name_type);
74+
subscr.seq.lower = htonl(name_instance);
75+
subscr.seq.upper = htonl(name_instance);
76+
subscr.timeout = htonl(wait);
77+
subscr.filter = htonl(TIPC_SUB_SERVICE);
78+
79+
if (send(sd, &subscr, sizeof(subscr), 0) != sizeof(subscr)) {
80+
perror("Client: failed to send subscription");
81+
exit(1);
82+
}
83+
/* Now wait for the subscription to fire */
84+
85+
if (recv(sd, &event, sizeof(event), 0) != sizeof(event)) {
86+
perror("Client: failed to receive event");
87+
exit(1);
88+
}
89+
if (event.event != htonl(TIPC_PUBLISHED)) {
90+
printf("Client: server {%u,%u} not published within %u [s]\n",
91+
name_type, name_instance, wait/1000);
92+
exit(1);
93+
}
94+
95+
close(sd);
10496
}
10597

98+
10699
void client_mcast(int sd, int lower, int upper)
107100
{
108101
struct sockaddr_tipc server_addr;
@@ -119,19 +112,20 @@ void client_mcast(int sd, int lower, int upper)
119112
server_addr.addr.nameseq.type,
120113
server_addr.addr.nameseq.lower,
121114
server_addr.addr.nameseq.upper);
122-
printf("Sending: %s\n", buf);
115+
printf("Client: sending %s\n", buf);
123116
} else {
124117
sd = -sd;
125118
buf[0] = '\0';
126-
printf("Sending: termination message to {%u,%u,%u}\n",
119+
printf("Client: sending termination message to {%u,%u,%u}\n",
127120
server_addr.addr.nameseq.type,
128121
server_addr.addr.nameseq.lower,
129122
server_addr.addr.nameseq.upper);
130123
}
131124

132125
if (0 > sendto(sd, buf, strlen(buf) + 1, 0,
133126
(struct sockaddr *)&server_addr, sizeof(server_addr))) {
134-
perror("Client: Failed to send");
127+
perror("Client: failed to send");
128+
exit(1);
135129
}
136130
}
137131

@@ -149,12 +143,12 @@ void client_mcast(int sd, int lower, int upper)
149143
* message.
150144
*/
151145

152-
int main(int argc, char* argv[], char* envp[])
146+
int main(int argc, char *argv[], char *envp[])
153147
{
154-
int sd = socket (AF_TIPC, SOCK_RDM, 0);
155148
int lower;
156149
int upper;
157150
char dummy;
151+
int sd = socket(AF_TIPC, SOCK_RDM, 0);
158152

159153
if (sd < 0) {
160154
printf("TIPC not active on this node\n");
@@ -180,11 +174,15 @@ int main(int argc, char* argv[], char* envp[])
180174

181175
/* multicast to instance range specified by user */
182176

183-
if (sscanf(argv[1], "%u%c", &lower, &dummy) != 1)
184-
perror("invalid lower bound");
177+
if (sscanf(argv[1], "%u%c", &lower, &dummy) != 1) {
178+
perror("Client: invalid lower bound");
179+
exit(1);
180+
}
185181
if (argc > 2) {
186-
if (sscanf(argv[2], "%u%c", &upper, &dummy) != 1)
187-
perror("invalid upper bound");
182+
if (sscanf(argv[2], "%u%c", &upper, &dummy) != 1) {
183+
perror("Client: invalid upper bound");
184+
exit(1);
185+
}
188186
}
189187
else
190188
upper = lower;

0 commit comments

Comments
 (0)