source: tags/0.5/stoposserver.cpp @ 9

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

willem

File size: 6.5 KB
Line 
1#include <iostream>
2#include <stdlib.h>
3#include <vector>
4#include <string>
5#include "stopos.h"
6#include "wutils.h"
7#include "stopos_pool.h"
8#include "gdbm_pool.h"
9#include "flatfile_pool.h"
10#include "files_pool.h"
11#include "mysql_pool.h"
12
13void output(const std::string &return_message, const std::string &key,
14             unsigned int committed, const std::string &return_value)
15{
16  std::cout << "STOPOS:"                 << outsep <<
17               zstrtohex(return_message) << outsep <<
18               zstrtohex(key)            << outsep <<
19               zstrtohex(NumberToString(committed)) << outsep <<
20               zstrtohex(return_value) << 
21               std::endl;
22}
23
24void output(const std::string &msg)
25{
26  output(msg,"",0,"");
27}
28struct commandline
29{
30  std::string header;
31  std::string prot;
32  std::string id;
33  std::string pool;
34  std::string command;
35  std::string multi;
36  std::string value;
37  std::string full_dbname;
38  std::string return_value;
39  stopos_pool *handler;
40
41  int create_full_dbname(void)
42  {
43    if (this->prot == "gdbm" ||
44        this->prot == "flat" ||
45        this->prot == "files")
46    {
47      this->full_dbname = "/tmp/"              +
48                          zstrtohex(this->id)   +
49                          "/"                  +
50                          zstrtohex(this->pool) +
51                          "_"                  +
52                          this->prot;
53    }
54    else if (this->prot == "mysql")
55    {
56      this->full_dbname = zstrtohex(this->id) +
57                          "_"                +
58                          zstrtohex(this->pool);
59    }
60    else
61      return 1;
62
63    this->handler->set_db_name(this->full_dbname);
64    return 0;
65  }
66
67  int create_handler()
68  {
69    if (this->prot == "gdbm")
70    {
71      this->handler = new gdbm_pool();
72    }
73    if (this->prot == "flat")
74    {
75      this->handler = new flatfile_pool();
76    }
77    if (this->prot == "files")
78    {
79      this->handler = new files_pool();
80    }
81    if (this->prot == "mysql")
82    {
83      this->handler = new mysql_pool();
84    }
85    return 0;
86  }
87
88  int init()
89  {
90    int rc;
91    rc = this->create_handler();
92    if (rc != 0) 
93      return rc;
94    if (this->multi == "yes")
95      this->handler->set_kvp(1);
96    else
97      this->handler->set_kvp(0);
98    rc = this->create_full_dbname();
99    return rc;
100  }
101
102  int create_db()
103  {
104    int rc = this->handler->create_db();
105    return rc;
106  }
107
108  int purge_db()
109  {
110    int rc = this->handler->purge_db();
111    return rc;
112  }
113
114  int add_line(std::string &key,std::string &r)
115  {
116    int rc;
117    rc = this->handler->open_db();
118    if (rc != 0)
119    {
120      r = "ERROR: cannot open pool "+this->pool;
121      return rc;
122    }
123
124    rc = this->handler->add_line(this->value,key);
125    if (rc != 0)
126    {
127      r = "ERROR: cannot write to pool "+this->pool;
128      return rc;
129    }
130    return 0;
131  }
132
133  int get_line(std::string &key, unsigned int &comm, std::string &r)
134  {
135    int rc;
136    rc = this->handler->open_db();
137    if (rc != 0)
138    {
139      r = "ERROR: cannot open pool "+this->pool;
140      return rc;
141    }
142    rc = this->handler->get_line(this->return_value, comm, key);
143    std::cerr << "WTD:"<<__FILE__<<__LINE__<<":"<<this->return_value<<":"<<comm<<std::endl;
144    return rc;
145  }
146
147  int remove_line(std::string & rkey,std::string &key, std::string &r)
148  {
149    int rc;
150    rc = this->handler->open_db();
151    if (rc != 0)
152    {
153      r = "ERROR: cannot open pool "+this->pool;
154      return rc;
155    }
156    rkey = key;
157    rc = this->handler->remove_line(key);
158    std::cerr << "WTD:"<<__FILE__<<__LINE__<<":"<<rc<<std::endl;
159    return rc;
160  }
161
162};
163
164std::string sanitycheck(commandline &l)
165{
166  if (l.header != "stopos")
167    return "invalid header";
168
169  if (l.prot.find_first_of(' ') != l.prot.npos)
170    return "no valid protocol given";
171
172  if(l.id.find_first_of(' ') != l.id.npos)
173    return "no valid id given";
174
175  if(l.pool.find_first_of(' ') != l.pool.npos)
176  {
177    return "no valid pool given";
178  }
179  if(l.command.find_first_of(' ') != l.command.npos)
180  {
181    return "no valid command given";
182  }
183  return "";
184}
185
186int parsecom(commandline &l,std::string &line)
187{
188  std::vector <std::string> v;
189  split(v,line,httpsep); 
190                       
191  for (unsigned int i =0; i<v.size(); i++)
192  {
193    std::cout <<"WTD:"<< i << ":[" << zhextostr(v[i]) << "]" << std::endl;
194    switch(i)
195    {
196      case 0: l.header   = zhextostr(v[i]); break;
197      case 1: l.prot     = zhextostr(v[i]); break;
198      case 2: l.id       = zhextostr(v[i]); break;
199      case 3: l.pool     = zhextostr(v[i]); break;
200      case 4: l.command  = zhextostr(v[i]); break;
201      case 5: l.multi    = zhextostr(v[i]); break;
202      case 6: l.value    = zhextostr(v[i]); break;
203    }
204  }
205  return 0;
206}
207
208
209int executecom(commandline &l)
210{
211  int rc;
212  unsigned int committed = 0;
213
214  std::string return_message;
215  std::string key;
216  rc = l.init();
217  if (rc != 0)
218  {
219    output("ERROR: cannot initialize server software");
220    return rc;
221  }
222
223  if (l.command == "create")
224  {
225    std::cerr << "WTD: execute:"<<l.command<<std::endl;
226    std::cerr << "WTD: full:"<<l.full_dbname<<std::endl;
227    std::cerr << "WTD: prot:"<<l.prot<<std::endl;
228
229    rc = l.create_db();
230    if (rc != 0)
231      return_message = "ERROR: cannot create pool "+l.pool;
232    std::cerr << "WTD: created! :"<<rc<<std::endl;
233  }
234  else if (l.command == "purge")
235  {
236    std::cerr << "WTD: execute:"<<l.command<<std::endl;
237    rc = l.purge_db();
238  }
239  else if (l.command == "add")
240  {
241    std::cerr << "WTD: add:"<<l.command<<std::endl;
242    rc = l.add_line(key,return_message);
243    std::cerr << "WTD: added! :"<<rc<<":'"<<l.value<<"'"<<std::endl;
244  }
245  else if (l.command == "next")
246  {
247    std::cerr << "WTD: next:"<<l.command<<std::endl;
248    rc = l.get_line(key, committed, return_message);
249    std::cerr << "WTD: nexted! :"<<rc<<":"<<committed<<std::endl;
250  }
251  else if (l.command == "remove")
252  {
253    std::cerr << "WTD: ready:"<<l.command<<std::endl;
254    rc = l.remove_line(key,l.value, return_message);
255    std::cerr << "WTD: readyed! :"<<rc<<std::endl;
256  }
257  else rc = 1;
258  if (rc == 0)
259    return_message="OK";
260  else
261    if (return_message == "")
262      return_message="ERROR";
263
264  output(return_message, key, committed, l.return_value);
265  return rc;
266}
267
268int main()
269{
270  int rc;
271  std::cout << "Content-Type: text/plain\n\n";
272  std::cout << "WTD: Dit is stoposserver\n";
273  std::string line;
274  line = envtostr("PATH_INFO");
275  if (line.size() >0)
276    if (line[0] == '/')
277      line.erase(0,1);
278  std::cout << "WTD: PATH_INFO '"<<line <<"'"<< std::endl;
279
280  commandline l;
281  parsecom(l,line);
282  std::string msg = sanitycheck(l);
283  if (msg != "")
284  {
285    output(msg);
286    return 1;
287  }
288  rc = executecom(l);
289
290  std::cout << "WTD: stoposserver done\n";
291  return rc;
292}
Note: See TracBrowser for help on using the repository browser.