-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
269 lines (194 loc) · 4.8 KB
/
main.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <iostream>
#include <string>
#include <stdlib.h>
#include <pthread.h>
#include <pthread.h>
#include "src/cArduino.cpp"
#include "src/server.cpp"
#include "src/query.cpp"
cArduino *ARDUINO = new cArduino();
string powitanie = "";//co powiedzial modol na dziendobry
//kolejka
//##########################################
static myQuery kolejka(2550);
using namespace std;
pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
void smartExecuteStart();
void Add(char *msg,int sock=-1){
pthread_mutex_lock(&mutex1);
kolejka.push(msg,sock);
pthread_mutex_unlock(&mutex1);
smartExecuteStart();
}
//##########################################
//server
//##########################################
void server_onNewClient(int);
static TcpSercer server(3333,server_onNewClient);
//obsluga clienta
void* task_server_client(void *int_sock){
int sock = ((int)int_sock);
server.client_write(sock,(char*)powitanie.c_str());
server.client_write(sock,(char*)"\n>>");
while(true)
{
//odczytaj zapytanie
char *request=server.client_read(sock);
if(request==0){ //disconect
break;
}
//dodaj do kolejki
Add(request,sock);
}
server.client_close(sock);
}
//polaczenie nowego klienta/połączenia -> tworzymy nowy wątek dla niego
void server_onNewClient(int sock){
int a=sock;
pthread_t t;
pthread_create(&t,NULL,task_server_client,(void*)a);
}
//wątek obsugi servera
void* task_server(void *arg){
printf("server runing at port %d\n",server.getPort());
server.run();
}
//##########################################
//############################
#define KDEFAULT "\x1B[37m"
#define KINPUT "\x1B[32m"
#define KOUTPUT "\x1B[33m"
//############################
// wypisz odczytany napis
void myprint(char *str){
printf("%s%s",KOUTPUT,str);
printf("%s",KDEFAULT);//dalej domyslny kolor
}
//zdanie odczytania polecen z klawiarury
void* task_stdin(void *arg){
while(1)
{
char send_buffer[255];
//wczytaj z klawiatury polecnie & wyslij
printf("%s>> ",KINPUT);
scanf("%s",send_buffer);
printf("%s",KDEFAULT);//dalej domyslny kolor
Add(send_buffer);
//sleep(2);
}
}
//zadamoe wykonania polecen (wywolywane przez 'smartExecuteStart' )
void* task_execute(void *){
pthread_mutex_lock(&mutex1);
while(kolejka.getNum()>0)
{
myQuery::sMesage m = kolejka.pop();
//wyslij polecenie i odczytaj odpowiedz
ARDUINO->write(m.msg);//zapisz do arduino
string odpowiedz="";
ARDUINO->read(odpowiedz,(char)3,500000);
if(odpowiedz.length()<1)
odpowiedz="FAIL\n";
//---
//odpowiedz (komu trzeba)
if(m.sock==-1){//STDOUT
myprint((char*)odpowiedz.c_str());
}
if(m.sock!=-1){//TPC
server.client_write(m.sock,(char*)odpowiedz.c_str());
}
//-----
}
pthread_mutex_unlock(&mutex1);
}
//sprytne Wykonywanie polecenia
pthread_mutex_t mutexSmart = PTHREAD_MUTEX_INITIALIZER;
bool smart=false;
void smartExecuteStart(){
pthread_mutex_lock(&mutex1);
if(smart==true)
{
pthread_mutex_unlock(&mutex1);
return;
}
smart=true;
pthread_mutex_unlock(&mutex1);
usleep(1000);//TODO: czy potrzebne?
pthread_t t1;
pthread_create(&t1,NULL,task_execute,NULL);
pthread_join(t1,0);
pthread_mutex_lock(&mutex1);
smart=false;
pthread_mutex_unlock(&mutex1);
}
//--
//łapanie przerwan programu
void sighandler(int sig){
cout<< "Signal " << sig << " caught...exiting" << endl;
server.stop();
ARDUINO->close();
sleep(1);
exit(0);
}
int main (int argc, char **argv) {
//##
//sprawdz czy server uruchomiony
if(!server.isBinded())
{
perror("server problem");
return 1;
}
//##########################
//##
//sprawdz czy podloaczono arduino
if(ARDUINO->findArduino()==0)
{
perror("nie podloczono Arduino");
return 1;
}
printf("arduino: "); ARDUINO->print_port(ARDUINO->getDeviceName());
//############################
signal(SIGABRT, &sighandler);
signal(SIGTERM, &sighandler);
signal(SIGINT, &sighandler);
//otwieranie Arduino
if(!ARDUINO->open( (ArduinoBaundRate)B115200bps)){
perror("nie moge podloczonyc Arduino");
return 1;
}
if(!ARDUINO->isOpen()) {
perror("nie moge otworzyc Arduino");
return 1;
}
//--
//powitanie
ARDUINO->read(powitanie,(char)3,3*1000000);
if(powitanie.length()<10){
perror("arduino problem (nie ma powitania)");
return 1;
}else{
powitanie =
"server date version: "+string(__DATE__)+"\n"
+powitanie;
myprint((char*)powitanie.c_str());//pokaz powiadomienie
}
//--
//tworze watek tcpip
pthread_t t1;
pthread_create(&t1,NULL,task_server,NULL);
//--
//tworze watek klawiatury
pthread_t t2;
//pthread_create(&t2,NULL,task_stdin,NULL);
//poczekaj na zakonczenie abu watkow
pthread_join(t1,0);
//pthread_join(t2,0);
//---
//koniec pracy
ARDUINO->close();
//--
return 0;
}