root/gsdl/trunk/src/corba/corbaServer.mpp @ 15459

Revision 15459, 14.2 KB (checked in by mdewsnip, 12 years ago)

Started to move all the CORBA stuff out into a new "src/corba" directory, because it is uglying up the core source directories and Makefiles.

  • Property svn:keywords set to Author Date Id Revision
Line 
1// Standard headers
2#include <iostream.h>
3#include <fstream.h>
4
5// protocol headers
6#include "corbaiface.h"
7using namespace gsdlInterface;
8
9#include "corbaconv_text_t.h"
10
11// library headers
12#include "fileutil.h"
13//#include "gsdlhome.h"
14
15// greenstone headers
16#include "comtypes.h"
17#include "recptproto.h"
18#include "nullproto.h"
19
20// local headers
21#include "corbatext_t.h"
22
23void corbatext_setToCorbaArray(text_tset set, corbatext_tset *cset);
24void corbatext_corbaArrayToSet(corbatext_tset cset, text_tset *set);
25void corbatext_mapToCorbaMap(text_tmap map, corbatext_tmap *tm);
26void corbaresponse_resultDocInfoToCorba(ResultDocInfo_tarray docinfo, corbaResultDocInfo_array &corbainfo);
27
28void corbaresponse_termInfoToCorba(TermInfo_tarray terminfo, corbaTermInfo_array &corbainfo) {
29  TermInfo_tarray::iterator here = terminfo.begin();
30  TermInfo_tarray::iterator end  = terminfo.end();
31  unsigned int i;
32   
33  i = 0;
34
35  while (here != end)
36    {
37      corbainfo.length(i+1);
38     
39      corbainfo[i].frequency = here->freq;
40      corbaconv_text_t::getCorbatext(here->term,corbainfo[i].term);
41      corbatext_arrayToCorbaArray(here->matchTerms, &corbainfo[i].matchTerms);
42
43      i ++;
44      here ++;
45    }
46  cout << "Terminfo " << i << endl;
47}
48
49void corbatext_optionValueArrayToC(corbaOptionValue_array corba, OptionValue_tarray &array)
50{
51  unsigned int i = 0;
52  OptionValue_t option;
53
54  for (i = 0; i < corba.length(); i ++)
55    {
56      corbaconv_text_t::setCorbatext(option.name,corba[i].name);
57      corbaconv_text_t::setCorbatext(option.value,corba[i].value);
58
59      // cout << corbatext_string(corba[i].name) << "?" << option
60
61      array.push_back(option);
62    }
63}
64
65class corbaServer : virtual public corbaiface_skel
66{
67private:
68  text_tarray collections;
69  recptproto *protocol;
70  text_t      gsdlhome;
71
72  void openLogfile(text_t extension, ofstream &out)
73  {
74    char *cfilename;
75    text_t filename;
76
77    filename = filename_cat(gsdlhome, extension);
78    cfilename = filename.getcstr();
79
80    out.open(cfilename);
81    delete cfilename;
82  }
83
84public:
85  corbaServer(recptproto *protocol, text_t home)
86  {
87    text_t dirname;
88    char * cdirname;
89
90    cout << "CorbaServer: gsdlhome = " << home << endl;
91
92    this->gsdlhome = home;
93    cout << gsdlhome.getcstr() << endl;
94    dirname = filename_cat(gsdlhome, "/collect");
95    cdirname = dirname.getcstr();
96    if (!read_dir (cdirname, collections)) {
97      cout << "Unable to read the gsdl directory; terminating" << endl;
98      exit (1);
99    }
100    delete cdirname;
101
102    cout << "Constructing set list server" << endl;
103    this->protocol = protocol;
104
105  }
106 
107  CORBA::Boolean initialise(corbaComError &err)
108  {
109      ofstream      logout;
110      int           reply;
111      comerror_t    error = noError;
112
113      this->openLogfile("/etc/corbaout.txt", logout);
114      reply = protocol->init(error, logout);
115      logout.close();
116      return (reply != 0 ? 1 : 0);
117    }
118
119  void configure(const corbatext_t &corbaKey, const corbatext_tarray &corbaCfgline,corbaComError &err)
120    {
121      text_t        key;
122      text_tarray   cfgline;
123      comerror_t    error = noError;
124
125      corbaconv_text_t::setCorbatext(key,corbaKey);
126      corbatext_corbaArrayToArray(corbaCfgline, &cfgline);
127
128
129      // ****
130      cout << "Recieved " << key << " = ";
131      for (int unsigned i=0; i<cfgline.size(); i++)
132    {
133      cout << cfgline[i] << " ";
134    }
135      cout << endl;
136
137      // DB // ****
138      if (key=="gsdlhome")
139    { 
140      text_tarray cfgline;
141      cfgline.push_back (gsdlhome);
142      cout << "Changing gsdlhome to " << gsdlhome << endl;
143      protocol->configure(key, cfgline, error);
144    }
145      else if (key=="httpdomain")
146    {
147      // Only let gsdlhome through !!!! // ****
148      cout << "Supressing httpdomain" << endl;
149    }
150      else if (key=="httpprefix")
151    {
152      cout << "Supressing httpprefix" << endl;
153    }
154      else
155    {
156      protocol->configure(key, cfgline,error);
157    }
158    }
159
160  void hasCollection(const corbatext_t &corbaCollect, CORBA::Boolean &has,
161                     corbaComError &error) {
162    // do nowt!
163    corbaconv_text_t *cct;
164    bool    _has;
165    comerror_t err = noError;
166    ofstream logout;
167
168    this->openLogfile("/etc/corbaout.txt", logout);
169
170    cct = new corbaconv_text_t(corbaCollect);
171
172    cout << "Collection: " << cct->getcstr() << endl;
173
174    protocol->has_collection(*cct, _has, err, logout); // 'cct' typecast to text_t
175    if (_has)
176      {
177    has = 1;
178      }
179    else
180      {
181    has = 0;
182      }
183
184    // cout << "  " << has << endl;
185
186    delete cct;
187
188    error = corbaNoError;
189    logout.close();
190  }
191
192  void ping(const struct corbatext_t &corbaCollect, CORBA::Boolean &wasSuccess, corbaComError &corbaError)
193  {
194    text_t collection;
195    bool   success;
196    comerror_t error = noError;
197    ofstream logout;
198
199    this->openLogfile("/etc/corbaout.txt", logout);
200
201    corbaconv_text_t::setCorbatext(collection,corbaCollect);
202    protocol->ping(collection, success, error, logout);
203
204    corbaError = (corbaComError) error;
205    wasSuccess = (CORBA::Boolean) success;
206    logout.close();
207  }
208
209  void filter(const struct corbatext_t &corbaCollect,
210          const struct corbaFilterRequest &corbaRequest,
211          struct corbaFilterResponse &corbaResponse,
212          corbaComError &corbaError)
213  {
214    // variables for action
215    FilterRequest_t request;
216    FilterResponse_t response;
217    comerror_t       error = noError;
218    text_t           collection;
219    ofstream         logout;
220
221    this->openLogfile("/etc/corbaout.txt", logout);
222
223    // copy collection id
224    corbaconv_text_t::setCorbatext(collection,corbaCollect);
225
226    // copy request values
227    corbaconv_text_t::setCorbatext(request.filterName,corbaRequest.filter);
228    corbatext_optionValueArrayToC(corbaRequest.filterOptions, request.filterOptions);
229    corbatext_corbaArrayToArray(corbaRequest.docSet, &request.docSet);
230    request.filterResultOptions = corbaRequest.filterResultOptions;
231    corbaconv_text_t::setCorbatext(request.requestParams,corbaRequest.requestParams);
232    corbaconv_text_t::setCorbatext(request.refParams,corbaRequest.refParams);
233    corbatext_corbaArrayToSet(corbaRequest.fields, &request.fields);
234    request.getParents = corbaRequest.getParents;
235
236    protocol->filter(collection, request, response, error, logout);
237
238    // copy response values
239    corbaResponse.numDocs = response.numDocs;
240    corbaResponse.isApprox = (::gsdlInterface::corbaIsApprox) response.isApprox;
241    corbaresponse_termInfoToCorba(response.termInfo, corbaResponse.termInfo);
242    corbaresponse_resultDocInfoToCorba(response.docInfo, corbaResponse.docInfo);
243
244    // tidy up variables
245    logout.close();
246    corbaError = (corbaComError) error;
247    cout << "Filter prepared" << corbaResponse.docInfo.length() << "," << corbaResponse.numDocs << endl;
248  }
249
250  void getCollectInfo(const struct corbatext_t &corbaCollect,
251                      corbaColInfoResponse &corbaResponse,
252                      corbaComError &corbaError)
253  {
254    ColInfoResponse_t response;
255    comerror_t       error = noError;
256    text_t           collection;
257    ofstream logout;
258
259    this->openLogfile("/etc/corbaout.txt", logout);
260
261    corbaconv_text_t::setCorbatext(collection,corbaCollect);
262    protocol->get_collectinfo(collection, response, error, logout);
263
264    cout << "IsPublic  = " << ((response.isPublic)?"True":"False") << endl;
265    cout << "IsBeta    = " << ((response.isBeta)  ?"True":"False") << endl;
266    cout << "BuildDate = " << response.buildDate << endl;
267    cout << "NumDocs   = " << response.numDocs << endl;
268    cout << "NumBytes  = " << response.numBytes << endl;
269    cout << "NumWords  = " << response.numWords << endl;
270    cout << "UseBook   = " << ((response.useBook)  ?"True":"False") << endl;
271   
272    corbaResponse.useBook   = response.useBook;
273    corbaResponse.isPublic  = response.isPublic;
274    corbaResponse.isBeta    = response.isBeta;
275    corbaResponse.buildDate = response.buildDate;
276    corbatext_arrayToCorbaArray(response.languages, &corbaResponse.languages);
277    corbatext_arrayToCorbaArray(response.ccsCols, &corbaResponse.ccsCols);
278    corbaResponse.numDocs   = response.numDocs;
279    corbaResponse.numBytes  = response.numBytes;
280    corbaResponse.numWords  = response.numWords;
281
282    corbatext_mapToCorbaMap(response.collectionmeta, &corbaResponse.collectionMeta);
283    corbatext_mapToCorbaMap(response.format, &corbaResponse.format);
284    corbatext_mapToCorbaMap(response.building, &corbaResponse.building);
285    corbaconv_text_t::getCorbatext(response.httpdomain,corbaResponse.httpdomain);
286
287    corbaconv_text_t::getCorbatext(response.httpprefix,corbaResponse.httpprefix);
288    corbaconv_text_t::getCorbatext(response.receptionist,corbaResponse.receptionist);
289
290    /* text_tmap::iterator f_here = response.format.begin();
291    text_tmap::iterator f_end = response.format.end();
292    while (f_here!=f_end)
293    {
294      cout << "**** format: " << f_here->first << " = " << f_here->second << endl;
295      f_here++;
296    }
297    */ // ****
298
299    logout.close();
300    corbaError = (corbaComError) error;
301  }
302
303  void getFilterOptions(const struct corbatext_t &corbaCollect,
304            const struct corbatext_t &corbaFilterName,
305            struct corbaFilterOptionsResponse &corbaResponse,
306            enum corbaComError &corbaError)
307  {   
308    text_t collection;
309    InfoFilterOptionsRequest_t request;
310    InfoFilterOptionsResponse_t response;
311    comerror_t error = noError;
312    unsigned int i;
313    ofstream logout;
314
315    this->openLogfile("/etc/corbaout.txt", logout);
316
317    corbaconv_text_t::setCorbatext(collection,corbaCollect);
318    corbaconv_text_t::setCorbatext(request.filterName,corbaFilterName);
319    protocol->get_filteroptions(collection, request, response, error, logout);
320
321    // decode filters back into corba format
322    FilterOption_tmap::iterator here = response.filterOptions.begin();
323    FilterOption_tmap::iterator end = response.filterOptions.end();
324    i = 0;
325    while (here != end)
326      {
327    // set array for name/options to be one bigger
328    corbaResponse.names.length(i+1);
329    corbaResponse.options.length(i+1);
330
331    // get left-hand side of map
332    corbaconv_text_t::getCorbatext((*here).first,corbaResponse.names[i]);
333   
334    // get right-hand side of map
335    corbaconv_text_t::getCorbatext((*here).second.name,corbaResponse.options[i].name);
336    corbaResponse.options[i].type =
337      (::gsdlInterface::corbaFilterType)(*here).second.type;
338    corbaResponse.options[i].repeatable =
339      (::gsdlInterface::corbaFilterRepeatable)(*here).second.repeatable;
340    corbaconv_text_t::getCorbatext((*here).second.defaultValue,corbaResponse.options[i].defaultValue);
341    corbatext_arrayToCorbaArray((*here).second.validValues, &corbaResponse.options[i].validValues);
342
343    // take next iterators
344    i++;
345    here ++;
346      }
347
348    logout.close();
349    corbaError = (corbaComError) error;
350  }
351
352  void getFilterInfo(const struct corbatext_t &corbaCollect,
353             corbatext_tarray &filterNames,
354             enum corbaComError &corbaError)
355  { text_tarray ta;
356    comerror_t error = noError;
357    text_t collection;   
358    InfoFiltersResponse_t response;
359    ofstream logout;
360
361    this->openLogfile("/etc/corbaout.txt", logout);
362
363    corbaconv_text_t::setCorbatext(collection,corbaCollect);
364    cout << collection.getcstr() << endl;
365               
366    protocol->get_filterinfo(collection, response, error, logout);
367    corbatext_setToCorbaArray(response.filterNames, &filterNames);
368
369    corbaError = (corbaComError) error;
370    logout.close();
371  }
372 
373  void getDocument(const struct corbatext_t &corbaCollection, struct corbaDocRequest &corbaRequest,
374           struct corbaDocResponse &corbaResponse,  enum corbaComError &corbaError)
375  { DocumentRequest_t request;
376    DocumentResponse_t response;
377    comerror_t         error = noError;
378    text_t             collection;
379    ofstream logout;
380
381    this->openLogfile("/etc/corbaout.txt", logout);
382
383    // set up request
384    corbaconv_text_t::setCorbatext(collection,corbaCollection);
385    corbaconv_text_t::setCorbatext(request.OID,corbaRequest.OID);
386    corbaconv_text_t::setCorbatext(request.docType,corbaRequest.docType);
387    corbaconv_text_t::setCorbatext(request.docFormat,corbaRequest.docFormat);
388
389    // do actual get document
390    protocol->get_document(collection, request, response, error, logout);
391    corbaconv_text_t::getCorbatext(response.doc,corbaResponse.doc);
392
393    // decode response
394    corbaError = (corbaComError) error;
395
396    logout.close();
397  }
398
399  void collectionList(corbatext_tarray &corbalist, corbaComError &error)
400  {
401    /* stringSeq *reply;
402    unsigned int i;
403    char *istring; */
404
405    text_tarray collist;
406    comerror_t  err;
407    ofstream logout;
408
409    cout << "Received collection list request" << endl;
410
411    this->openLogfile("/etc/corbaout.txt", logout);
412
413    protocol->get_collection_list(collist, err, logout);
414
415    /*    reply = new stringSeq(collections.size());
416   
417    (*reply).length(collections.size());
418    for (i = 0; i < collections.size(); i ++)
419      {
420    istring = collections[i].getcstr();
421    (*reply)[i] = CORBA::string_dup(istring);
422    delete istring;
423      }
424   
425    return reply;
426    */
427
428    corbatext_arrayToCorbaArray(collist, &corbalist);
429    cout << "Replying " << collist.size() << ": " << endl;
430
431    for (int unsigned i = 0; i < collist.size(); i ++)
432      {
433    cout << collist[i];
434    if (i<collist.size()-1) cout << ", ";
435      }
436    cout << endl;
437
438    logout.close();
439    // CORBA::string_dup(_listString);
440  }
441};
442
443int main (int argc, char *argv[])
444{
445  nullproto protocol;
446  collectset *cservers;
447  text_t     gsdlhome;
448
449  cservers = new collectset(gsdlhome);
450  protocol.set_collectset(cservers);
451
452  cout << "Started Corba ..." << endl;
453
454  // ORB initialization
455  CORBA::ORB_var orb = CORBA::ORB_init( argc, argv, "mico-local-orb" );
456  CORBA::BOA_var boa = orb->BOA_init( argc, argv, "mico-local-boa" );
457
458  cout << "Initialised ORB and BOA" << endl;
459
460  corbaServer* server = new corbaServer(&protocol, gsdlhome);
461
462  cout << "Initialised server" << endl;
463
464  CORBA::String_var ref = orb->object_to_string( server );
465
466  cout << "Mapped Corba object to string" << endl;
467
468  // write id to "naming service"
469  ofstream out ("/tmp/localcorba.objid");
470    if (out.is_open()) {
471        out << ref << endl;
472        if (out.fail() || out.bad()) {
473            cout << "Failed " << endl;
474        }
475        out.close ();
476    }
477    else {
478        cout << "Unable to write string to file /tmp/localcorba.objid" << endl;
479        cout << "Please check the file permissions/ownership" << endl;
480        return 0;
481    }
482  cout << "Corba string is " << ref << endl;
483
484  // indicate readiness and run
485  boa->impl_is_ready( CORBA::ImplementationDef::_nil() );
486  orb->run ();
487  CORBA::release( server );
488  return 0;
489}
490
491
Note: See TracBrowser for help on using the browser.