summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKostyantyn Ovechko <fastinetserver@gmail.com>2010-07-12 08:04:59 +0300
committerKostyantyn Ovechko <fastinetserver@gmail.com>2010-07-12 08:04:59 +0300
commit3e628375bbe27352be1f35e9e5a2155dc63528f7 (patch)
tree9be1a45b32986231135f324a619c3a9571e91943
parentAdd colors to tuiclient interface (diff)
downloadidfetch-3e628375bbe27352be1f35e9e5a2155dc63528f7.tar.gz
idfetch-3e628375bbe27352be1f35e9e5a2155dc63528f7.tar.bz2
idfetch-3e628375bbe27352be1f35e9e5a2155dc63528f7.zip
Add scrolling to tuiclient
-rw-r--r--segget/segget.cpp69
-rw-r--r--segget/segget.h2
-rw-r--r--segget/tui.cpp41
-rw-r--r--segget/ui_server.cpp41
-rw-r--r--segget/ui_server.h13
-rw-r--r--tuiclient/Makefile3
-rw-r--r--tuiclient/tuiclient.cpp178
7 files changed, 202 insertions, 145 deletions
diff --git a/segget/segget.cpp b/segget/segget.cpp
index ca93aa5..dc2692c 100644
--- a/segget/segget.cpp
+++ b/segget/segget.cpp
@@ -267,58 +267,18 @@ int download_pkgs(){
void *print_message_function( void *ptr );
void launch_tui_thread(){
- pthread_t thread1;
-// , thread2;
-// char *message1 = "Thread 1";
-// char *message2 = "Thread 2";
- int iret1;
-// int iret2;
-
- /* Create independent threads each of which will execute function */
-
- iret1 = pthread_create( &thread1, NULL, print_message_function, (void*) NULL);
-// iret2 = pthread_create( &thread2, NULL, print_message_function, (void*) message2);
-
- /* Wait till threads are complete before main continues. Unless we */
- /* wait we run the risk of executing an exit which will terminate */
- /* the process and all threads before the threads have completed. */
-
- debug("oooooooooooooo111111111111111111");
-// pthread_join( thread1, NULL);
- debug("oooooooooooooo2222222222222222222222");
-// pthread_join( thread2, NULL);
-
-// printf("Thread 1 returns: %d\n",iret1);
-// printf("Thread 2 returns: %d\n",iret2);
+ pthread_t tui_thread;
+ int iret1;
+ iret1 = pthread_create( &tui_thread, NULL, print_message_function, (void*) NULL);
}
void launch_ui_server_thread(){
- pthread_t thread1;
-// , thread2;
-// char *message1 = "Thread 1";
-// char *message2 = "Thread 2";
- int iret1;
-// int iret2;
-
- /* Create independent threads each of which will execute function */
-
- debug_no_msg("Creating ui_server_thread.");
- ui_server.init();
-
- iret1 = pthread_create( &thread1, NULL, run_ui_server, (void*) NULL);
-// iret2 = pthread_create( &thread2, NULL, print_message_function, (void*) message2);
- debug_no_msg("ui_server_lanched");
- /* Wait till threads are complete before main continues. Unless we */
- /* wait we run the risk of executing an exit which will terminate */
- /* the process and all threads before the threads have completed. */
-
-// debug("oooooooooooooo111111111111111111");
-// pthread_join( thread1, NULL);
-// debug("oooooooooooooo2222222222222222222222");
-// pthread_join( thread2, NULL);
-
-// printf("Thread 1 returns: %d\n",iret1);
-// printf("Thread 2 returns: %d\n",iret2);
+ pthread_t ui_server_thread;
+ int iret1;
+ debug_no_msg("Creating ui_server_thread.");
+ ui_server.init();
+ iret1 = pthread_create( &ui_server_thread, NULL, run_ui_server, (void*) NULL);
+ debug_no_msg("ui_server_lanched");
}
void segget_exit(int sig){
@@ -420,21 +380,14 @@ int main()
}
void *print_message_function(void *ptr){
-// char *message;
- char * args;
- args = (char *) ptr;
- ulong i=0;
+ char * args = (char *) ptr;
while (true){
ulong time_diff_msecs=time_left_from(stats.previous_time);
if (time_diff_msecs >= settings.current_speed_time_interval_msecs){
show_progress(time_diff_msecs);
};
- log("Thread:"+toString(i));
- //haven't downloaded anything - anyway, show totals
- stats.show_totals();
+ stats.show_totals();
sleep(1);
- i++;
}
return 0;
-// printf("%s \n", message);
} \ No newline at end of file
diff --git a/segget/segget.h b/segget/segget.h
index 0291149..f224609 100644
--- a/segget/segget.h
+++ b/segget/segget.h
@@ -34,6 +34,7 @@
#include <iostream>
#include <json/json.h>
#include <ncurses.h>
+#include <pthread.h>
#include "checksum.cpp"
#include "config.cpp"
#include "connection.cpp"
@@ -48,7 +49,6 @@
#include "str.cpp"
#include "tui.cpp"
#include "utils.cpp"
-#include <pthread.h>
#include "ui_server.cpp"
using namespace std;
diff --git a/segget/tui.cpp b/segget/tui.cpp
index 60f9074..ade8e68 100644
--- a/segget/tui.cpp
+++ b/segget/tui.cpp
@@ -29,21 +29,23 @@ extern Tsettings settings;
const uint CONNECTION_LINES=5;
-bool msg_idle=true;
+//bool msg_idle=true;
void msg(uint y, uint x, string msg_text){
- if (msg_idle){
- msg_idle=false;
+// if (msg_idle){
+// msg_idle=false;
try{
- ui_server.send(y,msg_text);
- move(y,x);
- string ready_msg_text=msg_text+" ";
- printw(ready_msg_text.c_str());
+ if (max_published_screenline_num<y && y<MAX_LINES) max_published_screenline_num=y;
+ screenlines[y]=msg_text;
+ ui_server.send_all_clients(y,msg_text);
+// string ready_msg_text=msg_text+" ";
+ string ready_msg_text=msg_text+"";
+ mvaddstr(y,x,ready_msg_text.c_str());
refresh();
}catch(...){
error_log_no_msg("Error in tui.cpp: msg()");
}
- msg_idle=true;
- }
+// msg_idle=true;
+// }
}
void msg_connecting(uint connection_num, uint distfile_num, uint segment_num, string msg_text){
@@ -98,10 +100,15 @@ void msg_status2(uint connection_num, string msg_text){
}
void msg_clean_connection(uint connection_num){
try{
- msg(connection_num*CONNECTION_LINES,0," ");
- msg(connection_num*CONNECTION_LINES+1,0," ");
- msg(connection_num*CONNECTION_LINES+2,0," ");
- msg(connection_num*CONNECTION_LINES+3,0," ");
+ msg(connection_num*CONNECTION_LINES,0,"");
+ msg(connection_num*CONNECTION_LINES+1,0,"");
+ msg(connection_num*CONNECTION_LINES+2,0,"");
+ msg(connection_num*CONNECTION_LINES+3,0,"");
+
+// msg(connection_num*CONNECTION_LINES,0," ");
+// msg(connection_num*CONNECTION_LINES+1,0," ");
+// msg(connection_num*CONNECTION_LINES+2,0," ");
+// msg(connection_num*CONNECTION_LINES+3,0," ");
}catch(...){
error_log_no_msg("Error in tui.cpp: msg_clean_connection()");
}
@@ -115,14 +122,14 @@ void msg_error(string error_text){
}
void msg_total(string msg_text){
try{
- msg(settings.max_connections*CONNECTION_LINES,0,msg_text);
+ msg(TOTAL_LINE_NUM,0,msg_text);
}catch(...){
error_log_no_msg("Error in tui.cpp: msg_total()");
}
}
void log(string log_msg_text){
try{
- msg(settings.max_connections*CONNECTION_LINES+1,0, "LOG:"+log_msg_text);
+ msg(LOG_LINE_NUM,0, "LOG:"+log_msg_text);
ofstream file;
file.exceptions (ofstream::failbit | ofstream::badbit);
try{
@@ -145,7 +152,7 @@ void log(string log_msg_text){
}
void debug(string debug_msg_text){
try{
-// msg(settings.max_connections*CONNECTION_LINES+2,0, "DEBUG:"+debug_msg_text);
+// msg(DEBUG_LINE_NUM,0, "DEBUG:"+debug_msg_text);
debug_no_msg(debug_msg_text);
}catch(...){
error_log("Error in tui.cpp: debug()");
@@ -197,7 +204,7 @@ void error_log(string error_msg_text){
fprintf(stderr, "Error log file: %s/%s",settings.logs_dir.c_str(),settings.error_log_file.c_str());
}
try{
- msg(settings.max_connections*CONNECTION_LINES+3,0, "ERROR:"+error_msg_text);
+ msg(ERROR_LINE_NUM,0, "ERROR:"+error_msg_text);
}catch(...){
error_log_no_msg("Error in tui.cpp: error_log()");
}
diff --git a/segget/ui_server.cpp b/segget/ui_server.cpp
index b6efbdc..6cb000f 100644
--- a/segget/ui_server.cpp
+++ b/segget/ui_server.cpp
@@ -57,23 +57,34 @@ void Tui_server::init(){
listen(server_sockfd, 5);
FD_ZERO(&readfds);
FD_SET(server_sockfd, &readfds);
+ send_to_fd_busy=false;
}
-
-ulong Tui_server::send(uint y, string msg){
-
- for(uint fd = 0; fd <= ui_server.max_fd_num; fd++) {
- if (fd !=server_sockfd){
- if(FD_ISSET(fd,&ui_server.testfds)) {
- string message="<y>"+toString(y)+"<s>"+msg+"<.>";
- return write(fd, message.c_str(), message.length());
- }
- }
+//prevent simultaneous writes
+ulong Tui_server::send_to_fd(uint fd, uint y, string msg){
+// if (send_to_fd_idle) {
+ while (send_to_fd_busy){
+ sleep(1);
+ }
+ send_to_fd_busy=true;
+ if (fd !=server_sockfd){
+ if(FD_ISSET(fd,&ui_server.readfds)) {
+ string message="<y>"+toString(y)+"<s>"+msg+"<.>";
+ write(fd, message.c_str(), message.length());
}
+ }
+ send_to_fd_busy=false;
return 0;
}
+void Tui_server::send_all_clients(uint y, string msg){
+ for(uint fd = 0; fd <= ui_server.max_fd_num; fd++){
+ send_to_fd(fd, y, msg);
+ }
+}
+
void *run_ui_server(void * ){
while(1) {
+ max_published_screenline_num=0;
uint fd;
int nread;
ui_server.testfds = ui_server.readfds;
@@ -101,7 +112,17 @@ void *run_ui_server(void * ){
debug("Connected new ui client");
if (client_sockfd>ui_server.max_fd_num) ui_server.max_fd_num=client_sockfd;
+
FD_SET(client_sockfd, &ui_server.readfds);
+
+ // Get this info to catch up!
+ for (uint line_num=0; line_num<=max_published_screenline_num;line_num++){
+ ui_server.send_to_fd(client_sockfd, line_num, screenlines[line_num]);
+ debug_no_msg("Sending to client line:"+toString(line_num)+" "+screenlines[line_num]);
+// ui_server.send(line_num,screenlines[line_num]);
+ }
+
+
//If it isn’t the server, it must be client activity. If close is received, the client has gone away, and you remove it from the descriptor set. Otherwise, you “serve” the client as in the previous examples.
}else{
ioctl(fd, FIONREAD, &nread);
diff --git a/segget/ui_server.h b/segget/ui_server.h
index 225dd7b..e33e94a 100644
--- a/segget/ui_server.h
+++ b/segget/ui_server.h
@@ -41,14 +41,25 @@
using namespace std;
#define MAX_BIND_ATTEMPTS 5
+const uint MAX_LINES=200;
+const uint TOTAL_LINE_NUM=MAX_LINES;
+const uint ERROR_LINE_NUM=MAX_LINES+1;
+const uint LOG_LINE_NUM=MAX_LINES+2;
+const uint DEBUG_LINE_NUM=MAX_LINES+3;
+
+//const uint MAX_LINES=200;
+string screenlines[DEBUG_LINE_NUM+1];
+uint max_published_screenline_num;
class Tui_server{
public:
+ bool send_to_fd_busy;
uint server_sockfd;
uint max_fd_num;
fd_set readfds, testfds;
void init();
- ulong send(uint y, string msg);
+ ulong send_to_fd(uint fd, uint y, string msg);
+ void send_all_clients(uint y, string msg);
};
Tui_server ui_server;
diff --git a/tuiclient/Makefile b/tuiclient/Makefile
index cfea393..f3176ee 100644
--- a/tuiclient/Makefile
+++ b/tuiclient/Makefile
@@ -1,4 +1,3 @@
-
BINS = tuiclient
OBJS = $(addsuffix .o,$(BINS))
PKGCONFIG_MODULES =
@@ -12,7 +11,7 @@ CXXFLAGS_OPT = -O2 -g
LIBS = $(LIBS_PKGCONFIG)
#CIBS = -lncurses
-CXXFLAGS = $(CXXFLAGS_PKGCONFIG) -lncurses $(CXXFLAGS_WARNINGS) $(CXXFLAGS_OPT)
+CXXFLAGS = $(CXXFLAGS_PKGCONFIG) -lncurses -lpthread $(CXXFLAGS_WARNINGS) $(CXXFLAGS_OPT)
all: clean $(BINS)
diff --git a/tuiclient/tuiclient.cpp b/tuiclient/tuiclient.cpp
index c938f28..85f5baf 100644
--- a/tuiclient/tuiclient.cpp
+++ b/tuiclient/tuiclient.cpp
@@ -10,48 +10,49 @@
#include <string>
#include <string.h>
#include <ncurses.h>
+#include <pthread.h>
#include "str.cpp"
-#define BUFFER_SIZE 1000
+#define BUFFER_SIZE 2000
+
+#define TOTAL_LINE_NUM 200
+#define ERROR_LINE_NUM 201
+#define LOG_LINE_NUM 202
+#define DEBUG_LINE_NUM 203
using namespace std;
WINDOW * screen;
int max_x, max_y;
-int mx;
+int max_received_screenline_num;
+int last_screenline_num;
void show_lines();
const uint CONNECTION_LINES=5;
const uint MAX_LINES=200;
-string screenlines[MAX_LINES];
+int top_position;
+string screenlines[200];
+string screen_info_lines[4];
string status_str;
-bool msg_idle=true;
void msg_line(uint y, string msg_text){
- if (msg_idle){
- msg_idle=false;
- try{
- msg_text=msg_text+" ";
- mvaddstr(y,1,msg_text.substr(0,max_x-2).c_str());
-// mvwprintw(screen,y,1,ready_msg_text.c_str());
- wrefresh(screen);
- }catch(...){
-// error_log_no_msg("Error in tui.cpp: msg()");
- }
- msg_idle=true;
+ try{
+// cout << msg_text;
+ msg_text=msg_text+" ";
+ mvaddstr(y,1,msg_text.substr(0,max_x-2).c_str());
+// mvwprintw(screen,y,1,ready_msg_text.c_str());
+// mvwprintw(screen,y,1,msg_text.substr(0,max_x-2).c_str());
+// mvaddstr();
+ }catch(...){
+// error_log_no_msg("Error in tui.cpp: msg()");
}
}
void msg_short(uint y, uint x, string msg_text){
- if (msg_idle){
- msg_idle=false;
- try{
- mvaddstr(y,x,msg_text.c_str());
- wrefresh(screen);
- }catch(...){
-// error_log_no_msg("Error in tui.cpp: msg()");
- }
- msg_idle=true;
+ try{
+ mvaddstr(y,x,msg_text.c_str());
+ }catch(...){
+// error_log_no_msg("Error in tui.cpp: msg()");
}
}
void set_status(string str){
@@ -59,32 +60,87 @@ void set_status(string str){
show_lines();
}
-void msg_status(){
- if(has_colors())
- {
+void color_downloads(){
+ if(has_colors()){
start_color(); /* Start color */
init_pair(1, COLOR_WHITE, COLOR_BLUE);
- init_pair(2, COLOR_RED, COLOR_BLUE);
- attron(COLOR_PAIR(2));
- msg_short(max_y-1,2,status_str);
-// attroff(COLOR_PAIR(2));
attron(COLOR_PAIR(1));
- }else{
- msg_short(max_y-1,2,status_str);
}
}
+void color_info(){
+ if(has_colors()){
+ start_color(); /* Start color */
+ init_pair(2, COLOR_BLACK, COLOR_CYAN);
+ attron(COLOR_PAIR(2));
+ }
+}
+
+void color_status(){
+ if(has_colors()){
+ start_color(); /* Start color */
+ init_pair(3, COLOR_RED, COLOR_BLUE);
+ attron(COLOR_PAIR(3));
+ }
+}
+
+
+void msg_status(){
+ msg_short(0,max_x/2-13,"tuiclient to segget daemon");
+ msg_short(max_y-1,2,status_str);
+ msg_short(max_y-1,max_x-10,"[q-Quit]");
+}
+
void show_lines(){
- clear();
+ //clear();
box(screen, ACS_VLINE, ACS_HLINE);
- for (int y=1; y<max_y-1; y++){
- msg_line(y,screenlines[y]);
- }
+ getmaxyx(screen,max_y,max_x);
+ last_screenline_num=max_y-6;
+ color_status();
msg_status();
+ color_info();
+ for (int y=max_y-6,status_line_num=0; y<max_y-2; y++,status_line_num++){
+ msg_line(y+1,screen_info_lines[status_line_num]);
+ }
+ color_downloads();
+ screenlines[26]="Max_num:"+toString(max_received_screenline_num);
+ for (int y=0, line_num=top_position; y<max_y-6; y++, line_num++){
+ msg_line(y+1,screenlines[line_num]);
+ }
+ wrefresh(screen);
+ refresh();
}
void set_line(uint y, string msg_text){
screenlines[y]=msg_text;
+ if (max_received_screenline_num<y) max_received_screenline_num=y;
+}
+void updown(int inc){
+ int tmp_top_position=top_position+inc;
+ if (tmp_top_position>=0 && tmp_top_position<(max_received_screenline_num-last_screenline_num)){
+ top_position=tmp_top_position;
+ }
+}
+
+void * watch_keyboard_thread_function(void * ptr){
+ char * args = (char *) ptr;
+ while (true){
+ char key=getch();
+ switch (key){
+ //KEY_DOWN
+ case (char)2:updown(1);break;
+ //KEY_RIGHT
+ case (char)5:updown(1);break;
+ //KEY_UP
+ case (char)3:updown(-1);break;
+ //KEY_LEFT
+ case (char)4:updown(-1);break;
+ case 'u':updown(1);break;
+ case 'd':updown(-1);break;
+// default: screenlines[25]=toString((int)key);
+ }
+ }
+ return 0;
}
int main()
@@ -94,26 +150,26 @@ int main()
//init curses
screen=initscr();
curs_set(0);
+ //don't echo keyboard to the screen
noecho();
- refresh();
-// WINDOW * spad;
-// spad=newpad(100,100);
-// box(spad, ACS_VLINE, ACS_HLINE);
-// mvwprintw(spad,1,1,"--wwwwwwwwwwwooooooooooowowwwwwwwwww --------");
-// wrefresh(spad);
-
-
-
-// getch();
-
-// screen = newwin(max_y, 0, 0, 0);
- getmaxyx(screen,max_y,max_x);
- wrefresh(screen);
+ //don't wait for enter
+ cbreak();
+ //enable arrow keys
+ keypad(screen,true);
+ top_position=0;
+ max_received_screenline_num=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
}
-
try{
while (true){
int sockfd;
@@ -161,17 +217,27 @@ int main()
// printf("removing client on fd %d\n", sockfd);
run_flag=false;
}else {
- char recv_buffer[BUFFER_SIZE];
+ char recv_buffer[BUFFER_SIZE+1]="";
read(sockfd, recv_buffer, BUFFER_SIZE);
+ //recv_msg=recv_msg+recv_buffer;
+ recv_msg="MMM";
recv_msg=recv_msg+recv_buffer;
+// cout<<recv_buffer;
while (recv_msg.find("<.>")!=recv_msg.npos){
+ string original_msg=recv_msg;
recv_msg=recv_msg.substr(recv_msg.find("<y>")+3,recv_msg.npos);
first_part=recv_msg.substr(0,recv_msg.find("<.>"));
- recv_msg=recv_msg.substr(recv_msg.find("<.>")+3,recv_msg.npos);
- uint y=atoi(first_part.substr(0,first_part.find("<s>")).c_str());
+ recv_msg=recv_msg.substr(recv_msg.find("<.>")+3);
+ uint line_num=atoi(first_part.substr(0,first_part.find("<s>")).c_str());
msg_text=first_part.substr(first_part.find("<s>")+3,first_part.npos);
- set_line(y+1, msg_text);
+ if (line_num<200){
+ set_line(line_num, msg_text);
+// 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{
+ screen_info_lines[line_num-200]=msg_text;
+ }
show_lines();
}
};