/* * Copyright (C) 2010 Robin H.Johnson, Ovechko Kostyantyn . * * 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" Tui_server ui_server; enum TDFsearch_rusults{ NOT_FOUND, DOWNLOADED, IN_QUEUE }; 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=settings.ui_ip; server_address.sin_addr.s_addr = inet_addr(bind_address.c_str()); server_address.sin_port = htons(settings.ui_port); 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); send_to_fd_busy=false; } //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){ sleep(1); } send_to_fd_busy=true; if (fd !=server_sockfd){ if(FD_ISSET(fd,&ui_server.readfds)) { ulong bytes_written=write(fd, msg.c_str(), msg.length()); if (bytes_written!=msg.length()){ debug("Error: Not all data has been sent to ui_client()"); } } } send_to_fd_busy=false; return 0; } void Tui_server::send_connection_msg_to_fd(uint fd, uint y, string msg){ string message="c"+toString(y)+""+msg+"<.>"; send_to_fd(fd, message); } void Tui_server::send_connection_msg_to_all_clients(uint y, string msg){ string message="c"+toString(y)+""+msg+"<.>"; for(uint fd = 0; fd <= ui_server.max_fd_num; fd++){ 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="l"+msg+"<.>"; for(uint fd = 0; fd <= ui_server.max_fd_num; fd++){ if FD_ISSET(fd, &tuiclient_fds_set){ send_to_fd(fd, message); } } } void Tui_server::send_distfile_progress_msg_to_fd(uint fd, string msg){ string message="d"+msg+"<.>"; send_to_fd(fd, message); } void Tui_server::send_distfile_progress_msg_to_all_clients(string msg){ string message="d"+msg+"<.>"; for(uint fd = 0; fd <= ui_server.max_fd_num; fd++){ 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="e"+msg+"<.>"; for(uint fd = 0; fd <= ui_server.max_fd_num; fd++){ 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("",msg,request_str_before,request_str_after); split("<.>",request_str_after,request_str_before,request_str_after); vector wait_distfiles_vector; wait_distfiles_vector=split_to_vector(",", request_str_before); TDFsearch_rusults ALL_distfiles_search_result; if (wait_distfiles_vector.size()<1){ // if no distfile specified just wait for the ones in queue ALL_distfiles_search_result=IN_QUEUE; }else{ // Let's start from DONWLOADED, and if we find one IN_QUEUE or even NOT_FOUND we'll change it ALL_distfiles_search_result=DOWNLOADED; } for (ulong cur_tuiwaited_distfile=0;cur_tuiwaited_distfile0){ for (ulong distfile_num=0; distfile_numname); if (wait_distfiles_vector[cur_tuiwaited_distfile]==request_server_pkg.Pdistfile_list[distfile_num]->name){ if (request_server_pkg.Pdistfile_list[distfile_num]->get_status()==DDOWNLOADED){ distfile_search_result=DOWNLOADED; debug("....downloaded:"+wait_distfiles_vector[cur_tuiwaited_distfile]); }else{ debug("....in queue:"+wait_distfiles_vector[cur_tuiwaited_distfile]); distfile_search_result=IN_QUEUE; ALL_distfiles_search_result=IN_QUEUE; } break; } } if (distfile_search_result==NOT_FOUND){ for (ulong distfile_num=0; distfile_numname){ if (proxy_fetcher_pkg.Pdistfile_list[distfile_num]->get_status()==DDOWNLOADED){ debug("2....downloaded:"+wait_distfiles_vector[cur_tuiwaited_distfile]); distfile_search_result=DOWNLOADED; }else{ debug("2....in queue:"+wait_distfiles_vector[cur_tuiwaited_distfile]); distfile_search_result=IN_QUEUE; ALL_distfiles_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; } if (distfile_search_result==NOT_FOUND){ ALL_distfiles_search_result=NOT_FOUND; break; } } switch (ALL_distfiles_search_result){ case NOT_FOUND: send_to_fd(fd, "n<.>"); //distfile is not in the list quit error_log_no_msg("Distfile(s) not found"); break; case DOWNLOADED: send_to_fd(fd, "N<.>"); //distfile is not in the list quit error_log_no_msg("Distfile(s) downloaded"); break; case IN_QUEUE: error_log_no_msg("Distfile(s) in queue"); send_to_fd(fd, "y<.>"); //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++){ 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_numget_distfile_progress_str()); debug("Sending to client:"+request_server_pkg.Pdistfile_list[distfile_num]->get_distfile_progress_str()); } for (ulong distfile_num=0; distfile_numget_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 distfile_percent; if (a_distfile->size>0){ distfile_percent=a_distfile->dld_bytes*100/a_distfile->size; }else{ distfile_percent=50; } string result=(string)"" +((distfile_percent>0)?"" :"") +((100-distfile_percent>0)?"" :"") +"
" +toString(distfile_percent)+"%" +""+a_distfile->name +"statusToColor()+"\">"+a_distfile->statusToString() +""+toString(a_distfile->dld_segments_count) +""+toString(a_distfile->segments_count) +""+toString(a_distfile->dld_bytes) +""+toString(a_distfile->size) +""; return result; }catch(...){ error_log("Error: ui_server.cpp: serve_browser_distfile_progress()"); return ""; } } string Tui_server::get_html_header(string title){ string header; header=(string)"HTTP/1.0 200 OK\n\n" +"" // +"" +" Segget - "+title+"" +"" +"\n" +"" +"" +"" +"" +"" +"" +"" +"" +"\n" +"" +"" +"" +"" +"" +"" +"" +"" +"\n" +"
\"Segments\"\"Distfiles\"\"Stats\"\"mirrors\"\"Log\"\"Errors\"Log\"
ConnectionsDistfilesStatsMirrorsLogErrors logRSS
\n" +"

"+title+"

\n"; return header; } string Tui_server::get_html_footer(){ return ""; } string Tui_server::get_html_stats(){ ulong total_progress; if (stats.total_size>0){ total_progress=(stats.total_size-stats.dld_size)*100/stats.total_size; }else total_progress=0; string stats_str= (string)"\n" +"\n" +" \n" +" \n" +"\n" +"\n" +" \n" +"\n" +"\n" +"\n" +"\n" +"\n" +"\n" +"\n" +"\n" +"\n" +"\n" +"\n" +"\n" +"\n" +"\n" +"\n" // +"" // +"" +"
Up time"+secsToString(time_left_since(stats.segget_start_time)/1000)+"
Distfiles
Downloaded distfiles"+toString(stats.dld_distfiles_count)+"
Bytes downloaded"+toString(stats.dld_size)+"
Total progress"+toString(total_progress)+"%
Distfiles left to download"+toString(stats.distfiles_count-stats.dld_distfiles_count)+"
Bytes left to download"+toString(stats.total_size-stats.dld_size)+"
Distfiles total"+toString(stats.distfiles_count)+"
Bytes total"+toString(stats.total_size)+"
Segments
Downloaded segments"+toString(stats.dld_segments_count)+"
Segmentss left to download"+toString(stats.segments_count-stats.dld_segments_count)+"
Segments total"+toString(stats.segments_count)+"
Connections
Failed connections"+toString(stats.fails_counter)+"
AVG speed of active connections"+speedToString(stats.avg_total_speed)+"
AVG speed since segget start"+speedToString(stats.dld_size*1000/time_left_since(stats.segget_start_time))+"
AVG speed since segget start except idle times"+speedToString(stats.avg_total_speed)+"
\n"; return stats_str; } string Tui_server::get_html_mirrors_stats(){ string stats_str=(string)"\n" +"\n\n" +"\n" +"\n" +"\n" +"\n" +"\n" +"\n" +"\n" +"\n" +"\n"; map::iterator iter; for( iter = mirror_list.begin(); iter != mirror_list.end(); iter++ ) { ulong avg_speed; if ((iter->second->dld_size!=1) and (iter->second->dld_time>0)){ avg_speed=(iter->second->dld_size)/iter->second->dld_time; }else avg_speed=0; ulong fail_ratio; if (iter->second->failed_downloads+iter->second->successful_downloads>0){ fail_ratio=(iter->second->failed_downloads)*100/(iter->second->failed_downloads+iter->second->successful_downloads); }else fail_ratio=0; stats_str=stats_str +"\n\n" +"\n" +"\n" +"\n" +"\n" +"\n" +"\n" +"\n" +"\n" +"\n"; } stats_str=stats_str+"
URLActive connectionsAVG speed per connection**Failed downloadsSuccessful downloadsFail ratioUsage time**, secSuccesfuly downloaded bytesHonesty
\n"+iter->first+""+toString(iter->second->active_num)+""+speedToString(avg_speed)+""+toString(iter->second->failed_downloads)+""+toString(iter->second->successful_downloads)+""+toString(fail_ratio)+"%"+toString(iter->second->dld_time)+""+toString(iter->second->dld_size)+""+toString(iter->second->honesty)+"
\n"; stats_str=stats_str+"

** NOTE: When a mirror has N simultaneous connections " +"\"Usage time\" will be increasing N times faster, therefore \"AVG speed per connection\" will " +"be smaller than average outgoing rate provided by this mirror to your host " +"(which is a summary speed of the simultaneous connections to this mirror)

"; return stats_str; } string Tui_server::get_html_connections(){ try{ string result=(string)"
\n" +"

Active connections: "+toString(stats.active_connections_counter)+"/"+toString(settings.max_connections)+"

\n" +"\n" +"\n" +"\n" +"\n" +"\n" +"\n" +"\n" +"\n" +"\n" +"\n" +"\n" +"\n" +"\n" +"\n" +"\n" +"\n" +"\n" +"\n" +"\n" +"\n"; 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+""+connection_array[connection_num].get_html_connection_progress()+"\n"; } } result=result+"
Distfile progressDistfile nameSegment progressSegment #TryNetworkBytesSpeedETA
NumTypeDownloadedTotalCurrentAverage
\n
\n"; return result; }catch(...){ error_log("Error: ui_server.cpp: get_connections_info()"); return ""; } } string Tui_server::get_html_distfiles(){ try{ debug("Sending to client distfiles_num:"+toString(request_server_pkg.Pdistfile_list.size())); string distfiles_html=(string)"
\n" +"" +" " +"\n" +" \n"; for (ulong distfile_num=0; distfile_num"+serve_browser_distfile_progress(request_server_pkg.Pdistfile_list[distfile_num])+"\n"; debug("Sending to client:"+request_server_pkg.Pdistfile_list[distfile_num]->get_distfile_progress_str()); } for (ulong distfile_num=0; distfile_num"+serve_browser_distfile_progress(proxy_fetcher_pkg.Pdistfile_list[distfile_num])+"\n"; debug("Sending to client:"+proxy_fetcher_pkg.Pdistfile_list[distfile_num]->get_distfile_progress_str()); } distfiles_html=distfiles_html+"
Progress" +" Name" +" Status" +" Segments" +" Bytes" +"
Downloaded" +" Total" +" Downloaded" +" Total" +"
"; return distfiles_html; }catch(...){ error_log("Error: ui_server.cpp: get_connections_info()"); return ""; } } string Tui_server::get_html_log(){ try{ string log_html=(string)"
"; for (uint log_line_num=0; log_line_num"; } log_html=log_html+"
"+log_lines[log_line_num]+"
"; return log_html; }catch(...){ error_log("Error: ui_server.cpp: get_html_log()"); return ""; } } string Tui_server::get_html_errors_log(){ try{ debug("Sending to client distfiles_num:"+toString(request_server_pkg.Pdistfile_list.size())); string error_log_html=(string)"
"; for (uint error_log_line_num=0; error_log_line_num"; } error_log_html=error_log_html+"
"+error_log_lines[error_log_line_num]+"
"; return error_log_html; }catch(...){ error_log("Error: ui_server.cpp: get_html_error_log()"); return ""; } } string Tui_server::get_rss_info(){ try{ string rss_result= (string)" " "" +""+settings.rss_title+"" +""+settings.rss_description+"" +""+settings.provide_mirror_to_others_url+"" +""+get_time(settings.general_log_time_format)+"" +"5" +"" +""+"settings.rss_title"+"" +"/img/segget_feed.jpg" +"http://simsim/forum/index.php" +""; for (uint rss_line_num=0; rss_line_num" +""+rss_distfile_lines[rss_line_num]+"" +" " +""+"settings.rss_starter"+"" +"seggetd" +"" +""+rss_time_lines[rss_line_num]+"" +""+toString(rss_line_num)+"" +""; } rss_result=rss_result+""; return rss_result; }catch(...){ error_log("Error: ui_server.cpp: get_rss_info()"); return ""; } } string Tui_server::get_ajax_for(string content_name){ string ajax_str=(string)"\n\n"; return ajax_str; } 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_html_header("Connections")); send_to_fd(fd,"\n"); send_to_fd(fd,get_html_connections()); send_to_fd(fd,"\n"); send_to_fd(fd,get_ajax_for("connections")); send_to_fd(fd,get_html_footer()); }else if (uri=="/connections_"){ send_to_fd(fd,get_html_connections()); }else if ((uri=="/rss") or (uri=="/rss.rss") or (uri=="/rss.xml")){ send_to_fd(fd,get_rss_info()); }else if (uri=="/stats"){ send_to_fd(fd,get_html_header("Stats")); send_to_fd(fd,"\n"); send_to_fd(fd,get_html_stats()); send_to_fd(fd,"\n"); send_to_fd(fd,get_ajax_for("stats")); send_to_fd(fd,get_html_footer()); }else if (uri=="/stats_"){ ui_server.send_to_fd(fd,get_html_stats()); }else if (uri=="/mirrors_stats"){ send_to_fd(fd,get_html_header("Mirrors stats")); send_to_fd(fd,"\n"); send_to_fd(fd,get_html_mirrors_stats()); send_to_fd(fd,"\n"); send_to_fd(fd,get_ajax_for("mirrors_stats")); send_to_fd(fd,get_html_footer()); }else if (uri=="/mirrors_stats_"){ send_to_fd(fd,get_html_mirrors_stats()); }else if (uri=="/log"){ send_to_fd(fd,get_html_header("Log")); send_to_fd(fd,"\n"); send_to_fd(fd,get_html_log()); send_to_fd(fd,"\n"); send_to_fd(fd,get_ajax_for("log")); send_to_fd(fd,get_html_footer()); }else if (uri=="/log_"){ send_to_fd(fd,get_html_log()); }else if (uri=="/errors_log"){ send_to_fd(fd,get_html_header("Errors log")); send_to_fd(fd,"\n"); send_to_fd(fd,get_html_errors_log()); send_to_fd(fd,"\n"); send_to_fd(fd,get_ajax_for("errors_log")); send_to_fd(fd,get_html_footer()); }else if (uri=="/errors_log_"){ send_to_fd(fd,get_html_errors_log()); }else if (uri=="/distfiles"){ send_to_fd(fd,get_html_header("Distfiles")); send_to_fd(fd,"\n"); send_to_fd(fd,get_html_distfiles()); send_to_fd(fd,"\n"); send_to_fd(fd,get_ajax_for("distfiles")); send_to_fd(fd,get_html_footer()); }else if (uri=="/distfiles_"){ ui_server.send_to_fd(fd,get_html_distfiles()); }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_html_header("Connections")); send_to_fd(fd,"\n"); send_to_fd(fd,get_html_connections()); send_to_fd(fd,"\n"); send_to_fd(fd,get_ajax_for("connections")); send_to_fd(fd,get_html_footer()); } shutdown(fd,2); close(fd); }catch(...){ error_log_no_msg("Error in ui_server.cpp: serve_server()"); } } void *run_ui_server(void * ){ while(1) { max_published_screenline_num=0; 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); // 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{ debug("else"); ioctl(fd, FIONREAD, &nread); 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{ debug("reading buffer"); char buffer[1000]=""; if (nread!=read(fd, &buffer, nread)){ debug("Not all data has been read from ui_client()"); } 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 (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); } } } } } } } return 0; }