source: tags/0.58/stoposclient.cpp @ 9

Last change on this file since 9 was 9, checked in by willem, 11 years ago

willem

File size: 8.1 KB
Line 
1#include <iostream>
2#include <vector>
3#include <stdlib.h>
4#include <getopt.h>
5#include <fstream>
6#include "wrequest.h"
7#include "shellenv.h"
8#include "stopos.h"
9
10struct returnvalue
11{
12  std::string msg;
13  std::string key;
14  std::string comm;
15  std::string count;
16  std::string present;
17  std::string present0;
18  std::string value;
19
20  void parse(std::string &s)
21  {
22    // we expect somewhere in the output of the server a line like
23    // STOPOS:/OK/123/beautiful
24    // OK        -> msg
25    // 123       -> key
26    // 1         -> comm
27    // beautiful -> value
28    //
29    // OK, 123 and beautiful are hex coded
30    this->msg       = "";
31    this->key       = "";
32    this->comm      = "0";
33    this->count     = "0";
34    this->present   = "0";
35    this->present0  = "0";
36    this->value     = "";
37
38    size_t p = s.find("STOPOS:");
39    if (p == s.npos)
40      return;
41
42    size_t q = s.find('\n',p+1);
43    std::string l = s.substr(p,q-p);
44    std::vector <std::string> v;
45
46    split(v,l,outsep);
47    for (unsigned int i=1; i<v.size(); i++)
48    {
49      switch (i)
50      {
51        case 1: this->msg       = zhextostr(v[i]); break;
52        case 2: this->key       = zhextostr(v[i]); break;
53        case 3: this->comm      = zhextostr(v[i]); break;
54        case 4: this->count     = zhextostr(v[i]); break;
55        case 5: this->present   = zhextostr(v[i]); break;
56        case 6: this->present0  = zhextostr(v[i]); break;
57        case 7: this->value     = zhextostr(v[i]); break;
58      }
59      // special case for key: if key == "", we try to take
60      // it from the environment.
61      if (trim(this->key) == "")
62        this->key = envtostr("STOPOS_KEY");
63    }
64  }
65};
66
67void usage()
68{
69  std::cerr << "Usage:" << std::endl;
70
71  std::cerr << program_name << " -h,--help : this message" << std::endl;
72  std::cerr << program_name << " -c,--create [-p,--pool POOL] [-i,--input   INPUTFILE]" << std::endl;
73  std::cerr << program_name << " -n,--next   [-p,--pool POOL] [-d --lockdir LOCKDIR  ] [-k,--key KEY] [-m,--max MAX]" << std::endl;
74  std::cerr << program_name << " -r,--ready  [-p,--pool POOL] [-d --lockdir LOCKDIR  ] [-k,--key KEY]" << std::endl;
75  std::cerr << program_name << " -s,--stats  [-p,--pool POOL] [-d --lockdir LOCKDIR  ]" << std::endl;
76  std::cerr << program_name << " -u,--unhandled  [-p,--pool POOL] [-d --lockdir LOCKDIR  ]" << std::endl;
77  std::cerr << program_name << " -v,--version" << std::endl;
78  std::cerr << "Defaults:" << std::endl;
79  std::cerr << "  POOL     : " << "d.defpath_pool" << std::endl;
80  std::cerr << "  LOCKDIR  : " << "d.defpath_lockdir"  << std::endl;
81  std::cerr << "  MAX      : " << "1" << std::endl;
82  std::cerr << "  INPUTFILE: " << "d.definputfilename";
83  //if ("d.definputfilename" == "-")
84   // std::cerr << " (standard input)";
85  std::cerr << std::endl;
86}
87
88int handle_add(wrequest &w,const std::string &fname,returnvalue &r)
89{
90
91  int rc;
92  std::ifstream inp;
93  std::istream *pinp;
94  if(fname == "")  // read from stdin
95    pinp = &std::cin;
96  else
97  {
98    inp.open(fname.c_str());
99    if (!inp.is_open())
100    {
101      r.msg = "ERROR: Cannot open '"+fname+"'";
102      return 1;
103    }
104    pinp = &inp;
105  }
106  std::string line;
107  size_t n = 0;
108  w.set_command("add");
109  std::string body,header;
110  while (getline(*pinp,line))
111  {
112    w.set_value(line);
113    rc = w.send_message(body,header);
114    if (rc != 0)
115    {
116      r.msg = "ERROR: Cannot connect to server";
117      return 1;
118    }
119    r.parse(body);
120    if (r.msg != "OK")
121      break;
122    n++;
123  }
124  std::cerr << program_name << ": Number of lines added = "<<n<<std::endl;
125  if (r.msg != "OK")
126    return 1;
127  return 0;
128}
129
130int main(int argc, char*argv[])
131{
132  wrequest w;
133 
134  int rc;
135
136  rc = w.read_config();
137  if (rc != 0 || w.get_config().size() != w.config_size)
138  {
139    std::cerr << "No valid config file found, creating it ..." << std::endl;
140    rc = w.write_config();
141    if (rc != 0)
142    {
143      std::cerr << "Cannot write config file, exiting." << std::endl;
144      return 1;
145    }
146  }
147
148  bool addflag=0;
149  std::string inputfilename;
150  std::vector <std::string> parms;
151
152  static struct option long_options[] =
153  {
154    {"help",     0, 0, 'h'},
155    {"pool",     1, 0, 'p'},
156    {"version",  0, 0, 'v'},
157    {"multi",    0, 0, 'm'},
158    {0,          0, 0, 0}
159  };
160
161  // determine name of pool, in case it is not set on commandline
162
163  std::string pool = default_pool;
164  std::string p = envtostr("STOPOS_POOL");
165
166  if (p != "")
167    pool = p;
168
169  if (trim(w.get_pool()) != "")
170    pool = w.get_pool();
171
172  while(1)
173  {
174    int c = getopt_long(argc,argv,"-hp:vm",long_options,0);
175    if (c == -1)
176    {
177      if (optind < argc)  // unexpected extra argument
178      {
179        std::cerr << program_name << ": Unexpected argument:" << argv[optind] << std::endl;
180        return 1;
181      }
182      break;
183    }
184    if ( c == '?' || c == ':' )
185    {
186      std::cerr << program_name << ": Invalid parameter, try "<< program_name<<" --help " <<std::endl;
187      return 1;
188    }
189    switch(c)
190    {
191      case 'h':
192        usage();
193        return 0;
194        break;
195      case 'p':
196        pool = optarg;
197        break;
198      case 'f':
199        inputfilename = optarg;
200        break;
201      case 'v':
202        std::cerr << program_version << std::endl;
203        return 0;
204        break;
205      case 'm':
206        w.set_multi("yes");
207        break;
208      case 1:
209        parms.push_back(optarg);
210    }
211  }
212
213  w.set_pool(pool);
214  w.set_serverurl("http://localhost:5050/cgi-bin/stoposserver/");
215  std::string  prot;
216  prot = "mysql";
217  prot = "gdbm";
218  prot = "files";
219  prot = "flat";
220  std::string protenv;
221  protenv = envtostr("STOPOS_PROT");
222  if (protenv.size() !=0)
223    prot = protenv;
224  w.set_protocol(prot);
225  w.set_whoami(envtostr("USER"));
226
227  rc = 0;
228  returnvalue r;
229
230  if (parms.size() == 0)
231  {
232    std::cerr << program_name << ": No command given." << std::endl;
233    return 1;
234  }
235
236  std::string command = parms[0];
237
238  // only the remove and add commands can accept an extra parameter
239  //
240
241  if (parms.size() >1)
242    if (command !="remove" && command != "add")
243    {
244      std::cerr << program_name << ": Extraneous parameter:'"<<parms[1]<<"'."<<std::endl;
245      return 1;
246    }
247
248  if(command == "create")
249  {
250    w.set_command("create");
251  }
252  else if(command == "purge")
253  {
254    w.set_command("purge");
255  }
256  else if(command == "status")
257  {
258    w.set_command("status");
259  }
260  else if(command == "next")
261  {
262    w.set_command("next");
263  }
264  else if(command == "dump")
265  {
266    w.set_command("dump");
267  }
268  else if(command == "pools")
269  {
270    w.set_command("pools");
271  }
272  else if(command == "remove")
273  {
274    w.set_command("remove");
275    if (parms.size() < 2)
276    {
277      std::string a=envtostr("STOPOS_KEY");
278      if (a == "")
279      {
280        std::cerr << program_name << ": No key found, exit.\n";
281        return 1;
282      }
283      w.set_value(a);
284    }
285    else
286      w.set_value(parms[1]);
287  }
288  else if(command == "add")
289  {
290    addflag = 1;
291  }
292  else
293  {
294    std::cerr << program_name << ": Invalid command: '"<<command<<"'"<<std::endl;
295    return 1;
296  }
297
298  if(addflag)
299  {
300    if (parms.size() > 1)
301      inputfilename = parms[1];
302    rc |= handle_add(w,inputfilename,r);
303  }
304  else
305  {
306    std::string body,header;
307
308    rc = w.send_message(body,header);
309
310    if (rc != 0)
311      r.msg = "ERROR: Cannot connect to server";
312    else
313      r.parse(body);
314  }
315
316  int shelltype;
317
318  char *shell = getenv("SHELL");
319  std::string sh;
320  if (shell == 0)
321    sh = "bash";
322  else
323    sh = shell;
324
325  if (sh.find("csh") != sh.npos)
326    shelltype=SHELL_CSH;
327  else
328    shelltype = SHELL_SH;
329
330  std::cout << shellenv(r.msg,      "STOPOS_RC",        shelltype);
331  std::cout << shellenv(r.key,      "STOPOS_KEY",       shelltype);
332  std::cout << shellenv(r.comm,     "STOPOS_COMMITTED", shelltype);
333  std::cout << shellenv(r.count,    "STOPOS_COUNT",     shelltype);
334  std::cout << shellenv(r.present,  "STOPOS_PRESENT",   shelltype);
335  std::cout << shellenv(r.present0, "STOPOS_PRESENT0",  shelltype);
336  std::cout << shellenv(r.value,    "STOPOS_VALUE",     shelltype);
337
338  if (command == "status")
339  {
340    std::cerr << "lines added:           " << r.count    <<std::endl;
341    std::cerr << "lines present:         " << r.present  <<std::endl;
342    std::cerr << "lines never committed: " << r.present0 << std::endl;
343  }
344  else if (command == "pools")
345  {
346    std::cerr << r.value << std::endl;
347  }
348  if (r.msg != "OK")
349  {
350    std::cerr << program_name<<": "<<r.msg << std::endl;
351    rc = 1;
352  }
353
354  return rc;
355}
Note: See TracBrowser for help on using the repository browser.