00001
00012 #include <cstdlib>
00013 #include "unix_buffer.H"
00014 #include <fstream>
00015 #include <string>
00016 #include "qsieve-fwd.H"
00017
00018 extern "C"
00019 {
00020 #include <getopt.h>
00021 }
00022
00023 using namespace std;
00024
00025 const char* const option_string = "+hgpvi:f:";
00026 static const struct option long_options[] =
00027 {
00028 { "help", 0, 0, 'h' },
00029 { "get", 0, 0, 'g' },
00030 { "put", 0, 0, 'p' },
00031 { "verbose", 0, 0, 'v' },
00032 { "interval", 1, 0, 'i' },
00033 { "file", 1, 0, 'f' },
00034 { 0, 0, 0, 0 }
00035 };
00036
00037 void usage(void)
00038 {
00039 cout << "usage: transfer-client [-h] [-v] [-g]|-p [-i interval] [-f file] <server>" << endl;
00040 cout << " -h, --help this text" << endl;
00041 cout << " -g, --get get-mode for fetching of sieve parameters from server" << endl;
00042 cout << " -p, --put put-mode for sending relations to server" << endl;
00043 cout << " -v, --verbose be verbose" << endl;
00044 cout << " -i, --interval <interval>" << endl;
00045 cout << " number of polynomial intervals to fetch (default:25000)" << endl;
00046 cout << " -f, --file <file>" << endl;
00047 cout << " file to save transmitted data (default:qsieve-fc.param)" << endl;
00048 }
00049
00050 int main(const int argc, char* const argv[])
00051 {
00052 int polynome = 25000;
00053 const char *file = NULL;
00054 bool get = true;
00055 bool option = false;
00056 bool help = false;
00057 bool verbose = false;
00058 string s;
00059 int i, option_index;
00060
00061 while ((i=getopt_long(argc,argv,option_string,long_options,&option_index))!=-1)
00062 {
00063 switch (i)
00064 {
00065 case 'g':
00066 if (option && !get)
00067 {
00068 cerr << "error: -f und -p are mutual exclusive!" << endl;
00069 exit(1);
00070 }
00071 option = get = true;
00072 break;
00073 case 'p':
00074 if (option && get)
00075 {
00076 cerr << "error: -f und -p are mutual exclusive!" << endl;
00077 exit(1);
00078 }
00079 option = true;
00080 get = false;
00081 break;
00082 case 'f':
00083 if (file != NULL)
00084 {
00085 cerr << "error: -f must be specified at most once!" << endl;
00086 exit(1);
00087 }
00088 file = optarg;
00089 break;
00090 case 'h':
00091 help = true;
00092 usage();
00093 break;
00094 case 'i':
00095 polynome = atoi (optarg);
00096 break;
00097 case 'v':
00098 verbose = true;
00099 break;
00100 default:
00101 usage();
00102 exit(1);
00103 }
00104 }
00105
00106 if (file == NULL)
00107 {
00108 if (get)
00109 file = "qsieve-fc.param";
00110 else
00111 file = "qsieve-fc.param.out";
00112 }
00113 if (optind+1 != argc) {
00114 if (help) exit(0);
00115 cerr << "wrong number of parameters!" << endl;
00116 exit(1);
00117 }
00118 const string server = argv[optind];
00119
00120 if(get)
00121 {
00122 char c;
00123 ofstream file_stream(file, ios::out|ios::trunc);
00124 {
00125 unix_io_stream communication_stream(server, server_port);
00126 communication_stream << QsieveLogon << endl;
00127 if (communication_stream.eof())
00128 {
00129 cerr << "Unexpected EOF!" << endl;
00130 exit(1);
00131 }
00132 else
00133 {
00134 communication_stream.get(c);
00135 if (c=='-')
00136 {
00137 cout.put(c);
00138 while (!communication_stream.eof())
00139 {
00140 communication_stream.get(c);
00141 cout.put(c);
00142 }
00143 exit(1);
00144 }
00145 else communication_stream.putback(c);
00146 }
00147 while (!communication_stream.eof())
00148 {
00149 communication_stream.get(c);
00150 file_stream.put(c);
00151 if (verbose) cout.put(c);
00152 }
00153 }
00154 {
00155 unix_io_stream communication_stream(server, server_port);
00156 communication_stream << "DynamicFactors?_ab_fpos 0" << endl;
00157 while (!communication_stream.eof())
00158 {
00159 communication_stream.get(c);
00160 file_stream.put(c);
00161 if (verbose) cout.put(c);
00162 }
00163 }
00164 {
00165 unix_io_stream communication_stream(server, server_port);
00166 communication_stream << "Polynom? " << polynome << endl;
00167 while (!communication_stream.eof())
00168 {
00169 communication_stream.get(c);
00170 file_stream.put(c);
00171 if (verbose) cout.put(c);
00172 }
00173 }
00174 }
00175 else
00176 {
00177 char c;
00178 ifstream file_stream(file, ios::in);
00179 if (!file_stream)
00180 {
00181 cerr << "Unable to open \"" << file << "\"" << endl;
00182 exit(1);
00183 }
00184 char kN_str[1024];
00185 file_stream.getline(kN_str,sizeof(kN_str),'\n');
00186
00187 char line[1024];
00188 while (file_stream.peek()!=EOF)
00189 {
00190 int count = 1000+1;
00191 unix_io_stream communication_stream(server, server_port);
00192
00193 communication_stream << "NewRelations! " << kN_str << endl;
00194
00195 communication_stream >> s;
00196 if (s!="proceed")
00197 {
00198 cerr << "Oops! Server will not accept my relations: " << endl;
00199 cerr << s << endl;
00200 exit(1);
00201 }
00202
00203 while (file_stream.peek()!=EOF && --count)
00204 {
00205 communication_stream << "RL ";
00206 while (file_stream.peek()!='\n')
00207 {
00208 file_stream.get(line,sizeof(line),'\n');
00209 communication_stream << line;
00210 if (verbose) cout << line;
00211 }
00212 file_stream.get(c);
00213 communication_stream << endl;
00214 if (verbose) cout << endl;
00215 }
00216 communication_stream << "Relationsblock_Ende " << flush;
00217 communication_stream >> s;
00218 if (s!="empfangen.")
00219 cerr << "transmitted relations were not acknowledged by server!" << endl;
00220 if (s=="ignoriert!")
00221 {
00222 cerr << "Oops! - Relations were ignored by server!" << endl;
00223 cerr << "Please check, whether transmitted data in "
00224 << "\"" << file << "\"" << endl
00225 << "really belong to current server session!"
00226 << endl;
00227 exit(1);
00228 }
00229 }
00230 }
00231 }