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
|
/*
* 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 "tuiclient.h"
void quit(){
mainwindow.exit_flag=TRUE;
try{
close(sockfd);
}
catch(...)
{
//error while closing socket
}
try{
close(sockfd);
// sleep(1);
nocbreak();
endwin();
exit(0);
}
catch(...)
{
//error while ending curses
}
}
void * watch_keyboard_thread_function(void *){
while (true){
char key=getch();
switch (key){
//KEY_DOWN
case (char)2:mainwindow.down(1);break;
//KEY_RIGHT
case (char)5:mainwindow.down(1);break;
//KEY_UP
case (char)3:mainwindow.up(1);break;
//KEY_LEFT
case (char)4:mainwindow.up(1);break;
//KEY_PG_DOWN
case (char)82:mainwindow.down(mainwindow.bottom_screenline_num);break;
//KEY_PG_UP
case (char)83:mainwindow.up(mainwindow.bottom_screenline_num);break;
case 'u':
case 'U':mainwindow.up(1);break;
case 'd':
case 'D':mainwindow.down(1);break;
case 'q':
case 'Q':quit();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: screenlines[25]=toString((int)key);
}
}
return 0;
}
void * refresh_screen_thread_function(void *){
while (! mainwindow.exit_flag){
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
}
return 0;
}
Tparts split(string splitter, string str){
Tparts result;
int splitter_pos=str.find(splitter);
result.before=str.substr(0,splitter_pos);
result.after=str.substr(splitter_pos+splitter.length());
return result;
}
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_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 main()
{
try{
try{
//init curses
settings.init();
mainwindow.init();
// init_color(COLOR_BLUE, 0, 0, 0);
}catch(...)
{
//error while init curses
}
try{
pthread_t watch_keyboard_thread;
int iret1;
iret1 = pthread_create( &watch_keyboard_thread, NULL, watch_keyboard_thread_function, (void*) NULL);
}catch(...)
{
//error while init curses watch_keyboard_thread;
}
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{
// struct timeval timeout;
while ( ! mainwindow.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);
}
}
mainwindow.connected();
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;
Tparts msg_parts;
while (rest_of_the_msg.find("<.>")!=rest_of_the_msg.npos){
msg_parts=split("<m>",rest_of_the_msg);
msg_parts=split("<t>",msg_parts.after);
char msg_type=msg_parts.before[0];
debug("msg_type="+msg_type);
msg_parts=split("<.>",msg_parts.after);
string msg_body=msg_parts.before;
debug("msg_body="+msg_body);
rest_of_the_msg=msg_parts.after;
switch (msg_type){
case 'c': decode_connection_msg(msg_parts.before); break;
case 'l': decode_log_msg(msg_parts.before);break;
case 'e': decode_error_log_msg(msg_parts.before);break;
}
}
};
}
}
close(sockfd);
}
}catch(...){
// error_log_no_msg("Error in segget.cpp launch_tui_theread() failed");
}
}
catch(...){
//error during init and downloading process
}
try{
nocbreak();
endwin();
}
catch(...)
{
//error while ending curses
}
return 0;
}
|