summaryrefslogtreecommitdiff
blob: 838f702ece4ca074b71a429d184adabbdeb8918c (plain)
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
/*
* Copyright (C) 2010 Robin H.Johnson, Ovechko Kostyantyn <fastinetserver@gmail.com>.
*
* Project: IDFetch.
* Developer: Ovechko Kostyantyn Olexandrovich (Kharkiv State Technical University of Construction and Architecture, Ukraine).
* Mentor: Robin H. Johnson (Gentoo Linux: Developer, Trustee & Infrastructure Lead).
* Mentoring organization: Gentoo Linux.
* Sponsored by GSOC 2010.
*
* This file is part of Segget.
*
* Segget is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* Segget 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with Segget; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/

#include <iostream>
#include "tuiclient.h"
#include "str.h"


Twindow * next_window(){
	cur_window_num++;
	if (cur_window_num>3) cur_window_num=0;
	switch (cur_window_num){
		case 0:
				mainwindow.log_win.visible=false;
				mainwindow.error_log_win.visible=false;
				mainwindow.distfiles_win.visible=false;
				return &mainwindow;
		case 1:
				mainwindow.log_win.visible=true;
				mainwindow.error_log_win.visible=false;
				mainwindow.distfiles_win.visible=false;
				return &mainwindow.log_win;
		case 2:
				mainwindow.log_win.visible=false;
				mainwindow.error_log_win.visible=true;
				mainwindow.distfiles_win.visible=false;
				return &mainwindow.error_log_win;
		case 3:
				mainwindow.log_win.visible=false;
				mainwindow.error_log_win.visible=false;
				mainwindow.distfiles_win.visible=true;
				return &mainwindow.distfiles_win;
	}
	return &mainwindow;
}

/*
void * watch_keyboard_thread_function(void *){
	Twindow * cur_window=&mainwindow.distfiles_win;
	while (not exit_flag){
		int key=getch();
		if (key==ERR){
			mainwindow.refresh();
			struct timeval delay;
			delay.tv_sec=0;
			delay.tv_usec=300000;
			select(FD_SETSIZE, (fd_set *)0, (fd_set *)0,(fd_set *)0, (struct timeval *) &delay);//&timeout
		}else{
			switch (key){
				//tab - move between windows
				case (char)9:cur_window=next_window();
				//KEY_DOWN
				case (char)2:cur_window->down(1);mainwindow.set_line(25,toString((int)key));break;
				//KEY_RIGHT
				case (char)5:cur_window->down(1);mainwindow.set_line(25,toString((int)key));break;
				//KEY_UP
				case (char)3:cur_window->up(1);mainwindow.set_line(25,toString((int)key));break;
				//KEY_LEFT
				case (char)4:cur_window->up(1);mainwindow.set_line(25,toString((int)key));break;
				//KEY_PG_DOWN
				case (char)82:cur_window->down(mainwindow.bottom_screenline_num);break;
				//KEY_PG_UP
				case (char)83:cur_window->up(mainwindow.bottom_screenline_num);break;
				case 'd':
				case 'D':mainwindow.distfiles_win.toggle();
						mainwindow.log_win.visible=false;
						mainwindow.error_log_win.visible=false;
						cur_window=&mainwindow.distfiles_win;
						mainwindow.show();
						break;
				case 'q':
				case 'Q':quit(0,"Interrupted by user");break;
				case 'h':
				case 'H':mainwindow.help_win.toggle(); mainwindow.show();break;
				case 'l':
				case 'L':mainwindow.log_win.toggle(); mainwindow.show();break;
				case 'e':
				case 'E':mainwindow.error_log_win.toggle(); mainwindow.show();break;
				default: mainwindow.set_line(25,toString((int)key));
			}
		}
	}
//	pthread_exit((void*)watch_keyboard_thread_status);
	exit_curses_and_show_msg();
	return (void*)0;
	//watch_keyboard_thread_status;
}

void * refresh_screen_thread_function(void *){
	return (void*)0;
}
*/

void * watch_keyboard_thread_function(void *){
	Twindow * cur_window=&mainwindow.distfiles_win;
	while (not exit_flag){
		char key=getch();
		switch (key){
			//tab - move between windows
			case (char)9:cur_window=next_window();
			//KEY_DOWN
			case (char)2:cur_window->down(1);break;
			//KEY_RIGHT
			case (char)5:cur_window->down(1);break;
			//KEY_UP
			case (char)3:cur_window->up(1);break;
			//KEY_LEFT
			case (char)4:cur_window->up(1);break;
			//KEY_PG_DOWN
			case (char)82:cur_window->down(mainwindow.bottom_screenline_num);break;
			//KEY_PG_UP
			case (char)83:cur_window->up(mainwindow.bottom_screenline_num);break;
			case 'd':
			case 'D':mainwindow.distfiles_win.toggle();
					mainwindow.log_win.visible=false;
					mainwindow.error_log_win.visible=false;
					cur_window=&mainwindow.distfiles_win;
					mainwindow.show();
					break;
			case 'q':
			case 'Q':quit(0,"Interrupted by user");break;
			case 'h':
			case 'H':mainwindow.help_win.toggle(); mainwindow.show();break;
			case 'l':
			case 'L':mainwindow.log_win.toggle(); mainwindow.show();break;
			case 'e':
			case 'E':mainwindow.error_log_win.toggle(); mainwindow.show();break;
//			default: mainwindow.set_line(25,toString((int)key));
		}
	}
//	pthread_exit((void*)watch_keyboard_thread_status);
	return (void*)watch_keyboard_thread_status;
}

void * refresh_screen_thread_function(void *){
	while (! exit_flag){
		mainwindow.refresh();
		mainwindow.refresh();
//		sleep(1);
		struct timeval delay;
		delay.tv_sec=0;
		delay.tv_usec=300000;
		select(FD_SETSIZE, (fd_set *)0, (fd_set *)0,(fd_set *)0, (struct timeval *) &delay);//&timeout
	}
	exit_curses_and_show_msg();
	return 0;
}

void decode_connection_msg(string msg_body){
	Tparts parts=split("<y>",msg_body);
	int line_num=atoi(parts.before.c_str());
	if (line_num<200){
		mainwindow.set_line(line_num, parts.after);
//		set_line(line_num, "||"+toString(line_num)+"||("+first_part.substr(0,first_part.find("<s>")+3)+")"+msg_text);
//		set_line(line_num, "||"+toString(line_num)+"||("+original_msg);
	}else{
		mainwindow.screen_info_lines[line_num-200]=parts.after;
	}
}
/*

void decode_downloaded_distfile_msg(string msg_body){
	vector <string> parts;
	parts=split_to_vector(" ", msg_body);
	if (parts.size()>4){
//		if (settings.arg_wait_distfile!=""){
//			if (parts[0]==settings.arg_wait_distfile){
//				cant_find_distfile_exit_flag==false;
//				if
//			}
//		}
		for (ulong cur_distfile=0;cur_distfile<tuidistfiles.size();cur_distfile++){
			if (tuidistfiles[cur_distfile].name==parts[0]){
				tuidistfiles[cur_distfile].dld_segments=atol(parts[1]);
				tuidistfiles[cur_distfile].segments_count=atol(parts[2]);
				tuidistfiles[cur_distfile].dld_bytes=atol(parts[3]);
				tuidistfiles[cur_distfile].size=atol(parts[4]);
				return;
			}
		}
		Ttuidistfile a_tuidistfile;
		a_tuidistfile.name=parts[0];
		a_tuidistfile.dld_segments=atol(parts[1]);
		a_tuidistfile.segments_count=atol(parts[2]);
		a_tuidistfile.dld_bytes=atol(parts[3]);
		a_tuidistfile.size=atol(parts[4]);
		tuidistfiles.push_back(a_tuidistfile);
		mainwindow.distfiles_win.max_received_screenline_num=tuidistfiles.size();
	}
}
*/

inline void check_for_quit(ulong cur_distfile){
		if (tuidistfiles[cur_distfile].is_finished()){
			for (ulong cur_tuiwaited_distfile=0;cur_tuiwaited_distfile<settings.wait_distfiles_vector.size();cur_tuiwaited_distfile++){
				if (settings.wait_distfiles_vector[cur_tuiwaited_distfile]==tuidistfiles[cur_distfile].name){
					if (settings.wait_distfiles_vector.size()>1){
//						debug("cur_wait_distfile"+settings.wait_distfiles_vector[cur_tuiwaited_distfile]);
//						debug("wait distfiles num="+toString(settings.wait_distfiles_vector.size()));
//						debug("d0"+settings.wait_distfiles_vector[0]);
//						debug("d1"+settings.wait_distfiles_vector[1]);
						settings.wait_distfiles_vector.erase(settings.wait_distfiles_vector.begin()+cur_tuiwaited_distfile,settings.wait_distfiles_vector.begin()+cur_tuiwaited_distfile+1);
//						debug("wait distfiles num="+toString(settings.wait_distfiles_vector.size()));
//						debug("d0"+settings.wait_distfiles_vector[0]);
					}else{
						quit(0,"All distfiles have been downloaded");
					}
				}
			}
		}
}
void decode_downloaded_distfile_msg(string msg_body){
	vector <string> parts;
	parts=split_to_vector("<>", msg_body);
	if (parts.size()>5){
		for (ulong cur_distfile=0;cur_distfile<tuidistfiles.size();cur_distfile++){
			if (tuidistfiles[cur_distfile].name==parts[0]){
				// if status changes
				if (tuidistfiles[cur_distfile].status!=(Tdistfile_status)atoi(parts[1])){
					tuidistfiles[cur_distfile].status=(Tdistfile_status)atoi(parts[1]);
					check_for_quit(cur_distfile);
				}else{
					tuidistfiles[cur_distfile].status=(Tdistfile_status)atoi(parts[1]);
				}

				tuidistfiles[cur_distfile].status=(Tdistfile_status)atoi(parts[1]);
				tuidistfiles[cur_distfile].dld_segments=atol(parts[2]);
				tuidistfiles[cur_distfile].segments_count=atol(parts[3]);
				tuidistfiles[cur_distfile].dld_bytes=atol(parts[4]);
				tuidistfiles[cur_distfile].size=atol(parts[5]);
				return;
			}
		}
		Ttuidistfile a_tuidistfile;
		tuidistfiles.push_back(a_tuidistfile);
		ulong new_distfile=tuidistfiles.size()-1;
		tuidistfiles[new_distfile].name=parts[0];
		tuidistfiles[new_distfile].status=(Tdistfile_status)atoi(parts[1]);
		check_for_quit(new_distfile);
		tuidistfiles[new_distfile].dld_segments=atol(parts[2]);
		tuidistfiles[new_distfile].segments_count=atol(parts[3]);
		tuidistfiles[new_distfile].dld_bytes=atol(parts[4]);
		tuidistfiles[new_distfile].size=atol(parts[5]);
		// wont work here
	}
}
		
void decode_log_msg(string msg_body){
	mainwindow.log_win.add_line(msg_body);
}

void decode_error_log_msg(string msg_body){
	mainwindow.error_log_win.add_line(msg_body);
}
int parse_cli_arguments(int argc, char* argv[]){
	try{
		string option,name,value;
		cout << "argc = " << argc << endl;
		int posEqual;
		for(int i = 0; i < argc; i++){
			cout << "argv[" << i << "] = " << argv[i] << endl; 
			option=argv[i];
			posEqual=option.find('=');
			name  = trim(option.substr(0,posEqual));
			value = trim(option.substr(posEqual+1));
			if (name=="--wait-distfiles"){
				settings.arg_wait_distfiles=value;
				settings.wait_distfiles_vector=split_to_vector(",", value);
				continue;
			};
//		//f (name=="--pkglist-file") {settings.pkg_list_file=value; continue;};
		}
		return 0;
	}catch(...){
		perror("Error in segget.cpp: init_curses()");
	}
	return 1;
}

void no_curses_msg(string msg){
	cout << msg << endl;
}

void exit_curses_and_show_msg(){
	try{
//		sleep(1);
//		nocbreak();
		endwin();
		try{
			close(sockfd);
		}catch(...){
			no_curses_msg("error while closing socket");
		}
		no_curses_msg(exit_msg);
		exit(exit_code);
	}catch(...){
		//error while ending curses
	}
}

void quit(int exit_code_, string exit_msg_){
//		mainwindow.exit_flag=TRUE;
		exit_code=exit_code_;
		exit_msg=exit_msg_;
		exit_flag=true;
		//wait to exit
		while (1){
			sleep(10);
		}
	//		nodelay(mainwindow.window,0);
//		sleep(1); // wait for thread to finish its job
//		void *status;
//		pthread_join(watch_keyboard_thread, &status);
//		if ((int)status != watch_keyboard_thread_status) {
//			printf("Secondary thread failed\n");
//		exit(1);
//		}
//		pthread_join(watch_keyboard_thread, 0);
}

int main(int argc, char* argv[])
{
	try{
		parse_cli_arguments(argc, argv);
	}catch(...){
		perror("error while parsing arguments");
	}
	try{
		try{
			//init curses
			settings.init();
			mainwindow.init();
//			init_color(COLOR_BLUE, 0, 0, 0); 
		}catch(...){
			//error while init curses
		}
		try{
			pthread_t refresh_screen_thread;
			int iret1;
			iret1 = pthread_create( &refresh_screen_thread, NULL, refresh_screen_thread_function, (void*) NULL);
		}catch(...){
			//error while init refresh_screen_thread;
		}
		try{
			int iret1;
			iret1 = pthread_create( &watch_keyboard_thread, NULL, watch_keyboard_thread_function, (void*) NULL);
		}catch(...){
			//error while init curses watch_keyboard_thread;
		}
		try{
//			struct timeval timeout;
			while ( ! exit_flag ){
				int len;

				struct sockaddr_in address;
				string rest_of_the_msg, first_part, msg_text;

				//Create a socket for the client:
				int result=-1;
				while (result==-1){
					mainwindow.attempt_num++;
					sockfd = socket(AF_INET, SOCK_STREAM, 0);

					//Name the socket, as agreed with the server:
					address.sin_family = AF_INET;
					address.sin_addr.s_addr = inet_addr(settings.ui_ip.c_str());
					address.sin_port = htons(settings.ui_port);
					len = sizeof(address);
					//Connect your socket to the server’s socket:
					result = connect(sockfd, (struct sockaddr *)&address, len);
					if(result == -1) {
						mainwindow.disconnected();
						close(sockfd);
						sleep(1);
					}
				}

				debug("connected");
				mainwindow.connected();
				if (settings.arg_wait_distfiles.length()>9999){
					quit(1,"Error in argument --wait-distfile="+settings.arg_wait_distfiles+" : Distfile name is too long");
				};
				char send_buffer[10000];
				string send_msg="<d>"+settings.arg_wait_distfiles+"<.>";
				strcpy(send_buffer,send_msg.c_str());
				if (write(sockfd, send_buffer, strlen(send_buffer))!=(int)send_msg.length()){
					perror("Error in request.cpp: request(): request msg size and sent data size are different.");
				}
				fd_set readfds, testfds;
				FD_ZERO(&readfds);
				FD_SET(sockfd, &readfds);
				testfds = readfds;
				bool run_flag=true;
				while (run_flag){
					//init each time !
//					timeout.tv_sec=1;
//					timeout.tv_usec=0;
					result = select(FD_SETSIZE, &testfds, (fd_set *)0,
						(fd_set *)0, (struct timeval *) 0);//&timeout
					// if fd is set - it's not because of timeout - read data
					if (FD_ISSET(sockfd, &testfds)){
						int nread;
						ioctl(sockfd, FIONREAD, &nread);
						if(nread == 0) {
							close(sockfd);
//							FD_CLR(sockfd, &readfds);
//							printf("removing client on fd %d\n", sockfd);
							run_flag=false;
						}else {
							char recv_buffer[BUFFER_SIZE+1]=" ";
							if (nread!=read(sockfd, recv_buffer, BUFFER_SIZE)){
								error_log("Error in tuiclient.cpp : main() read bytes count does NOT match declared count.");
							};
							//recv_msg=recv_msg+recv_buffer;
//							rest_of_the_msg=rest_of_the_msg+recv_buffer;
							rest_of_the_msg=recv_buffer;
							debug("RECEIVED:"+rest_of_the_msg);
							debug("END============================");
							Tparts msg_parts;
							vector<string> submsgs=split_to_vector("<.>", rest_of_the_msg);
							for (ulong submsg_num=0; submsg_num<submsgs.size(); submsg_num++){
								debug("Submsg: "+submsgs[submsg_num]);
								vector<string> before_msg_and_msg=split_to_vector("<m>",submsgs[submsg_num]);
								debug("Size:"+toString(submsgs.size()));
								if (before_msg_and_msg.size()>1){
									vector<string> msg_type_and_body=split_to_vector("<t>",before_msg_and_msg[1]);
									if (msg_type_and_body.size()>1){
										debug("msg_body="+msg_type_and_body[1]);
									}else{
										debug("No msg_body found");
									}
									if (msg_type_and_body.size()>0){
										debug("msg_type="+msg_type_and_body[0]);
										char msg_type=msg_type_and_body[0][0];
//										debug("msg_type="+msg_type_str);
										switch (msg_type){
											case 'c': decode_connection_msg(msg_type_and_body[1]); break;
											case 'l': decode_log_msg(msg_type_and_body[1]);break;
											case 'e': decode_error_log_msg(msg_type_and_body[1]);break;
											case 'd': decode_downloaded_distfile_msg(msg_type_and_body[1]);break;
											// yes-distfile present in the queue
											case 'y': break; // continue waiting for catchup info
											// no distfile in the queue
											case 'n': 
												quit(1,"Distfile(s): "+settings.arg_wait_distfiles+" is(are) not in the queue - quit");
												break; //shouldn't get to this point - but just in case
											case 'N': 
												quit(0,"Distfile(s): "+settings.arg_wait_distfiles+" is(are) already downloaded - quit");
												break; //shouldn't get to this point - but just in case
										}
									}
								}
							}
						};
					}
				}
				close(sockfd);
			}
		}catch(...){
//			error_log_no_msg("Error in segget.cpp launch_tui_theread() failed");
		}
	
	}catch(...){
		perror("error during init and downloading process");
	}
	while (1){
		sleep(10);
	}
	quit(0,"");
}