-
Notifications
You must be signed in to change notification settings - Fork 0
/
sub_test.c
146 lines (122 loc) · 2.56 KB
/
sub_test.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <assert.h>
#include <fcntl.h>
#include <stdint.h>
#include <string.h>
#include "icehydra.h"
#define strLen(str) ((!str)?0:strlen(str))
#define memzero(buf, n) (void) memset(buf, 0, n)
void help(void)
{
printf(
" v (show version )\n"
" h (show help info)\n"
" D (run in Daemon.)\n"
" s (need send msg string.)\n"
" S (the name of Subscriber)\n"
" P (the name of Publisher)\n"
" i (broadcast ids. -i 11,22,33 )\n"
);
}
static void find_atoi(char *string,uint8_t *ids,uint8_t *ids_num)
{
if(string == NULL) return ;
char *p = strtok(string,",");
int count = 0;
while(p != NULL)
{
ids[count++] = atoi(p);
p = strtok(NULL,",");
}
*ids_num = count ;
return ;
}
int main(int argc,char *argv[])
{
char name[128] = {0},pub_name[128] = {0};
int ret,ch,deamon = 0,needsend = 0;
uint8_t ids_num=0,ids[128] = {0};
char str[128] = {0};
while((ch = getopt(argc,argv,"hvDi:s:P:S:"))!= EOF)
{
switch(ch){
case 'i':
find_atoi(optarg,ids,&ids_num);
break;
case 'S':
strcpy(name,optarg);
break;
case 'v':
case 'h':
help();
return 0;
break;
case 'D':
deamon = 1;
break;
case 's':
needsend = 1;
strcpy(str,optarg);
break;
case 'P':
strcpy(pub_name,optarg);
break;
default:
break;
}
}
if(deamon)
ret = daemon(1,1);
assert(strLen(name));
ret = ih_subscriber_create_connect(name,pub_name);
if(ret < 0)
return -1;
int datalen = strlen(str);
uint8_t shm_nifos[800] = {0};
ret = ih_get_shm_infos(shm_nifos,800);
if(ret <0)
exit(-1);
{
void *shm_ptr = NULL;
int shm_size = 0;
for(int i=0;i<4;i++){
char shm_name[16] = {0};
sprintf(shm_name,"testname%d",i);
shm_size = ih_get_shm_by_name(shm_nifos,shm_name,&shm_ptr);
printf("%s size %d %p \n",shm_name,shm_size,shm_ptr);
}
}
if(needsend){
IH_BROADCAST_CMD_T cmdtest = {
.br_ids = ids,
.br_num = ids_num,
.data = str,
.datalen = datalen,
};
ret = ih_send_broadcast_data(&cmdtest);
if(ret < 0){
printf("client send %d bytes %s \n",datalen,ret?"Fail":"SUCCESS");
return -1;
}
}
uint8_t rbuf[128] = {0};
int recvlen;
struct timeval timeout;
while(1)
{
timeout.tv_sec = 1;
timeout.tv_usec = 0;
if(!ih_select_recv_ready(&timeout))
continue;
memzero(rbuf, sizeof(rbuf));
ret = ih_recv_data(rbuf,&recvlen);
if(ret < 0){
printf("error recv \n");
exit(-1);
}
printf("%s client recv : %s \n",name,rbuf);
}
return 0;
}