-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.cpp
195 lines (168 loc) · 5 KB
/
server.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
#include "common.h"
#include "agent.h"
#include "tracer.h"
#include "server.h"
#include "xmlHandler.h"
#include "communication.h"
#include <pthread.h>
#include <unistd.h>
#include <limits.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <thread>
#include <chrono>
#include <future>
using namespace std;
bool go;
void clearFiles()
{
const string path_if = "sh/model/ta_model_agents.if";
const string path_xtr = "sh/traces/t-1.xtr";
const string path_out_xml = "result/plan.xml";
ofstream file_if(path_if, ios_base::out);
ofstream file_xtr(path_xtr, ios_base::out);
}
void parse(vector<Agent> agents)
{
const string path_if = "sh/model/ta_model_agents.if";
const string path_xtr = "sh/traces/t-1.xtr";
const string path_out_xml = "result/plan.xml";
int satisfied = 0;
clear();
clearFiles();
system("sh/cmd.sh");
/* Load model in intermediate format */
ifstream file_if(path_if);
if (!file_if)
{
perror(path_if.c_str());
exit(EXIT_FAILURE);
}
loadIF(file_if);
file_if.close();
printf("PARSE: load traces...\n");
/* Load trace */
ifstream file_xtr(path_xtr);
if (!file_xtr)
{
perror(path_xtr.c_str());
exit(EXIT_FAILURE);
}
satisfied = parseTrace(file_xtr, agents);
file_xtr.close();
createXmlFile(satisfied, path_out_xml, agents);
}
void* connect(void* args)
//void connect()
{
//const string path_if = "sh/model/ta_model_agents.if";
//const string path_xtr = "sh/traces/t-1.xtr";
const string path_out_xml = "result/plan.xml";
vector<Agent> agents;
const string path_model = "sh/model/ta_model_agents_test.xml";
const char *ip = "localhost";
const int port = 9779;
struct timeval timeout = {10,0};
int server_sock;
int client_sock;
int i, read_return, heartbeat = 0;
fd_set activeFdSet, readFdSet;
socklen_t size;
try
{
server_sock = initialize_sock(ip, port);
FD_ZERO(&activeFdSet);
FD_SET(server_sock, &activeFdSet);
while(go)
{
readFdSet = activeFdSet;
if(select(FD_SETSIZE, &readFdSet, NULL, NULL, &timeout) < 0)
{
perror("Select failed\n");
}
timeout.tv_sec = 10;
for(i = 0; i < FD_SETSIZE; i++)
{
if(FD_ISSET(i, &readFdSet))
{
if(i == server_sock)
{
printf("SERver: accepting file..............\n");
client_sock = accept_client(server_sock);
FD_SET(client_sock, &activeFdSet);
}
else if(i == client_sock)
{
if(receive_xml_from_mmt(i, path_model.c_str()) != -1)
{
parse(agents);
close(i);
FD_CLR(i, &activeFdSet);
client_sock = -1;
send_file_to_mmt(server_sock, path_out_xml.c_str());
//go = false;
}
else
{
close(i);
FD_CLR(i, &activeFdSet);
}
}
}
}
printf("Tick: %d\n", heartbeat++);
/*if(client_sock > 0)
{
send_file_to_mmt(client_sock, path_out_xml.c_str());
close(client_sock);
FD_CLR(client_sock, &activeFdSet);
client_sock = -1;
}*/
}
if(server_sock > 0)
{
close_communication(server_sock);
}
}
catch (std::exception &e)
{
cerr << "SUB: Caught exception: " << e.what() << endl;
}
puts("SUB: Quit the sub process!");
pthread_exit(0);
}
int main(int argc, char *argv[])
{
pthread_t pid;
string quit;
//const string path_model = "sh/model/ta_model_agents_test.xml";
go = true;
int ret = pthread_create(&pid, NULL, connect, NULL);
if (ret != 0)
{
cout << "MAIn: pthread_create error: error_code=" << ret << endl;
}
//write_file_test(path_model.c_str());
while(true)
{
puts("MAIn: New round! please input your command:");
std::cin >> quit;
/*if(go == false)
{
go = true;
ret = pthread_create(&pid, NULL, connect, NULL);
if (ret != 0)
{
cout << "MAIn: pthread_create error: error_code=" << ret << endl;
}
}*/
if(quit.compare("quit") == 0)
{
go = false;
break;
}
}
puts("MAIn: Quit the main program!\nPlease wait for the sub thread quit...");
pthread_exit(NULL);
}