summaryrefslogtreecommitdiffstats
path: root/servercomm/servercomm.hh
blob: 460fc2c165aeb66cbfcf202250e87d93539b7e80 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
/*
* This file is part of rasdaman community.
*
* Rasdaman community is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Rasdaman community 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with rasdaman community.  If not, see <http://www.gnu.org/licenses/>.
*
* Copyright 2003, 2004, 2005, 2006, 2007, 2008, 2009 Peter Baumann /
rasdaman GmbH.
*
* For more information please see <http://www.rasdaman.org>
* or contact Peter Baumann via <baumann@rasdaman.com>.
/
/**
 * INCLUDE: servercomm.hh
 *
 * MODULE:  servercomm
 * CLASS:   ServerComm
 *
 * COMMENTS:
 *			None
*/

#ifndef _SERVERCOMM_
#define _SERVERCOMM_

// Put it in front of any typedef bool ... because o2 is using bool as a variable. 
// #include "o2template_CC.hxx"

#include <list>

#include <rpc/rpc.h>
#include "raslib/error.hh"
#include "raslib/oid.hh"
#include "raslib/minterval.hh"

#include "reladminif/adminif.hh"
#include "reladminif/databaseif.hh"
#include "reladminif/transactionif.hh"
#include "relcatalogif/basetype.hh"

#include "servercomm/callbackmgr.hh"

#ifndef _RPCIF_
  #define _RPCIF_
  #include "clientcomm/rpcif.h"
#endif

#ifdef DECALPHA
#include <rpc/svc.h>
#endif

// forward declarations
class QtData;
class MDDObj;
class MDDCollIter;
class MDDColl;
class r_Parse_Params;

//@ManMemo: Module: {\bf servercomm}

/*@Doc:
  The class servercomm describes the one and only server communication object
  that can exist in a RasDaMan RPC server. It manages listening for client and
  maps incoming calls to the respective remote procedures (which reside in the
  file manager.cc). These remote procedures are global functions
  which mainly concern with RPC call processing and finally call the methods 
  of this servercomm class to forward client requests.
*/

class ServerComm
{
  public:
  
    /// the class defines an entry of the client table
    class ClientTblElt
    {
      public:
        /// default constructor
        ClientTblElt( const char* clientIdText, unsigned long clientId );	
        /**
          Default constructor that takes the information to be placed in the
	  clientIdText field of the client table entry and the unique ID to
	  be placed in the clientId field.
	*/

        /// destructor
	~ClientTblElt();

        /// release client context
        void release();
        /**
          Releasing the client context means to decrease the currentUsers counter
          and to update lastActionTime.
        */
        
	void endRequest();
	 
	/// releases transfer collection/iterator
	void releaseTransferStructures();	
	/**
	  The method releases transfer collection and iterator. As the collection is a
	  persistent one, care has to be taken that creation and deletion is done
	  within the same transaction.
	*/
	
        /// unique client identification assigned by the server
	unsigned long  clientId;

        /// counter indicating the number of current users
        unsigned int   currentUsers;

	/// binding information about the client (IP address and TCP port number)
	char*          clientIdText;

	/// Name of the client user name (if available)
	char*          userName;
	
	/// Name of the actual database (if one is open)
	char*          baseName;
	
	/// time when the database was opened (for curiosity purposes)
	unsigned long  creationTime;

	/// time of the client's last action (for garbage collection purposes)
	unsigned long  lastActionTime;
	
        /// convert raw array data to this data format before transfer
        r_Data_Format  transferFormat;
        char*          transferFormatParams;
        /// send data to client in the exact transfer format
        int            exactFormat;
        /// store array data in this data format in the database
        r_Data_Format  storageFormat;
        char*          storageFormatParams;

        /// the tile data converted into the transfer format, if required
        void*          encodedData;
        unsigned long  encodedSize;
        /// for establishing the compression ratio
        unsigned long  totalRawSize;
        unsigned long  totalTransferedSize;

	/// pointer to an MDD collection
        MDDColl*       transferColl;
        /**
           For collection of MDD constants with an update query.
        */

        /// pointer to an iterator for collection transferColl
	MDDCollIter*   transferCollIter;
	
        /// pointer to the query result which is currently in transfer
        std::vector<QtData*>* transferData;
        /**
           For the result of the last query (NULL if the result is completely delivered to the client).
        */

        /// point to an iterator for transfer data
        std::vector<QtData*>::iterator* transferDataIter;

	/// pointer to a persistent MDD object for tile based transfers
	MDDObj*    assembleMDD;

        /// pointer to an MDD object for tile base transfer
        MDDObj*        transferMDD;
	
	/// std::vector storing tiles of actual MDD for transfer
	std::vector< Tile* >* transTiles;
	
	/// iterator for the std::vector above
	std::vector< Tile* >::iterator* tileIter;
	
	/// std::vector storing pointers to transient tiles
	std::vector< Tile* >* deletableTiles;
        /**
          The tiles referenced by these pointers are border tiles dynamically created in getNextMDD().
          They do not belong to any MDD object, and, therefore, they have to be deleted explicitly.
	*/

	/// bytes to transfer in actual tile (valid only if tile is larger than {\tt MAXTRANSBYTES})
	unsigned long bytesToTransfer;

        /// std::vector of persistent MDD collections in use
        std::vector< MDDColl* >* persMDDCollections;
	
        /// object representing the actual database
	DatabaseIf     database;

        /// object representing the actual transaction (only one at a time possible)
	TransactionIf  transaction;

        /// pointer to a timer for recording transaction time
        RMTimer* taTimer;      

        /// pointer to a timer for recording transfer time
        RMTimer* transferTimer;      
	
        /// pointer to a timer for recording evaluation time
        RMTimer* evaluationTimer;      
	
        /// parameter object
        r_Parse_Params *clientParams;

      private:
        /// empty private definition prevents of using the copy constructor
	ClientTblElt( const ClientTblElt& ){};
    };

    /// default constructor
    ServerComm();

    /// constructor getting the client time out and the time interval for management routines, together with listen port, rasmgr host and port and the server name
    ServerComm( unsigned long timeOut, unsigned long managementInterval , unsigned long listenPort, char* rasmgrHost, unsigned int rasmgrPort, char* serverName);  

    /// destructor
    virtual ~ServerComm();

    /// forces the server to listen for client calls
    virtual void startRpcServer() throw( r_Error );

    /// stops the server
    virtual void stopRpcServer();

    // informs RasMGR:
    void informRasMGR(int);
    #define SERVER_DOWN        0
    #define SERVER_AVAILABLE   1
    // 2 is server crushed, but it's generated by rasmgr!
    // regularly signal the rasmgr that we are available
    #define SERVER_REGULARSIG  3
    
    /// adds an entry to the client table (used in RasServerEntry)
    void addClientTblEntry( ClientTblElt *context ) throw ( r_Error );
    /**
      Adds the context entry passed to the client table.
      Throws an exception if context==NULL.
    */
    
    /// deletes an entry of the client table (must be public because it is used in the global garbage collection function)
    unsigned short deleteClientTblEntry( unsigned long ClientId );    
    /**
      Deletes the entry of the client table corresponding to the given client id.
      If no corresponding id is found, false is returned.     
    */
    
    // quick hack function used when stopping server to abort transaction and close db
    void abortEveryThingNow();
        
    /// print server status with client table content to {\tt s}
    virtual void printServerStatus( ostream& s=cout );

    /// get server status
    virtual void getServerStatus( ServerStatRes &returnStruct );

    /// the client table which holds information about the calling clients
    static std::list<ClientTblElt*>  clientTbl;

    /// last used client ID (this is increased by one to get the clientId for the next client)
    static unsigned long clientCount;

    /// inactivity timeout in seconds after which pending client data is deleted
    const unsigned long clientTimeout;

    /// do a garbage collection every {\tt garbageCollectionInterval} seconds (ONC RPC only)
    const unsigned long garbageCollectionInterval;    

    /// flag for active o2 transaction (stores the clientID of the owner of the active transaction, or 0 if none open)
    unsigned long transactionActive;

    /// memory used by malloc in ordinary blocks (set in dumpClientTable)
    long memUsed;

    /// stores a pointer to the actual servercomm object, only one can exist at a time
    static ServerComm* actual_servercomm; 

    /**
      the callback manager object for garbage collection, license check, ...
      Always instantiated with default size, resized on startup if required.
    */
    CallBackManager callback_mgr;

    //@Man: Communication methods
    //@{
    ///

      /// process the client's alive signal
      virtual unsigned short aliveSignal( unsigned long client );
      /**
        The method take the alive signal of a client and updates the last action time.
      
        Return values:
        \begin{tabular}{lll}
        0 && operation was successfull\\
        1 && client context not found\\
        \end{tabular}    
      */

      ///
      /// open database
      virtual unsigned short openDB( unsigned long callingClientId, const char* dbName, const char* userName );
      /**
        The method opens the database with {\tt dbName}. The return value means the following:
       
        \begin{tabular}{lll}
        0 && database successfully opened\\
        1 && client context not found\\
        2 && database does not exist\\
        3 && database is already open\\
        \end{tabular}
      */    
    
      /// close current database
      virtual unsigned short closeDB( unsigned long callingClientId );
      /**
        The return value has the following meaning:
        \begin{tabular}{lll}
        0 && operation was successful\\
        1 && client context not found\\
        \end{tabular}    
      */

      /// create a database
      virtual unsigned short createDB( char* name );

      /// destroy a database
      virtual unsigned short destroyDB( char* name );

      ///
      /// open transaction
      virtual unsigned short beginTA( unsigned long callingClientId, unsigned short readOnly=0 );
      /**
        The return value has the following meaning:
        \begin{tabular}{lll}
        0 && operation was successful\\
        1 && client context not found\\
        2 && other transaction already active\\
        \end{tabular}    
      */


      /// commit current transaction
      virtual unsigned short commitTA( unsigned long callingClientId );
      /**
        The return value has the following meaning:
        \begin{tabular}{lll}
        0 && operation was successful\\
        1 && client context not found\\
        \end{tabular}    
      */


      /// abort current transaction
      virtual unsigned short abortTA( unsigned long callingClientId );
      /**
        The return value has the following meaning:
        \begin{tabular}{lll}
        0 && operation was successful\\
        1 && client context not found\\
        \end{tabular}    
      */

      /// is transaction open currently?
      /**
        The return value has the following meaning:
        \begin{tabular}{lll}
        true && a transaction is open\\
        false && no transaction is open\\
        \end{tabular}    
      */
      
      virtual bool isTAOpen( unsigned long callingClientId );

      ///    
      /// executes a retrieval query and prepares the result for transfer with \Ref{getNextMDD}.
      virtual unsigned short executeQuery( unsigned long callingClientId, const char* query, ExecuteQueryRes &returnStructure );
      /**
        Executes a query and puts the result in the actual transfer collection.
        The first parameter is the unique client id
        for which the query should be executed. The second parameter is the
        query itself represented as a string.
      
        Return values
        \begin{tabular}{lll}
        0 && operation was successful - result collection holds MDD elements\\
        1 && operation was successful - result collection holds non-MDD elements\\
        2 && operation was successful - result collection has no elements\\
        3 && client context not found\\
        4 && parse errror\\
        5 && execution error\\
        \end{tabular}    

        Communication protocol (return value = 0)
        \begin{tabular}{lll}
        \Ref{executeQuery} && \\
        ->                 && \Ref{getNextMDD} \\
                           && ->               && \Ref{getNextTile} \\
                           &&                  && : \\
                           && :\\
        \Ref{endTransfer} \\
        \end{tabular}    

        Communication protocol (return value = 1)
        \begin{tabular}{lll}
        \Ref{executeQuery} && \\
        ->                 && \Ref{getNextElement} \\
                           && :\\
        \Ref{endTransfer} \\
        \end{tabular}    
      */
    
      ///
      /// get the domain of the next MDD of the actual transfer collection
      virtual unsigned short getNextMDD( unsigned long  callingClientId,
					 r_Minterval    &mddDomain,
					 char*          &typeName,
					 char*          &typeStructure,
					 r_OId          &oid,
					 unsigned short &currentFormat             );
      /**
        The Method gets the domain of the next MDD of the actual transfer collection. 
        The first parameter is the unique client id. The second parameter returns the
        domain of the MDD to be transfered. {\tt typeName} returns the name of the
        MDD type and its structure.
        Transfer of MDD data is tile-based using the method \Ref{getNextTile}.
      
        Return values:
        \begin{tabular}{lll}
        0 && operation was successful, at least one MDD is left in the transfer collection\\
        1 && nothing left in the transfer collection\\
        2 && client context not found, no tiles in the MDD object, no actual transfer collection \\
        \end{tabular}    
      */

      /// get the next scalar element in the actual transfer collection.
      virtual unsigned short getNextElement( unsigned long callingClientId,
					     char*         &buffer,
					     unsigned int  &bufferSize );
      /**
        The Method gets the next non-MDD element in the actual transfer collection.
        The first parameter is the unique client id. The second parameter returns a 
        pointer to the memory occupied by the next element and the third one delivers
        the size of the buffer.
      
        Return values:
        \begin{tabular}{lll}
        0 && operation was successful, at least one element is left in the transfer collection\\
        1 && operation succesful, nothing left in the transfer collection\\
        2 && client context not found, no tiles in the MDD object, no actual transfer collection \\
        \end{tabular}    
      */

      /// get an MDD by OId
      virtual unsigned short getMDDByOId( unsigned long  callingClientId,
					  r_OId          &oid,
					  r_Minterval    &mddDomain,
					  char*          &typeName,
					  char*          &typeStructure,
					  unsigned short &currentFormat );
      /**
        The Method gets an MDD by OId {\tt oid}. If the MDD is found, it is initialized as transfer 
        object and can be picked up by \Ref{getNextTile} calls (tile-based transfer).

        Additionally, the method returns domain, type name, and type structure of the found MDD
        object by reference parameters.

        Return values:
        \begin{tabular}{lll}
        0 && operation was successful\\
        1 && client context not found\\
        2 && object with this oid not found\\
        3 && object has no tiles
        \end{tabular}    

        Communication protocol
        \begin{tabular}{lll}
        \Ref{getMDDByOId} \\
        ->                && \Ref{getNextTile} \\
                          && : \\
        \Ref{endTransfer} \\
        \end{tabular}    
      */

      /// get next tile of the actual MDD of the actual transfer collection
      virtual unsigned short getNextTile( unsigned long callingClientId,
					  RPCMarray**   rpcMarray );
      /**
        The Method gets the next tile of the actual MDD of the actual transfer collection. 
        The first parameter is the unique client id. The second parameter is the
        RPC representation of the Marray representing the tile. If a tile is too large to be
        transferred in one piece, the data is split. To get the rest of the data, consecutively
        use this method.
      
        Return values:
        \begin{tabular}{lll}
        0 && operation was successful, no further MDDs are left\\
        1 && operation was successful, at least one MDD is left in the transfer collection\\
        2 && operation was successful, at least one tile is left in the actual MDD\\
        3 && operation was successful, at least one block is left in the actual tile\\
        4 && client context not found, no tiles in the MDD object, no actual transfer collection \\
          && or nothing left in the collection\\
        \end{tabular}

        Examples of valid return value chains:
        \begin{itemize}
        \item To be transferred: 1 MDD consisting of 1 tile (which is too large)\\
        \begin{verbatim}
        3 ->...-> 3 -> 0
        \end{verbatim}
        \item To be transferred: 1 MDD consisting of 2 tiles (the first is too large)\\
        \begin{verbatim}
        3 ->...-> 3 -> 2 -> 0
        |--------------|    |
            1st tile     2nd tile
        \end{verbatim}
        \item To be transferred: 2 MDDs, each consisting of 1 tile (none too large)\\
        \begin{verbatim}
        1 -> 0
        \end{verbatim}
        \item To be transferred: 3 MDDs, the first (A) consisting of 1 tile (not too large),\\
        the second (B) consisting of 2 tiles (B1, B2, of which the first is too large),
        the third (C) consisting of 2 tiles (C1, C2, of which the second is too large),
        \begin{verbatim}
        1 -> 3 ->...-> 3 -> 2 -> 1 -> 2 -> 3 ->...-> 3 -> 0
        |    |--------------|    |    |    |--------------|
        |           B1          B2    C1          C2
        |    |-------------------|    |-------------------|
        A              B                        C
        \end{verbatim}
        \end{itemize}
      */

      /// process the client's alive signal
      virtual unsigned short endTransfer( unsigned long client );
      /**
        The method terminates a transfer session and releases all transfer structures.
      
        Return values:
        \begin{tabular}{lll}
        0 && operation was successfull\\
        1 && client context not found\\
        \end{tabular}    
      */

      ///
      /// prepares transfer of MDD constants and execution of update query
      virtual unsigned short initExecuteUpdate( unsigned long callingClientId );
      /**
        Return values:
        \begin{tabular}{lll}
        0 && operation was successful\\
        1 && client context not found\\
        \end{tabular}    

        Communication protocol
        \begin{tabular}{lll}
        \Ref{initExecuteUpdate} && \\
        ->                      && \Ref{startInsertTransMDD} \\
                                && ->                        && \Ref{insertTile} \\
                                &&                           && :\\
                                && \Ref{endInsertMDD}\\
                                && :\\
        \Ref{executeUpdate}     && \\
        \end{tabular}    

        Note: Method \Ref{executeUpdate} can be invoked without the \Ref{initExecuteUpdate} 
              prolog in case of no constant MDD objects.
      */

      /// executes an update query
      virtual unsigned short executeUpdate( unsigned long callingClientId, const char* query, ExecuteUpdateRes &returnStructure );
      /**
        Executes an update query.
        The first parameter is the unique client id
        for which the query should be executed. The second parameter is the
        query itself represented as a string.
      
        Return values:
        \begin{tabular}{lll}
        0 && operation was successful\\
        1 && client context not found\\
        2 && parse errror\\
        3 && execution error\\
        \end{tabular}    
      */
    
      ///
      /// prepares an MDD (transient) for transfer of tiles
      virtual unsigned short startInsertTransMDD( unsigned long callingClientId, 
						  r_Minterval   &domain, 
						  unsigned long typeLength,
						  const char*   typeName );
      /**
        Creates an object for tile based transfer with method \Ref{insertTile}.

        The first parameter is the unique client id for which the MDD should be created.
        The second parameter is the
        name of the collection to insert the MDD object. The third parameter holds the
        spatial domain of the following MDD object and {\tt typeLength} specifies the size of
        the base type in bytes. The last one gives the type structure as string representation.
      
        Return values:
        \begin{tabular}{lll}
        0 && operation was successful\\
        1 && client context not found\\
        2 && MDD type name not found\\
        3 && MDD and its type are incompatible\\
        \end{tabular}    
      */

      /// create a new persistent MDD object for tile based transfers
      virtual unsigned short startInsertPersMDD( unsigned long callingClientId, 
						 const char*   collName,
						 r_Minterval&  domain, 
						 unsigned long typeLength,
						 const char*   typeName,
						 r_OId&        oid         );
      /**
        Creates an object for tile based transfer with method \Ref{insertTile} to be 
        inserted into the specified MDD collection.

        Parameters
        \begin{tabular}{lll}
        {\tt callingClientId}  && unique client id of the calling client\\
        {\tt collName}         && name of the collection to insert the MDD object\\
        {\tt domain}           && spatial domain\\
        {\tt typeLength}       && size of base type in bytes\\
        {\tt typeName}         && type structure as string representation\\
        {\tt oid}              && object identifier\\
        \end{tabular}    

        Return values
        \begin{tabular}{lll}
        0 && operation was successful\\
        1 && client context not found\\
        2 && MDD type name not found\\
        3 && types of MDD and collection are incompatible\\
        4 && MDD and its type are incompatible\\
        5 && collection does not exist\\
        6 && creation of persistent object failed\\
        \end{tabular}    

        Communication protocol
        \begin{tabular}{lll}
        \Ref{startInsertPersMDD} && \\
        ->                       && \Ref{insertTile} \\
                                 && :\\
        \Ref{endInsertMDD} && \\
        \end{tabular}    
      */

      /// insert a tile into a persistent MDD object
      virtual unsigned short insertTile( unsigned long callingClientId, 
					 int isPersistent,
					 RPCMarray* rpcMarray );
      /**
        Inserts a tile into the current MDD object.
      
        Parameters
        \begin{tabular}{lll}
        {\tt callingClientId}  && unique client id of the calling client\\
        {\tt isPersistent}     && determines wheather it is a persistent or a transient tile\\
        {\tt rpcMarray}        && RPC representation of the tile\\
        \end{tabular}    

        Return values:
        \begin{tabular}{lll}
        0 && operation was successful\\
        1 && client context not found\\
        2 && base type name of inserting tile is not supported\\
        \end{tabular}    
      */



      // inserts a tile into a persistent MDD object splitting it up according to 
      // parameter tileSize
      virtual unsigned short insertTileSplitted( unsigned long callingClientId, 
					 int isPersistent,
					 RPCMarray* rpcMarray,
                                         r_Minterval* tileSize );
      /**
        Splits and inserts a tile into the current MDD object.
      
        Parameters
        \begin{tabular}{lll}
        {\tt callingClientId}  && unique client id of the calling client\\
        {\tt isPersistent}     && determines wheather it is a persistent or a transient tile\\
        {\tt rpcMarray}        && RPC representation of the tile\\
        {\tt tileSize}         && r_Minterval specifying the tile-size\\
        \end{tabular}    

        Return values:
        \begin{tabular}{lll}
        0 && operation was successful\\
        1 && client context not found\\
        2 && base type name of inserting tile is not supported\\
        \end{tabular}    
      */

      /// finnishes the MDD creation and inserts the MDD into the collection
      virtual unsigned short endInsertMDD( unsigned long callingClientId, 
					   int isPersistent );
      /**
        Parameters
        \begin{tabular}{lll}
        {\tt callingClientId}  && unique client id of the calling client\\
        {\tt isPersistent}     && determines wheather it is a persistent or a transient MDD\\
        \end{tabular}            
      */

      ///
      /// insert object into collection    
      virtual unsigned short insertMDD( unsigned long callingClientId,
					const char* collName,
					RPCMarray *rpcMarray, 
					const char* typeName, r_OId& oid );
      /**
        Inserts an object into an MDD collection. It is transfered in one piece.

        Parameters
        \begin{tabular}{lll}
        {\tt callingClientId}  && unique client id of the calling client\\
        {\tt collName}         && name of the collection to insert the MDD object\\
        {\tt rpcMarray}        && RPC representation of the MDD object\\
        {\tt typeName}         && type structure as string representation\\
        {\tt oid}              && object identifier\\
        \end{tabular}    

        Return values:
        \begin{tabular}{lll}
        0 && operation was successful\\
        1 && client context not found\\
        2 && MDD type name not found\\
        3 && types of MDD and collection are incompatible\\
        4 && MDD and its type are incompatible\\
        5 && collection does not exist\\
        6 && creation of persistent object failed\\
        \end{tabular}    
      */

      ///
      /// prepare an MDD collection for transfer with getNextMDD()
      virtual unsigned short getCollByName( unsigned long callingClientId,
					    const char*   collName,
					    char*         &typeName,
					    char*         &typeStructure,
					    r_OId         &oid             );
      /**
        ATTENTION: This function is not used at the moment. It hast
        to be adapted to transferData.

        Prepares an MDD collection for transfer with getNextMDD().

        Parameters
        \begin{tabular}{lll}
        {\tt callingClientId}  && unique client id of the calling client\\
        {\tt collName}         && name of the collection to be got\\
        {\tt typeName}         && returns name of the collection type\\
        {\tt typeStructure}    && returns structure of the collection type\\
        {\tt oid}              && returns oid of the collection\\
        \end{tabular}    

        The first parameter is the unique client id. The second parameter is the
        name of the collection to get. {\tt typeName} returns the name of the 
        collection type and {\tt typeStructure} its type structure.
      
        Return values:
        \begin{tabular}{lll}
        0 && operation was successful - collection has some elements\\
        1 && operation was successful - collection has no elements\\
        2 && collection is not known\\
        3 && client context not found\\
        \end{tabular}    
      */

      /// prepare an MDD collection for transfer with getNextMDD()
      virtual unsigned short getCollByOId( unsigned long callingClientId,
					   r_OId         &oid,
					   char*         &typeName,
					   char*         &typeStructure,
					   char*         &collName       );
      /**
        ATTENTION: This function is not used at the moment. It hast
        to be adapted to transferData.

        Prepares an MDD collection for transfer with \Ref{getNextMDD}.

        Parameters
        \begin{tabular}{lll}
        {\tt callingClientId}  && unique client id of the calling client\\
        {\tt oid}              && oid of the collection to be got\\
        {\tt typeName}         && returns name of the collection type\\
        {\tt typeStructure}    && returns structure of the collection type\\
        {\tt collName}         && returns name of collection\\
        \end{tabular}    

        Return values:
        \begin{tabular}{lll}
        0 && operation was successful - collection has some elements\\
        1 && operation was successful - collection has no elements\\
        2 && collection is not known\\
        3 && client context not found\\
        \end{tabular}    
      */

      /// gets oids of the collection specified by name
      virtual unsigned short getCollOIdsByName( unsigned long callingClientId,
						const char*   collName,
						char*         &typeName,
						char*         &typeStructure,
						r_OId         &oid,
						RPCOIdEntry*  &oidTable,
						unsigned int  &oidTableSize     );
      /**
        Gets the collection of oids of the collection with {\tt collName}.

        Parameters
        \begin{tabular}{lll}
        {\tt callingClientId}  && unique client id of the calling client\\
        {\tt collName}         && name of the collection to be got\\
        {\tt typeName}         && returns name of the collection type\\
        {\tt typeStructure}    && returns structure of the collection type\\
        {\tt oid}              && returns object identifier\\
        {\tt oidTable}         && returns an array of pointers to oids\\
        {\tt oidTableSize}     && returns the no of elements in the table\\
        \end{tabular}    

        Return values:
        \begin{tabular}{lll}
        0 && operation was successful - collection has some elements\\
        1 && operation was successful - collection has no elements\\
        2 && collection is not known\\
        3 && client context not found\\
        \end{tabular}    
      */

      /// gets oids of the collection specified by name
      virtual unsigned short getCollOIdsByOId( unsigned long callingClientId,
					       r_OId         &oid,
					       char*         &typeName,
					       char*         &typeStructure,
					       RPCOIdEntry*  &oidTable,
					       unsigned int  &oidTableSize,
					       char*         &collName       );
      /**
        Gets the collection of oids of the collection with {\tt collName}.

        Parameters
        \begin{tabular}{lll}
        {\tt callingClientId}  && unique client id of the calling client\\
        {\tt oid}              && oid of the collection to be got\\
        {\tt typeName}         && returns name of the collection type\\
        {\tt typeStructure}    && returns structure of the collection type\\
        {\tt oidTable}         && returns an array of pointers to oids\\
        {\tt oidTableSize}     && returns the no of elements in the table\\
        {\tt collName}         && returns name of collection\\
        \end{tabular}    

        Return values:
        \begin{tabular}{lll}
        0 && operation was successful - collection has some elements\\
        1 && operation was successful - collection has no elements\\
        2 && collection is not known\\
        3 && client context not found\\
        \end{tabular}    
      */

      ///   
      /// create new MDD collection
      virtual unsigned short insertColl( unsigned long callingClientId, 
					 const char* collName,
					 const char* typeName,
					 r_OId&      oid       );
      /**
        Creates a new MDD collection.

        Parameters
        \begin{tabular}{lll}
        {\tt callingClientId}  && unique client id of the calling client\\
        {\tt collName}         && name of the collection to be created\\
        {\tt typeName}         && name of the collection type\\
        {\tt oid}              && object identifier\\
        \end{tabular}    

        Return values:
        \begin{tabular}{lll}
        0 && operation was successful\\
        1 && client context not found\\
        2 && collection type name not found\\
        3 && collection name exists already in the db
        \end{tabular}    
      */
  
      ///    
      /// delete MDD collection
      virtual unsigned short deleteCollByName( unsigned long callingClientId, 
					       const char* collName );
      /**
        Deletes an MDD collection. The first parameter is the unique client id
        for which the collection should be deleted. The second parameter is the
        name for the collection to be deleted.
      
        Return values:
        \begin{tabular}{lll}
        0 && operation was successful\\
        1 && client context not found\\
        2 && collection with name does not exist\\
        \end{tabular}    
      */
    
      /// delete object by oid
      virtual unsigned short deleteObjByOId( unsigned long callingClientId, r_OId& oid );
      /**
        Deletes the object with {\tt oid}.
        The first parameter is the unique client id for which the object should be
        deleted.
      
        Return values:
        \begin{tabular}{lll}
        0 && operation was successful\\
        1 && client context not found\\
        2 && object with oid does not exist\\
        \end{tabular}    
      */
    
      ///    
      /// remove object specified by oid from collection specified by name
      virtual unsigned short removeObjFromColl( unsigned long callingClientId, 
						const char* collName, r_OId& oid );
      /**
        The method removes the object with {\\t oid} from collection with {\tt collName}.
        The first parameter is the unique client id for which the object should be removed.
      
        Return values:
        \begin{tabular}{lll}
        0 && operation was successful\\
        1 && client context not found\\
        2 && specified collection does not exist\\
        3 && specified object does not exist in the collection\\
        \end{tabular}    
      */

      ///    
      /// get new object identifier
      virtual unsigned short getNewOId( unsigned long callingClientId,
					unsigned short objType, r_OId& oid );
      /**
        Creates a new oid and gives it back by the refernce parameter {\tt oid}.
        {\tt objType} determines the type of object for which that oid is allocated. The folowing
        values are supported: 1 = MDD,  2 = Collection.
      
        Return values:
        \begin{tabular}{lll}
        0 && operation was successful\\
        1 && client context not found\\
        2 && error while creating oid\\
        \end{tabular}    
      */

      /// get type of object by oid
      virtual unsigned short getObjectType( unsigned long callingClientId, 
					    r_OId& oid, unsigned short &objType );
      /**
        Determines the type of the object indicated by {\tt oid}. The type is returned by the 
        reference parameter {\tt objType}. The folowing types are supported: 1 = MDD,  2 = Collection.
      
        Return values:
        \begin{tabular}{lll}
        0 && operation was successful\\
        1 && client context not found\\
        2 && oid not found\\
        \end{tabular}    
      */

      /// get type structure of a type name
      virtual unsigned short getTypeStructure( unsigned long  callingClientId, 
					       const char* typeName,
					       unsigned short typeType, 
					       char* &typeStructure);
      /**
        Determines the type structure of the type specified by {\tt typeName}. The type 
	either can be a set type (typeType=1), an mdd type (typeType=2), or a base type 
	(typeType = 3).
      
        Return values:
        \begin{tabular}{lll}
        0 && operation was successful\\
        1 && client context not found\\
        2 && type name not found\\
        \end{tabular}    
      */

      /// set the data format used for transferring data to the client
      virtual unsigned short setTransferMode( unsigned long callingClientId,
					      unsigned short format, const char* formatParams );
      /**
	Sets the data format used by the server to transfer data to the client to
	format which is of type r_Data_Format.

	Return values:
	\begin{tabular}{lll}
	0 && operation was successful\\
	1 && client context not found\\
	2 && unknown or unsupported data format\\
	\end{tabular}
      */

      /// set the data format for storing data into the database
      virtual unsigned short setStorageMode( unsigned long callingClientId,
					     unsigned short format, const char *formatParams );
      /**
	 return values exactly like setTransferMode()
      */
    ///
    //@}    
    
    /// returns a pointer to the context of the calling client, 0 it there is no context
    virtual ClientTblElt* getClientContext( unsigned long ClientId );
    /**
      Returns a pointer to the context of the calling client. This is done by
      searching the client table maintained by the server for the given client id.
      If there is no context corresponding to the client id, 0 is returned.

      Attention: After a client context was successfully received it has to be 
                 released using its member function release();
    */
   
    // get, set and clear extended error info.
    const char *getExtendedErrorInfo();

    void setExtendedErrorInfo(const char*);
  
    void clearExtendedErrorInfo(); 
    
    void clientEndRequest();
   
    // constant for clientID
    static const char* HTTPCLIENT;   

  private:
    /// copy constructor is private and therefore can not be used
    ServerComm( const ServerComm& );//and then why this? : clientTimeout(3600), garbageCollectionInterval(600){;};

  protected:
    /// make sure a tile has the correct data format, converting if necessary
    static int ensureTileFormat( r_Data_Format &hasFmt, r_Data_Format needFmt,
				 const r_Minterval &dom, const BaseType *type,
				 char *&data, unsigned long &size, int repack,
				 int owner, const char *params = NULL);
    ///returns the following:
    static const int ENSURE_TILE_FORMAT_OK;
    static const int ENSURE_TILE_FORMAT_BAD;
				   
    /// pointer to the actual administration interface object
    AdminIf* admin;
    
    char *errorText;

    unsigned long listenPort;
    char*         rasmgrHost;
    unsigned int  rasmgrPort;
    char*         serverName;
    
    bool          isHttpServer;
    //this trick didn't work and we are out of time
   // ClientTblElt  *uniqueClientContext;
};

    
/// indirect caller for rpcif_1
void rpcif_1_caller(struct svc_req *rqstp,SVCXPRT *transp);


/******************************************************************************************
***  This class shouldn't be here, later it will be put in its own file
******************************************************************************************/


class AccessControl
   {
     public:
       AccessControl();
       ~AccessControl();
       void initSyncro(const char *);
       void setServerName(const char *serverName);

       void resetForNewClient();
       int  crunchCapability(const char*); 
       /*   0 - ok
          804 - capability refused
       */
       
       void wantToRead(); // both throw
       void wantToWrite();
       bool isClient();
     private:
       int messageDigest(const char *input,char *output,const char *mdName);
       double initDeltaT;
       char serverName[100];
       
       bool okToRead;
       bool okToWrite;
       bool weHaveClient;
    };
extern AccessControl accessControl;       
   
#include "servercomm.icc"

#endif