summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'segget/ui_server.cpp')
-rw-r--r--segget/ui_server.cpp366
1 files changed, 305 insertions, 61 deletions
diff --git a/segget/ui_server.cpp b/segget/ui_server.cpp
index 09cb26d..dbc7af9 100644
--- a/segget/ui_server.cpp
+++ b/segget/ui_server.cpp
@@ -70,6 +70,53 @@ void Tui_server::init(){
//prevent simultaneous writes
+ulong Tui_server::send_binary_to_fd(uint fd, string image_file_name){
+// if (send_to_fd_idle) {
+ ifstream image_file;
+ try{
+ image_file.open (image_file_name.c_str(), ios::in|ios::binary|ios::ate);
+ // file.open((settings.conf_dir+"/"+config_file_name).c_str());
+ }
+ catch(...){
+ error_log("Error opening image file: ");
+ // return;
+ }
+ try{
+ //processing file
+ ifstream::pos_type size;
+ char * memblock;
+ if (image_file.is_open()){
+ size = image_file.tellg();
+ memblock = new char [size];
+ image_file.seekg (0, ios::beg);
+ image_file.read (memblock, size);
+
+ while (send_to_fd_busy){
+ sleep(1);
+ }
+ send_to_fd_busy=true;
+ if (fd !=server_sockfd){
+ if(FD_ISSET(fd,&ui_server.readfds)) {
+ ulong bytes_written=write(fd, memblock, size);
+ if (bytes_written!=size){
+ debug("Error: Not all data has been sent to ui_client during send_binary_to_fd()");
+ }
+ }
+ }
+ send_to_fd_busy=false;
+ image_file.close();
+ delete[] memblock;
+ }
+ }catch(ifstream::failure e){
+//
+ }catch(...){
+ // error_log("Settings file: "+config_file_name+" was opened, but an error occured while reading settings from it.");
+ }
+
+ return 0;
+}
+
+
ulong Tui_server::send_to_fd(uint fd, string msg){
// if (send_to_fd_idle) {
while (send_to_fd_busy){
@@ -96,14 +143,18 @@ void Tui_server::send_connection_msg_to_fd(uint fd, uint y, string msg){
void Tui_server::send_connection_msg_to_all_clients(uint y, string msg){
string message="<m>c<t>"+toString(y)+"<y>"+msg+"<.>";
for(uint fd = 0; fd <= ui_server.max_fd_num; fd++){
- send_to_fd(fd, message);
+ if FD_ISSET(fd, &tuiclient_fds_set){
+ send_to_fd(fd, message);
+ }
}
}
void Tui_server::send_log_msg_to_all_clients(string msg){
string message="<m>l<t>"+msg+"<.>";
for(uint fd = 0; fd <= ui_server.max_fd_num; fd++){
- send_to_fd(fd, message);
+ if FD_ISSET(fd, &tuiclient_fds_set){
+ send_to_fd(fd, message);
+ }
}
}
@@ -115,14 +166,247 @@ void Tui_server::send_distfile_progress_msg_to_fd(uint fd, string msg){
void Tui_server::send_distfile_progress_msg_to_all_clients(string msg){
string message="<m>d<t>"+msg+"<.>";
for(uint fd = 0; fd <= ui_server.max_fd_num; fd++){
- send_to_fd(fd, message);
+ if FD_ISSET(fd, &tuiclient_fds_set){
+ send_to_fd(fd, message);
+ }
}
}
void Tui_server::send_error_log_msg_to_all_clients(string msg){
string message="<m>e<t>"+msg+"<.>";
for(uint fd = 0; fd <= ui_server.max_fd_num; fd++){
- send_to_fd(fd, message);
+ if FD_ISSET(fd, &tuiclient_fds_set){
+ send_to_fd(fd, message);
+ }
+ }
+}
+
+void Tui_server::serve_tuiclient(uint fd, string msg){
+ try{
+ debug("tuiclient connected");
+ string request_str_before,request_str_after;
+ split("<d>",msg,request_str_before,request_str_after);
+ split("<.>",request_str_after,request_str_before,request_str_after);
+ string distfile_by_name_lookup_request=request_str_before;
+ TDFsearch_rusults distfile_search_result=NOT_FOUND;
+ if (distfile_by_name_lookup_request.length()>0){
+ for (ulong distfile_num=0; distfile_num<request_server_pkg.distfile_count; distfile_num++){
+ if (distfile_by_name_lookup_request==request_server_pkg.Pdistfile_list[distfile_num]->name){
+ if (request_server_pkg.Pdistfile_list[distfile_num]->get_status()==DDOWNLOADED){
+ distfile_search_result=DOWNLOADED;
+ }else{
+ distfile_search_result=IN_QUEUE;
+ }
+ break;
+ }
+ }
+ if (distfile_search_result==NOT_FOUND){
+ for (ulong distfile_num=0; distfile_num<proxy_fetcher_pkg.distfile_count; distfile_num++){
+ if (distfile_by_name_lookup_request==proxy_fetcher_pkg.Pdistfile_list[distfile_num]->name){
+ if (proxy_fetcher_pkg.Pdistfile_list[distfile_num]->get_status()==DDOWNLOADED){
+ distfile_search_result=DOWNLOADED;
+ }else{
+ distfile_search_result=IN_QUEUE;
+ }
+ break;
+ }
+ }
+ }
+ }else{
+ // if no name for distfile specified -> no need to find distfile
+ // just keep an eye on the ones in queue
+ distfile_search_result=IN_QUEUE;
+ }
+ switch (distfile_search_result){
+ case NOT_FOUND:
+ ui_server.send_to_fd(fd, "<m>n<t><.>"); //distfile is not in the list quit
+ break;
+ case DOWNLOADED:
+ ui_server.send_to_fd(fd, "<m>N<t><.>"); //distfile is not in the list quit
+ break;
+ case IN_QUEUE:
+ ui_server.send_to_fd(fd, "<m>y<t><.>"); //distfile is in the list continue
+ // Get this info to catch up!
+ for (uint line_num=0; line_num<=max_published_screenline_num;line_num++){
+ ui_server.send_connection_msg_to_fd(fd, line_num, screenlines[line_num]);
+ debug("Sending to client line:"+toString(line_num)+" "+screenlines[line_num]);
+ }
+ debug("Sending to client distfiles_num:"+toString(request_server_pkg.Pdistfile_list.size()));
+ for (ulong distfile_num=0; distfile_num<request_server_pkg.Pdistfile_list.size(); distfile_num++){
+ ui_server.send_distfile_progress_msg_to_fd(fd, request_server_pkg.Pdistfile_list[distfile_num]->get_distfile_progress_str());
+ debug("Sending to client:"+request_server_pkg.Pdistfile_list[distfile_num]->get_distfile_progress_str());
+ }
+ for (ulong distfile_num=0; distfile_num<proxy_fetcher_pkg.Pdistfile_list.size(); distfile_num++){
+ ui_server.send_distfile_progress_msg_to_fd(fd, proxy_fetcher_pkg.Pdistfile_list[distfile_num]->get_distfile_progress_str());
+ debug("Sending to client:"+proxy_fetcher_pkg.Pdistfile_list[distfile_num]->get_distfile_progress_str());
+ }
+ }
+ }catch(...){
+ error_log_no_msg("Error in ui_server.cpp: serve_server()");
+ }
+}
+
+string Tui_server::serve_browser_distfile_progress(Tdistfile * a_distfile){
+ try{
+ uint percent;
+ if (a_distfile->size>0){
+ percent=a_distfile->dld_bytes*100/a_distfile->size;
+ }else{
+ percent=50;
+ }
+ string result=(string)"<td>"
+ +"<img src=\"/img/blue_progress.jpg\" alt=\"blue_progress\" height=20 width="+toString(percent)+"/>"
+ +"<img src=\"/img/bw_progress.jpg\" alt=\"bw_progress\" height=20 width="+toString(100-percent)+" />"
+ +"</td><td align=center>"+toString(percent)+"%"
+ +"</td><td>"+a_distfile->name
+ +"</td><td align=center bgcolor=\""+a_distfile->statusToColor()+"\">"+a_distfile->statusToString()
+ +"</td><td align=right>"+toString(a_distfile->dld_segments_count)
+ +"</td><td align=right>"+toString(a_distfile->segments_count)
+ +"</td><td align=right>"+toString(a_distfile->dld_bytes)
+ +"</td><td align=right>"+toString(a_distfile->size)
+ +"</td>";
+ return result;
+ }catch(...){
+ error_log("Error: ui_server.cpp: serve_browser_distfile_progress()");
+ return "";
+ }
+}
+
+string Tui_server::get_header(string title){
+ string header;
+ header=(string)"HTTP/1.0 200 OK\n\n"
+ +"<html><head><meta http-equiv=\"refresh\" content=\"1\" ><title> Segget - "+title+"</title></head>"
+ +"<body>"
+ +"<table border=0>"
+ +"<tr align=center>"
+ +"<td><a href=\"connections\"><img src=\"/img/connections.jpg\" alt=\"Segments\" height=50 width=50/></a></td>"
+ +"<td><a href=\"distfiles\"><img src=\"/img/distfiles.png\" alt=\"Distfiles\" height=50 width=50/></a></td>"
+ +"<td><a href=\"log\"><img src=\"/img/log.png\" alt=\"Log\" height=50 width=50/></a></td>"
+ +"<td><a href=\"errors_log\"><img src=\"/img/errors_log.jpg\" alt=\"Errors log\" height=50 width=50/></a></td>"
+ +"</tr>"
+ +"<tr align=center>"
+ +"<td><a href=\"connections\">Connections</a></td>"
+ +"<td><a href=\"distfiles\">Distfiles</a></td>"
+ +"<td><a href=\"log\">Log</a></td>"
+ +"<td><a href=\"errors_log\">Errors log</a></td>"
+ +"</tr>"
+ +"</table>"
+ +"<h1>"+title+"</h1>";
+ return header;
+}
+
+string Tui_server::get_footer(){
+ return "</body></html>";
+}
+
+string Tui_server::get_connections_info(){
+ try{
+ string result=(string)"<center>"
+ +"<h3>Active connections: "+toString(stats.active_connections_counter)+"/"+toString(settings.max_connections)+"</h1>"
+ +"<table border=1 width=100%>"
+ +"<tr>"
+ +"<th rowspan=2>Distfile progress</th>"
+ +"<th rowspan=2>Distfile name</th>"
+ +"<th rowspan=2>Segment progress</th>"
+ +"<th rowspan=2>Segment #</th>"
+ +"<th rowspan=2>Try</th>"
+ +"<th colspan=2>Network</th>"
+ +"<th colspan=2>Bytes</th>"
+ +"<th colspan=2>Speed</th>"
+ +"<th rowspan=2 width=100>ETA</th>"
+ +"</tr>"
+ +"<tr>"
+ +"<th>Num</th>"
+ +"<th>Type</th>"
+ +"<th>Downloaded</th>"
+ +"<th>Total</th>"
+ +"<th>Current</th>"
+ +"<th>Average</th>"
+ +"</tr>";
+ for (uint connection_num = 0; connection_num < settings.max_connections; ++connection_num) {
+// debug("connection_num:"+toString(connection_num));
+ if (connection_array[connection_num].active){
+ result=result+"<tr>"+connection_array[connection_num].get_html_connection_progress()+"</tr>";
+ }
+ }
+ result=result+"</table></center>";
+ return result;
+ }catch(...){
+ error_log("Error: ui_server.cpp: serve_browser_distfile_progress()");
+ return "";
+ }
+}
+void Tui_server::serve_browser(uint fd, string msg){
+ try{
+ debug("Web browser connected");
+ uint pos_uri_start=msg.find("GET");
+ uint pos_uri_end=msg.find("HTTP",pos_uri_start);
+ string uri;
+ if (pos_uri_end!=msg.npos){
+ uri=msg.substr(pos_uri_start+4,pos_uri_end-pos_uri_start-5);
+ debug("Web browser requests URI:"+uri);
+ }
+ if (uri=="/connections"){
+ send_to_fd(fd,get_header("Connections"));
+ send_to_fd(fd,get_connections_info());
+ send_to_fd(fd,get_footer());
+ }else if (uri=="/log"){
+ ui_server.send_to_fd(fd,get_header("Log"));
+ ui_server.send_to_fd(fd,"<center><table border=\"1\" width=\"100%\">");
+ for (uint log_line_num=0; log_line_num<log_lines.size(); log_line_num++){
+ ui_server.send_to_fd(fd,"<tr><td>"+log_lines[log_line_num]+"</td></tr>");
+ }
+ ui_server.send_to_fd(fd,"</table></center>");
+ ui_server.send_to_fd(fd,get_footer());
+ }else if (uri=="/errors_log"){
+ ui_server.send_to_fd(fd,get_header("Errors log"));
+ ui_server.send_to_fd(fd,"<center><table border=\"1\" width=\"100%\">");
+ for (uint error_log_line_num=0; error_log_line_num<error_log_lines.size(); error_log_line_num++){
+ ui_server.send_to_fd(fd,"<tr><td>"+error_log_lines[error_log_line_num]+"</td></tr>");
+ }
+ ui_server.send_to_fd(fd,"</table></center>");
+ ui_server.send_to_fd(fd,get_footer());
+ }else if (uri=="/distfiles"){
+ ui_server.send_to_fd(fd,get_header("Distfiles"));
+ ui_server.send_to_fd(fd,"<center><table border=\"1\" width=\"100%\">");
+ debug("Sending to client distfiles_num:"+toString(request_server_pkg.Pdistfile_list.size()));
+ ui_server.send_to_fd(fd,(string)"<tr>"
+ +"</th><th rowspan=2>Progress"
+ +"</th><th rowspan=2>Percent"
+ +"</th><th rowspan=2>Name"
+ +"</th><th rowspan=2>Status"
+ +"</th><th colspan=2>Segments"
+ +"</th><th colspan=2>Bytes"
+ +"</th></tr>");
+ ui_server.send_to_fd(fd,(string)"<tr>"
+ +"</th><th>Downloaded"
+ +"</th><th>Total"
+ +"</th><th>Downloaded"
+ +"</th><th>Total"
+ +"</th></tr>");
+ for (ulong distfile_num=0; distfile_num<request_server_pkg.Pdistfile_list.size(); distfile_num++){
+ ui_server.send_to_fd(fd,"<tr>"+serve_browser_distfile_progress(request_server_pkg.Pdistfile_list[distfile_num])+"</tr>");
+ debug("Sending to client:"+request_server_pkg.Pdistfile_list[distfile_num]->get_distfile_progress_str());
+ }
+ for (ulong distfile_num=0; distfile_num<proxy_fetcher_pkg.Pdistfile_list.size(); distfile_num++){
+ ui_server.send_to_fd(fd,"<tr>"+serve_browser_distfile_progress(proxy_fetcher_pkg.Pdistfile_list[distfile_num])+"</tr>");
+ debug("Sending to client:"+proxy_fetcher_pkg.Pdistfile_list[distfile_num]->get_distfile_progress_str());
+ }
+ ui_server.send_to_fd(fd,"</table></center>");
+ ui_server.send_to_fd(fd,get_footer());
+ }else if (uri=="/favicon.ico"){
+ ui_server.send_binary_to_fd(fd,"./webui/img/favicon.ico");
+ }else if (uri.find("/img")!=uri.npos){
+ ui_server.send_binary_to_fd(fd,"./webui"+uri);
+ }else{
+ send_to_fd(fd,get_header("Connections"));
+ send_to_fd(fd,get_connections_info());
+ send_to_fd(fd,get_footer());
+ }
+ shutdown(fd,2);
+ close(fd);
+ }catch(...){
+ error_log_no_msg("Error in ui_server.cpp: serve_server()");
}
}
@@ -158,6 +442,7 @@ void *run_ui_server(void * ){
if (client_sockfd>ui_server.max_fd_num) ui_server.max_fd_num=client_sockfd;
FD_SET(client_sockfd, &ui_server.readfds);
+// close(client_sockfd);
//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{
@@ -166,6 +451,9 @@ void *run_ui_server(void * ){
if(nread == 0) {
debug("nothing to read");
FD_CLR(fd, &ui_server.readfds);
+ if FD_ISSET(fd, &ui_server.tuiclient_fds_set){
+ FD_CLR(fd, &ui_server.tuiclient_fds_set);
+ }
close(fd);
debug("Client parted from fd:"+toString(fd));
}else{
@@ -174,64 +462,20 @@ void *run_ui_server(void * ){
if (nread!=read(fd, &buffer, nread)){
debug("Not all data has been read from ui_client()");
}
- string request_str_before,request_str_after;
- debug("received_from tuiclient:");
- debug(buffer);
- split("<d>",buffer,request_str_before,request_str_after);
- split("<.>",request_str_after,request_str_before,request_str_after);
- string distfile_by_name_lookup_request=request_str_before;
- TDFsearch_rusults distfile_search_result=NOT_FOUND;
- if (distfile_by_name_lookup_request.length()>0){
- for (ulong distfile_num=0; distfile_num<request_server_pkg.distfile_count; distfile_num++){
- if (distfile_by_name_lookup_request==request_server_pkg.Pdistfile_list[distfile_num]->name){
- if (request_server_pkg.Pdistfile_list[distfile_num]->get_status()==DDOWNLOADED){
- distfile_search_result=DOWNLOADED;
- }else{
- distfile_search_result=IN_QUEUE;
- }
- break;
- }
- }
- if (distfile_search_result==NOT_FOUND){
- for (ulong distfile_num=0; distfile_num<proxy_fetcher_pkg.distfile_count; distfile_num++){
- if (distfile_by_name_lookup_request==proxy_fetcher_pkg.Pdistfile_list[distfile_num]->name){
- if (proxy_fetcher_pkg.Pdistfile_list[distfile_num]->get_status()==DDOWNLOADED){
- distfile_search_result=DOWNLOADED;
- }else{
- distfile_search_result=IN_QUEUE;
- }
- break;
- }
- }
- }
+ string buffer_as_str=buffer;
+ debug("received_from ui_client:"+buffer_as_str);
+ if FD_ISSET(fd, &ui_server.tuiclient_fds_set){
+ ui_server.serve_tuiclient(fd,buffer_as_str);
}else{
- // if no name for distfile specified -> no need to find distfile
- // just keep an eye on the ones in queue
- distfile_search_result=IN_QUEUE;
- }
- switch (distfile_search_result){
- case NOT_FOUND:
- ui_server.send_to_fd(fd, "<m>n<t><.>"); //distfile is not in the list quit
- break;
- case DOWNLOADED:
- ui_server.send_to_fd(fd, "<m>N<t><.>"); //distfile is not in the list quit
- break;
- case IN_QUEUE:
- ui_server.send_to_fd(fd, "<m>y<t><.>"); //distfile is in the list continue
- // Get this info to catch up!
- for (uint line_num=0; line_num<=max_published_screenline_num;line_num++){
- ui_server.send_connection_msg_to_fd(fd, line_num, screenlines[line_num]);
- debug("Sending to client line:"+toString(line_num)+" "+screenlines[line_num]);
- }
- debug("Sending to client distfiles_num:"+toString(request_server_pkg.Pdistfile_list.size()));
- for (ulong distfile_num=0; distfile_num<request_server_pkg.Pdistfile_list.size(); distfile_num++){
- ui_server.send_distfile_progress_msg_to_fd(fd, request_server_pkg.Pdistfile_list[distfile_num]->get_distfile_progress_str());
- debug("Sending to client:"+request_server_pkg.Pdistfile_list[distfile_num]->get_distfile_progress_str());
- }
- for (ulong distfile_num=0; distfile_num<proxy_fetcher_pkg.Pdistfile_list.size(); distfile_num++){
- ui_server.send_distfile_progress_msg_to_fd(fd, proxy_fetcher_pkg.Pdistfile_list[distfile_num]->get_distfile_progress_str());
- debug("Sending to client:"+proxy_fetcher_pkg.Pdistfile_list[distfile_num]->get_distfile_progress_str());
- }
+ if (buffer_as_str.find("Host:")==buffer_as_str.npos){
+ // it's tuiclient else it's browser
+ FD_SET(fd, &ui_server.tuiclient_fds_set);
+ ui_server.serve_tuiclient(fd,buffer_as_str);
+ }else{ // it's browser
+ ui_server.serve_browser(fd,buffer_as_str);
+ FD_CLR(fd, &ui_server.readfds);
+ close(fd);
+ }
}
}
}