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 /tuiclient
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.
Diffstat (limited to 'tuiclient')
-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
6 files changed, 326 insertions, 0 deletions
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;
+}