-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfake_client.cpp
More file actions
186 lines (129 loc) · 3.4 KB
/
fake_client.cpp
File metadata and controls
186 lines (129 loc) · 3.4 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/* (c) Intel Corporation 2015, All Rights Reserved */
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <stdio.h>
#include <sys/time.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <math.h>
#include <signal.h>
#include <pthread.h>
#include "parse.h"
#include <iostream>
#include <sys/time.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <signal.h>
#include "common.h"
using namespace std;
int intensity = 1;
void __send_analyzed_info(int sockfd, analyzed_info_t *info) {
int rc;
client_packet_t packet;
packet.header.signature = CLIENT_HEADER_SIGNATURE;
GET_TIMESTAMP(&info->timestamp_gotframe);
GET_TIMESTAMP(&info->timestamp_sent);
memcpy(&packet.data, info, sizeof(analyzed_info_t));
packet.checksum = __calc_checksum(info);
rc = send(sockfd, &packet, sizeof(client_packet_t), MSG_DONTWAIT);
if (rc == -1) {
perror("Error sending data : ");
}
}
char *server_ip;
int server_port;
typedef struct client_info_t {
int camera_id;
int client_id;
} client_info_t;
void *client_fn(void *thread_data) {
struct sockaddr_in server_addr;
int rc;
client_info_t *cl;
int sockfd;
analyzed_info_t data;
int one = 1;
cl = (client_info_t*)thread_data;
printf("Starting thread for %d:%d\n", cl->client_id, cl->camera_id);
sockfd = socket(AF_INET, SOCK_STREAM, 0);
setsockopt(sockfd, SOL_TCP, TCP_NODELAY, &one, sizeof(one));
memset(&server_addr, 0x0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(server_port);
server_addr.sin_addr.s_addr = inet_addr(server_ip);
rc = connect(sockfd, (struct sockaddr*)&server_addr, sizeof(server_addr));
if (rc < 0 ) {
perror("Faield to connect\n");
return NULL;
}
printf("Connected!\n");
int client_id;
client_id = cl->client_id;
rc = send(sockfd, &client_id, sizeof(client_id), 0);
if (rc < 0) {
perror("Failed to send client id\n");
return NULL;
}
data.frame_id = 120;
data.camera_id = cl->camera_id;
while(1) {
// Fill in info here
data.frame_id++;
int i,x,y;
for (i=0; i<7; i++) {
data.color_info[i].x = 100;
data.color_info[i].y = 100;
data.color_info[i].area = 300;
}
i=0;
for (y=0; y<NUM_VERT_CELLS; y++) {
for (x=0; x<NUM_HORIZ_CELLS; x++) {
data.map[i] = 0x0;
i++;
}
}
for (i=0; i<intensity; i++) {
data.map[rand() % (NUM_VERT_CELLS * NUM_HORIZ_CELLS)] |= COLOR0_PRESENT;
data.map[rand() % (NUM_VERT_CELLS * NUM_HORIZ_CELLS)] |= COLOR1_PRESENT;
data.map[rand() % (NUM_VERT_CELLS * NUM_HORIZ_CELLS)] |= COLOR2_PRESENT;
}
__send_analyzed_info(sockfd, &data);
usleep(30000 + rand() % 10000);
}
}
int main(int argc, char **argv)
{
int i;
pthread_t client_thread[30];
int num_clients = 1;
if (argc < 3) {
printf("Usage: ./fake_client <server_ip> <server_port> <num_clients> <inensity>\n");
return -1;
}
server_ip = argv[1];
server_port = atoi(argv[2]);
if (argc >= 4) {
num_clients = atoi(argv[3]);
}
if (argc == 5) {
intensity = atoi(argv[4]);
}
for (i=0; i<num_clients; i++) {
client_info_t *cl = (client_info_t*)malloc(sizeof(client_info_t));
cl->client_id = i/2 + 1;
cl->camera_id = i%2;
pthread_create(&client_thread[i], 0, client_fn, cl);
}
for (i=0; i<num_clients; i++) {
pthread_join(client_thread[i], 0);
}
return 0;
}