summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKostyantyn Ovechko <fastinetserver@gmail.com>2010-07-11 17:47:31 +0300
committerKostyantyn Ovechko <fastinetserver@gmail.com>2010-07-11 17:47:31 +0300
commit6ca252a4f14c2b9a6f84f628bba2d768d06914c1 (patch)
tree514fb658b97e6edb3364cc162e3a50d488bef1dd
parentMove show_progress() to a thread (diff)
downloadidfetch-6ca252a4f14c2b9a6f84f628bba2d768d06914c1.tar.gz
idfetch-6ca252a4f14c2b9a6f84f628bba2d768d06914c1.tar.bz2
idfetch-6ca252a4f14c2b9a6f84f628bba2d768d06914c1.zip
Separate text based ui from segget into a tuiclient.
Prepare segget for daemon phase. Segget gets ui_server interface and tuiclient in curses as a separate app.
-rw-r--r--segget/segget.cpp122
-rw-r--r--segget/segget.h2
-rw-r--r--segget/tui.cpp11
-rw-r--r--segget/tui.h4
-rw-r--r--segget/ui_server.cpp121
-rw-r--r--segget/ui_server.h58
-rw-r--r--tuiclient/Makefile52
-rw-r--r--tuiclient/str.cpp47
-rw-r--r--tuiclient/str.h35
-rw-r--r--tuiclient/tui.cpp45
-rw-r--r--tuiclient/tui.h33
-rw-r--r--tuiclient/tuiclient.cpp114
12 files changed, 617 insertions, 27 deletions
diff --git a/segget/segget.cpp b/segget/segget.cpp
index 315ff35..ca93aa5 100644
--- a/segget/segget.cpp
+++ b/segget/segget.cpp
@@ -266,9 +266,89 @@ 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);
+}
+
+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);
+}
+
+void segget_exit(int sig){
+ try{
+ endwin();
+ }
+ catch(...)
+ {
+ //error while ending curses
+ }
+ try{
+ for(uint fd = 0; fd <= ui_server.max_fd_num; fd++) {
+ close(fd);
+ }
+ }
+ catch(...)
+ {
+ //error while clossing server_sockfd
+ }
+ error_log_no_msg("Segget exits because cought signal:"+toString(sig));
+ cout<<"Bye!\n\n";
+ exit(0);
+}
+
int main()
{
try{
+ signal(SIGABRT,segget_exit);//If program aborts go to assigned function "segget_exit".
+ signal(SIGTERM,segget_exit);//If program terminates go to assigned function "segget_exit".
+ signal(SIGINT,segget_exit);//If program terminates go to assigned function "segget_exit".
prev_time=time((time_t *)NULL);
try{
//init curses
@@ -289,6 +369,16 @@ int main()
//error while loading settings
}
try{
+ launch_ui_server_thread();
+ }catch(...){
+ error_log_no_msg("Error in segget.cpp launch_ui_server() failed");
+ }
+ try{
+ launch_tui_thread();
+ }catch(...){
+ error_log_no_msg("Error in segget.cpp launch_tui_theread() failed");
+ }
+ try{
load_pkgs();
}
catch(...){
@@ -301,31 +391,6 @@ int main()
catch(...){
//error while showing stats
}
- 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);
-
-
try{
download_pkgs();
}
@@ -344,6 +409,13 @@ int main()
{
//error while ending curses
}
+ try{
+ close(ui_server.server_sockfd);
+ }
+ catch(...)
+ {
+ //error while clossing server_sockfd
+ }
return 0;
}
diff --git a/segget/segget.h b/segget/segget.h
index 58a9e57..0291149 100644
--- a/segget/segget.h
+++ b/segget/segget.h
@@ -28,6 +28,7 @@
#define __SEGGET_H__
#include <stdio.h>
+#include <signal.h>
#include <stdlib.h>
#include <fstream>
#include <iostream>
@@ -48,6 +49,7 @@
#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 341be4d..60f9074 100644
--- a/segget/tui.cpp
+++ b/segget/tui.cpp
@@ -34,6 +34,7 @@ void msg(uint y, uint x, string msg_text){
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());
@@ -144,7 +145,15 @@ 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(settings.max_connections*CONNECTION_LINES+2,0, "DEBUG:"+debug_msg_text);
+ debug_no_msg(debug_msg_text);
+ }catch(...){
+ error_log("Error in tui.cpp: debug()");
+ }
+}
+
+void debug_no_msg(string debug_msg_text){
+ try{
ofstream file;
file.exceptions (ofstream::failbit | ofstream::badbit);
try{
diff --git a/segget/tui.h b/segget/tui.h
index c90ffc5..a09f4e3 100644
--- a/segget/tui.h
+++ b/segget/tui.h
@@ -27,10 +27,11 @@
#ifndef __TUI_H__
#define __TUI_H__
#include "settings.h"
+#include "ui_server.h"
using namespace std;
-void msg_total(string msg_text);
+void msg_total(string msg_text);
void msg(uint y, uint x, string msg_text);
void msg_connecting(uint connection_num, uint distfile_num, uint segment_num, string msg_text);
void msg_segment_progress(uint connection_num, uint network_num, uint segment_num, uint try_num, ulong dld_bytes, ulong total_bytes, ulong speed, ulong avg_speed);
@@ -41,6 +42,7 @@ void msg_error(string error_text);
void msg_total(string msg_text);
void log(string log_msg_text);
+void debug_no_msg(string debug_msg_text);
void debug(string debug_msg_text);
void error_log(string error_msg_text);
void error_log_no_msg(string error_msg_text);
diff --git a/segget/ui_server.cpp b/segget/ui_server.cpp
new file mode 100644
index 0000000..b6efbdc
--- /dev/null
+++ b/segget/ui_server.cpp
@@ -0,0 +1,121 @@
+/*
+* 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 "ui_server.h"
+
+void Tui_server::init(){
+ socklen_t server_len;
+ struct sockaddr_in server_address;
+ // Create and name a socket for the server:
+ server_sockfd = socket(AF_INET, SOCK_STREAM, 0);
+ // Set socket options. We would like the socket to disappear
+ // as soon as it has been closed for whatever reason.
+ // Set socket options.
+ // Allow local port reuse in TIME_WAIT.
+
+ int on=1;
+ setsockopt(server_sockfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
+ max_fd_num=server_sockfd;
+
+ server_address.sin_family = AF_INET;
+ //server_address.sin_addr.s_addr = htonl(INADDR_ANY);
+ string bind_address="127.0.0.1";
+ server_address.sin_addr.s_addr = inet_addr(bind_address.c_str());
+ server_address.sin_port = htons(9999);
+ server_len = sizeof(server_address);
+ int res;
+ res=bind(server_sockfd, (struct sockaddr *)&server_address, server_len);
+ if (res != 0){
+ error_log("Error: "+toString(res)
+ +" in ui_server.cpp binding socket address"+bind_address
+ +":"+toString(ntohs(server_address.sin_port)));
+ }
+ //Create a connection queue and initialize readfds to handle input from server_sockfd:
+ listen(server_sockfd, 5);
+ FD_ZERO(&readfds);
+ FD_SET(server_sockfd, &readfds);
+}
+
+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());
+ }
+ }
+ }
+ return 0;
+}
+
+void *run_ui_server(void * ){
+ while(1) {
+ uint fd;
+ int nread;
+ ui_server.testfds = ui_server.readfds;
+ int result;
+ //Now wait for clients and requests. Because you have passed a null pointer as the timeout parameter, no timeout will occur. The program will logg an error if select returns a value less than 1:
+ result = select(FD_SETSIZE, &ui_server.testfds, (fd_set *)0,
+ (fd_set *)0, (struct timeval *) 0);
+ if(result < 1) {
+ error_log_no_msg("Error in ui_server on select");
+ break;
+ }
+ //Once you know you’ve got activity, you can find which descriptor it’s on by checking each in turn using FD_ISSET:
+ for(fd = 0; fd <= ui_server.max_fd_num; fd++) {
+ if(FD_ISSET(fd,&ui_server.testfds)) {
+ //If the activity is on server_sockfd, it must be a request for a new connection, and you add the associated client_sockfd to the descriptor set:
+ if(fd == ui_server.server_sockfd) {
+ uint client_sockfd;
+ socklen_t client_len;
+ struct sockaddr_in client_address;
+
+ client_len = sizeof(client_address);
+ client_sockfd = accept(ui_server.server_sockfd,
+ (struct sockaddr *)&client_address, &client_len);
+
+ 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);
+ //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);
+ if(nread == 0) {
+ close(fd);
+ FD_CLR(fd, &ui_server.readfds);
+ debug("Client parted from fd:"+toString(fd));
+ }else{
+ char buffer[1000];
+ read(fd, &buffer, nread);
+ }
+ }
+ }
+ }
+ }
+ return 0;
+}
diff --git a/segget/ui_server.h b/segget/ui_server.h
new file mode 100644
index 0000000..225dd7b
--- /dev/null
+++ b/segget/ui_server.h
@@ -0,0 +1,58 @@
+/*
+* 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
+*/
+
+#ifndef __UI_SERVER_H__
+#define __UI_SERVER_H__
+
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <stdio.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <sys/time.h>
+#include <sys/ioctl.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <string.h>
+
+using namespace std;
+
+#define MAX_BIND_ATTEMPTS 5
+
+class Tui_server{
+ public:
+ uint server_sockfd;
+ uint max_fd_num;
+ fd_set readfds, testfds;
+ void init();
+ ulong send(uint y, string msg);
+};
+
+Tui_server ui_server;
+
+void *run_ui_server(void * ptr);
+
+#endif \ No newline at end of file
diff --git a/tuiclient/Makefile b/tuiclient/Makefile
new file mode 100644
index 0000000..cfea393
--- /dev/null
+++ b/tuiclient/Makefile
@@ -0,0 +1,52 @@
+
+BINS = tuiclient
+OBJS = $(addsuffix .o,$(BINS))
+PKGCONFIG_MODULES =
+MISSING_PLUGINS_LIBS =
+LIBS_PKGCONFIG := $(foreach mod,$(PKGCONFIG_MODULES),$(shell pkg-config --libs $(mod)))
+CXXFLAGS_PKGCONFIG := $(foreach mod,$(PKGCONFIG_MODULES),$(shell pkg-config --libs $(mod)))
+
+CXXFLAGS_WARNINGS = -pedantic -Wall -Wextra -Wformat -Weffc++
+#CXXFLAGS_WARNINGS = -pedantic -Wextra -Wformat -Weffc++
+CXXFLAGS_OPT = -O2 -g
+
+LIBS = $(LIBS_PKGCONFIG)
+#CIBS = -lncurses
+CXXFLAGS = $(CXXFLAGS_PKGCONFIG) -lncurses $(CXXFLAGS_WARNINGS) $(CXXFLAGS_OPT)
+
+all: clean $(BINS)
+
+#log.o: log.cpp settings.h log.h
+#segget.o: segget.cpp pkg.cpp distfile.cpp segment.cpp log.o stats.cpp connection.cpp tui.cpp settings.o mirror.cpp str.cpp checksum.cpp
+
+#mirror.o: mirror.cpp tui.h
+# $(CXX) -c -o $@ mirror.cpp
+#
+#$(CXXFLAGS) $^
+#segget: segget.cpp log.o mirror.o distfile.o tui.o
+#pkg.o distfile.o segment.o mirror.o connection.o settings.o stats.o tui.o log.o
+#pkg.o: pkg.cpp pkg.h distfile.h segment.h log.h stats.h connection.h tui.h settings.h config.h str.h mirror.h checksum.h \
+# distfile.o segment.o log.o stats.o connection.o tui.o settings.o config.o str.o mirror.o checksum.o
+
+#distfile.o: distfile.cpp tui.o
+#distfile.o: distfile.cpp checksum.o
+#distfile.h segment.h log.h stats.h connection.h tui.h settings.h config.h str.h mirror.h checksum.h
+# $(CXX) -c -o $@ $(CXXFLAGS) $^
+
+#segment.o: segment.cpp segment.h log.h stats.h connection.h tui.h settings.h config.h str.h
+
+#segget.o: segget.cpp settings.o pkg.h distfile.h segment.h log.h stats.h connection.h tui.h settings.h config.h str.h mirror.h checksum.h
+#
+%.o: %.cxx
+ $(CXX) -c -o $@ $(CXXFLAGS) $^
+%: %.o
+ $(CXX) -o $@ $(LIBS) $(CIBS) $^
+
+make clean_log:
+ -rm -f ./logs/*.log
+
+clean:
+ -rm -f $(BINS) $(OBJS)
+ -rm -f ./logs/*.log *.o
+
+.PRECIOUS: %.o
diff --git a/tuiclient/str.cpp b/tuiclient/str.cpp
new file mode 100644
index 0000000..4c90034
--- /dev/null
+++ b/tuiclient/str.cpp
@@ -0,0 +1,47 @@
+/*
+* 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 "str.h"
+using namespace std;
+
+template<typename T> string toString(T t){
+ stringstream s;
+ s << t;
+ return s.str();
+}
+
+template<typename T> string field(string prefix,T t, int width){
+ try{
+ stringstream s1,s2;
+ s1 << t;
+ width=width+prefix.length();
+ s2.width(width);
+ s2 << prefix+s1.str();
+ return s2.str();
+ }catch(...){
+ return "";
+ }
+} \ No newline at end of file
diff --git a/tuiclient/str.h b/tuiclient/str.h
new file mode 100644
index 0000000..9db3202
--- /dev/null
+++ b/tuiclient/str.h
@@ -0,0 +1,35 @@
+ /*
+* 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
+*/
+
+#ifndef __STR_H__
+#define __STR_H__
+#include <string>
+#include <sstream>
+#include <algorithm>
+using namespace std;
+
+template<typename T> string toString(T t);
+#endif \ No newline at end of file
diff --git a/tuiclient/tui.cpp b/tuiclient/tui.cpp
new file mode 100644
index 0000000..9df772c
--- /dev/null
+++ b/tuiclient/tui.cpp
@@ -0,0 +1,45 @@
+/*
+* 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 "tui.h"
+
+const uint CONNECTION_LINES=5;
+
+bool msg_idle=true;
+void msg(uint y, uint x, string msg_text){
+ if (msg_idle){
+ msg_idle=false;
+ try{
+ move(y,x);
+ string ready_msg_text=msg_text+" ";
+ printw(ready_msg_text.c_str());
+ refresh();
+ }catch(...){
+// error_log_no_msg("Error in tui.cpp: msg()");
+ }
+ msg_idle=true;
+ }
+} \ No newline at end of file
diff --git a/tuiclient/tui.h b/tuiclient/tui.h
new file mode 100644
index 0000000..66d091b
--- /dev/null
+++ b/tuiclient/tui.h
@@ -0,0 +1,33 @@
+/*
+* 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
+*/
+
+#ifndef __TUI_H__
+#define __TUI_H__
+
+using namespace std;
+
+void msg_total(string msg_text);
+#endif \ No newline at end of file
diff --git a/tuiclient/tuiclient.cpp b/tuiclient/tuiclient.cpp
new file mode 100644
index 0000000..619fc07
--- /dev/null
+++ b/tuiclient/tuiclient.cpp
@@ -0,0 +1,114 @@
+//Make the necessary includes and set up the variables:
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <stdio.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <sys/ioctl.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <string>
+#include <string.h>
+#include <ncurses.h>
+#include "tui.h"
+#include "tui.cpp"
+#include "str.cpp"
+
+#define BUFFER_SIZE 1000
+
+using namespace std;
+
+int main()
+{
+ try{
+ try{
+ //init curses
+ initscr();
+ curs_set(0);
+ refresh();
+ }catch(...)
+ {
+ //error while init curses
+ }
+
+ try{
+ while (true){
+ int sockfd;
+ int len;
+ struct sockaddr_in address;
+ string recv_msg, first_part, msg_text;
+
+ //Create a socket for the client:
+ int result=-1;
+ ulong attempt_num=1;
+ while (result==-1){
+ 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("127.0.0.1");
+ address.sin_port = htons(9999);
+ len = sizeof(address);
+ //Connect your socket to the server’s socket:
+ result = connect(sockfd, (struct sockaddr *)&address, len);
+ if(result == -1) {
+ msg(0,0,"Error connectin to segget daemon. Attempt:"+toString(attempt_num)+" Result:"+toString(result)+" Waiting for 1 sec, before reconnect");
+ close(sockfd);
+ attempt_num++;
+ sleep(1);
+ }
+ }
+
+ msg(35,70,"Connected");
+ fd_set readfds, testfds;
+
+ FD_ZERO(&readfds);
+ FD_SET(sockfd, &readfds);
+ testfds = readfds;
+ bool run_flag=true;
+ while (run_flag){
+ result = select(FD_SETSIZE, &testfds, (fd_set *)0,
+ (fd_set *)0, (struct timeval *) 0);
+
+ 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];
+ read(sockfd, recv_buffer, BUFFER_SIZE);
+ recv_msg=recv_msg+recv_buffer;
+ while (recv_msg.find("<.>")!=recv_msg.npos){
+ 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 x=0;
+ uint y=atoi(first_part.substr(0,first_part.find("<s>")).c_str());
+ msg_text=first_part.substr(first_part.find("<s>")+3,first_part.npos);
+ msg(y, x, msg_text);
+ }
+ };
+ }
+ close(sockfd);
+ }
+ }catch(...){
+// error_log_no_msg("Error in segget.cpp launch_tui_theread() failed");
+ }
+ getch();
+ }
+ catch(...){
+ //error during init and downloading process
+ }
+ try{
+ endwin();
+ }
+ catch(...)
+ {
+ //error while ending curses
+ }
+ return 0;
+}