/* * 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 "request.h" int request(string msg){ try{ int len; struct sockaddr_in address; int result; //Create a socket for the client: 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(settings.request_ip.c_str()); address.sin_port = htons(settings.request_port); len = sizeof(address); //Connect your socket to the server’s socket: result = connect(sockfd, (struct sockaddr *)&address, len); if(result == -1) { printout("Can't connect to segget daemon"); return R_PF_ERROR_ADDING_TO_PROXY_QUEUE; } if (msg.length()>90000){return R_PF_ERROR_ADDING_TO_PROXY_QUEUE;}; char send_buffer[100000]; strcpy(send_buffer,msg.c_str()); //You can now read and write via sockfd: if (write(sockfd, send_buffer, strlen(send_buffer))!=(int)msg.length()){ printout("Error in request.cpp: request(): request msg size and sent data size are different."); }; fd_set readfds, testfds; FD_ZERO(&readfds); FD_SET(sockfd, &readfds); testfds = readfds; struct timeval response_timeout; response_timeout.tv_sec=1; response_timeout.tv_usec=0; result = select(FD_SETSIZE, &testfds, (fd_set *)0, (fd_set *)0, &response_timeout); if(FD_ISSET(sockfd,&testfds)) { int nread; ioctl(sockfd, FIONREAD, &nread); char recv_buffer[1000]; if(nread == 0) { close(sockfd); printout("Error in request.cpp: request(): no response from segget daemon"); }else{ if (nread!=read(sockfd, recv_buffer, nread)){ printout("Error in request.cpp: request(): response msg size and received data size are different."); }; return decode_server_response(recv_buffer); } }else{ printout("Error in request.cpp: request(): zero size response from segget daemon"); } close(sockfd); return R_PF_ERROR_ADDING_TO_PROXY_QUEUE; }catch(...){ printout("Error: request.cpp: request()"); return R_PF_ERROR_ADDING_TO_PROXY_QUEUE; } } int load_pkgs(){ try{ ifstream json_pkg_list_file; json_pkg_list_file.exceptions (ofstream::failbit | ofstream::badbit); try{ json_pkg_list_file.open(settings.pkg_list_file_name.c_str()); }catch(...){ printout("Error: Can't open distfile list file: "+settings.pkg_list_file_name); return 1; } string buff((std::istreambuf_iterator(json_pkg_list_file)), std::istreambuf_iterator()); try{ json_pkg_list_file>>buff; }catch(std::ifstream::failure e){ if (!json_pkg_list_file.eof()){ printout("Error: Can't read pkg.list file: "+(string)e.what()); return 1; } }catch(...){ printout("Unknown Error: Can't read pkg.list file"); return 1; } try{ json_pkg_list_file.close(); }catch(...){ printout("Error: Can't close pkg.list file"); } try{ json_object *json_array_pkg_list; json_array_pkg_list = json_tokener_parse(buff.c_str()); if (is_error(json_array_pkg_list)) printout("error parsing json:"+toString(json_tokener_errors[-(unsigned long)json_array_pkg_list])); else { uint pkg_count=json_object_array_length(json_array_pkg_list); // create 0 pkg for distfiles to provide proxy-fetcher for(uint array_item_num=0;array_item_num