-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmagTCSsim.cpp
208 lines (166 loc) · 4.31 KB
/
magTCSsim.cpp
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/** \file magTCSsim.cpp
* \author Jared R. Males
* \brief Main program for magTCSsim
*/
//***********************************************************************//
// Copyright 2018 Jared R. Males ([email protected])
//
// This file is part of magTCSsim.
//
// magTCSsim is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// magTCSsim is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with magTCSsim. If not, see <http://www.gnu.org/licenses/>.
//***********************************************************************//
#include "magtelescope.hpp"
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <pthread.h>
///Structure to pass to threads.
struct mtsfile
{
magtelescope *mts;
int fd;
int port;
};
/* set up a listen socket
*/
int lsocket (int port)
{
unsigned short sport;
int s;
struct sockaddr_in sockaddr;
int on;
/* start listening for connection
*/
sport = htons((short)port);
if ((s = socket (AF_INET, SOCK_STREAM, 0)) < 0)
{
perror ("socket");
return (-1);
}
on = 1;
if (setsockopt(s,SOL_SOCKET,SO_REUSEADDR,(char *)&on,sizeof(on)) < 0)
{
perror ("setsockopt");
return (-1);
}
memset ((char *)&sockaddr, 0, sizeof(sockaddr));
sockaddr.sin_family = AF_INET;
sockaddr.sin_port = sport;
sockaddr.sin_addr.s_addr = INADDR_ANY;
if (bind (s, (struct sockaddr *)&sockaddr, sizeof(sockaddr)) < 0)
{
perror ("bind");
return (-1);
}
if (listen (s, 10) < 0)
{
perror ("listen");
return (-1);
}
return (s);
}
void * listen_telescope_socket(void *vmf)
{
mtsfile mtf;
char string[MAGTCS_INPUT_SIZE];
mtf.mts = ((mtsfile *) vmf)->mts;
mtf.fd = ((mtsfile *) vmf)->fd;
mtf.port = ((mtsfile*) vmf)->port;
FILE *fp;
if ((fp = fdopen (mtf.fd, "r+")) == NULL)
{
exit (-6);
}
while (!tcs_fgets(string, MAGTCS_INPUT_SIZE, fp))
{
mtf.mts->process_string(std::string(string), fp, mtf.port == 5800 ); /// \todo status only port
}
std::cerr << "Closing\n";
fclose(fp);
return nullptr;
}
int network_accept_any( int & sockNo,
int socks[],
unsigned int count,
struct sockaddr *addr,
socklen_t *addrlen
)
{
fd_set readfds;
int maxfd, fd;
unsigned int i;
int status;
FD_ZERO(&readfds);
maxfd = -1;
for(i = 0; i < count; i++)
{
FD_SET(socks[i], &readfds);
if (socks[i] > maxfd) maxfd = socks[i];
}
status = select(maxfd + 1, &readfds, NULL, NULL, NULL);
if (status < 0)
{
return -1;
}
fd = -1;
for (i = 0; i < count; i++)
{
if (FD_ISSET(socks[i], &readfds))
{
fd = socks[i];
sockNo = i;
break;
}
}
if (fd == -1)
return -1;
else
return accept(fd, addr, addrlen);
}
int main (int argc, char *argv[])
{
int s;
magtelescope mts;
mts.set_typval();
mts.start_simulating();
mts.start_tracking();
int socks[2];
int ports[2];
ports[0] = 5800;
ports[1] = 5811;
if ((socks[0] = lsocket (ports[0])) < 0)
{
fprintf (stderr, "lsocket (5800)\n");
exit (-4);
}
if ((socks[1] = lsocket (ports[1])) < 0)
{
fprintf (stderr, "lsocket (5811)\n");
exit (-4);
}
while(1)
{
int fd;
int sockNo;
fd = network_accept_any(sockNo, socks, 2,(struct sockaddr *)0, (socklen_t *)0);
mtsfile mtf;
mtf.mts = &mts;
mtf.fd = fd;
mtf.port = ports[sockNo];
std::cerr << "Connected: " << sockNo << " " << ports[sockNo] << " " << fd << "\n";
pthread_t thread_id;
pthread_create(&thread_id, NULL, &listen_telescope_socket, (void *) &mtf);
}
exit (0);
}