summaryrefslogtreecommitdiffstats
path: root/rnprotocol/rnpclientcomm.hh
blob: 87f7dba353487ae7e43320ee887e7fa1e5e1b9bf (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
#ifndef RNPCLIENTCOMM_HH
#define RNPCLIENTCOMM_HH
/*
* 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>.
*/
/*************************************************************
 *
 *
 * COMMENTS:
 *
 ************************************************************/

#include "clientcomm/clientcomm.hh"
#include "rnprotocol/rnpcommunication.hh"
#include "rnprotocol/rnprasserver.hh"
#include "clientcomm/rpcif.h"

using namespace rnp;

/**
  * \defgroup Rnprotocols Rnprotocol Classes
  */

/**
  * \ingroup Rnprotocols
  */
 
class RnpClientComm : public ClientComm, RnpBaseClientComm
  {
    public:
      /// constructor getting the host name of the rasmgr host and it's listening port (default 7001).
      RnpClientComm( const char* rasmgrHost, int rasmgrPort = RASMGRPORT ) throw( r_Error );

      ~RnpClientComm() throw ();
      
      bool effectivTypeIsRNP() throw();

    //@Man: Database methods
    //@{
    ///
       
      /// open database
      int openDB( const char* database );
      /// close current database
      int closeDB();
      /// create a database
      int createDB( const char* name ) throw(r_Error);
      /// destroy a database
      int destroyDB( const char* name ) throw(r_Error);

    ///
    //@}
    
    //@Man: Transaction methods
    //@{
    ///

      /// begin transaction
      int openTA( unsigned short readOnly = 0 ) throw(r_Error);
      /// commit current transaction
      int commitTA() throw(r_Error);
      /// abort current transaction
      int abortTA();

    ///
    //@}
    
    //@Man: MDD methods
    //@{
    ///

      /// inserts a MDD object in an existing MDD collection on the server
      void insertMDD( const char* collName, r_GMarray* mar ) throw( r_Error );
      /// gets MDD object by oid
      r_Ref_Any getMDDByOId( const r_OId& oid ) throw( r_Error );

    ///
    //@}
    
    //@Man: Collection methods
    //@{
    ///

      /// creates an empty MDD collection on the server
      void insertColl( const char* collName, const char* typeName, const r_OId& oid ) throw( r_Error );
      /// deletes an MDD collection by name
      void deleteCollByName( const char* collName ) throw( r_Error );
      /// deletes an object by oid (right now, objects are collection only)
      void deleteObjByOId( const r_OId& oid ) throw( r_Error );
      /// removes an object from a collection
      void removeObjFromColl( const char* name, const r_OId& oid ) throw ( r_Error );
      /// gets collection by name
      r_Ref_Any getCollByName( const char* name ) throw( r_Error );
      /// gets collection by oid
      r_Ref_Any getCollByOId ( const r_OId& oid ) throw( r_Error );
      /// gets collection references by name
      r_Ref_Any getCollOIdsByName( const char* name ) throw( r_Error );
      /// gets collection references by oid
      r_Ref_Any getCollOIdsByOId ( const r_OId& oid ) throw( r_Error );

    ///
    //@}

    //@Man: Query methods
    //@{
    ///

      /// query execution
      void executeQuery( const r_OQL_Query& query, r_Set< r_Ref_Any >& result ) throw( r_Error );
      /*@Doc:
        Executes a retrieval query of type \Ref{r_OQL_Query} and returns the result. Every
        MDD object of the MDD collection is fetched from the server and inserted
        in the resulting \Ref{r_Set}.
      */
    
      /// update execution
      void executeQuery( const r_OQL_Query& query ) throw( r_Error );
      /*@Doc:
        Executes an update query of type \Ref{r_OQL_Query}. 
      */

    ///
    //@}


    //@Man: System methods
    //@{
    ///

      /// get new oid    
      r_OId getNewOId( unsigned short objType ) throw(r_Error);

      /// get oid type
      unsigned short getObjectType( const r_OId& oid ) throw(r_Error);

      /// get type structure
      /// dallocate using delete []
      char* getTypeStructure( const char* typeName, r_Type_Type typeType ) throw(r_Error);

    ///
    //@}

    /// provides read access to my clientID
    unsigned long getClientID() const;			

    /// set the preferred transfer format
    int setTransferFormat( r_Data_Format format, const char* formatParams=NULL );

    /// set the preferred storage format
    int setStorageFormat( r_Data_Format format, const char *formatParams=NULL );
    
    
    /// get real server name (the dinamic one, assigned by the RasMGR)
    const char* getServerName();
    
    /// user identification for RasMGR
    void setUserIdentification(const char *userName, const char *plainTextPassword);

    /// set maximum retry to get a server
    void setMaxRetry(unsigned int newMaxRetry);
    
    /// get maximum retry to get a server
    unsigned int  getMaxRetry();

    void setTurbo(bool turbo);
    
    ///
    //@}
	
	// obsolete, but kept because of virtual functions in base class
    void triggerAliveSignal();
    void sendAliveSignal();
    const char *getExtendedErrorInfo() throw(r_Error);
    
    void setTimeoutInterval(int seconds);
    int  getTimeoutInterval();

//#### secret, unofficial functions ###########
    
    r_OId  createCollection(const char *collName, const char *collTypeName) throw(r_Error);
    
    r_OId  createMDD(const char* collName, const char* mddTypeName, const char* definitionDomain, const char *tileDomain, bool rcindex = false) throw(r_Error);
   
    void   extendMDD(r_OId mddOId, const char *stripeDomain, const char* tileDomain) throw(r_Error);

    vector<r_OId> getOIdsFromCollection( const char* name ) throw( r_Error ); 
   
    vector<r_OId> getOIdsFromCollection( const r_OId& oid ) throw( r_Error );
    
    vector<r_Minterval> getTileDomains(r_OId mddOId, const char *stripeDomain) throw( r_Error );

    
    void preloadTiles(r_OId mddOId, const char *tileDomain) throw(r_Error);
    
    int  getTileData(r_OId mddOId, const char *tileDomain, char *&data, bool preallocated = false) throw(r_Error);
    
    void replaceTileData(r_OId mddOId, const char *tileDomain, const char *newData, int dataLength, const char *alfaData, int alfaLength) throw(r_Error);

//#############################################
  private:
    /// client ID assigned to me by the server
    int clientID;

    /// the name of the rasmgr host
    char *rasmgrHost;
    
    /// the listening port of the rasmgr 
    int  rasmgrPort;
    
    /// the name of the server host
    char serverHost[100]; //can't be just a pointer, it's never stored elsewhere
    
    int  serverPort;
        
    // the name of the opened database, needed because it will be opened again and again, in a hidden operation
    char databaseName[100]; 
    
    // the capability 
    char capability[100]; 
    
    /// user identification string
    char identificationString[100];
    
    /// requests a free server from the rasmgr, retrying maxRetry times
    int getFreeServer(bool readwrite, bool openDB);
    
    /// requests a free server from the rasmgr
    int executeGetFreeServer(bool readwrite, bool openDB);
        
	int readWholeMessage(int socket,char *destBuffer,int buffSize);
        
	int writeWholeMessage(int socket,char *destBuffer,int buffSize);
    
    // MD5 of password
    int messageDigest(const char *input,char *output,const char *mdName);
        
    /// internal function for client/server protocol handling of non-MDD collection transfer
    void getElementCollection( r_Set< r_Ref_Any >& result ) throw(r_Error);
    
    /// internal function for client/server protocol handling of MDD collection transfer
    void getMDDCollection( r_Set< r_Ref_Any >& result, unsigned int isQuery ) throw(r_Error);
    
    /// internal function for reading an MDD from the database
    unsigned short getMDDCore( r_Ref<r_GMarray> &mdd, GetMDDRes *thisResult, unsigned int isQuery ) throw( r_Error );
    
    /// concatenate data to an array, making sure there are no overflows (used by getMDDCore())
    int concatArrayData( const char *source, unsigned long srcSize, char *&dest,
			 unsigned long &destSize, unsigned long &destLevel );
    
    /// do transfer decompression
    r_Data_Format doTransferDecompression( r_GMarray *tile, const r_Base_Type *type,
					   r_Data_Format fmt, unsigned long size );
    
        /// internal function for converting a \Ref{r_GMarray} into its RPC representation
    void getMarRpcRepresentation( const r_GMarray* mar, RPCMarray*& rpcMarray,
                                  r_Data_Format initStorageFormat = r_Array,
				  const r_Base_Type *bt = NULL);

    /// internal function for freeing data allocated by getMarRpcRepresentation()
    void freeMarRpcRepresentation( const r_GMarray* mar, RPCMarray* rpcMarray );
    
    /// endianness of client and server (0 means big endian)
    int endianServer;
    int endianClient;

    /// data format for transfer compression
    r_Data_Format transferFormat;
    /// storage format for inserting new tiles
    r_Data_Format storageFormat;
    /// transfer format parameters
    char* transferFormatParams;
    /// storage format parameters
    char *storageFormatParams;
    /// parameter object for configuration
    r_Parse_Params *clientParams;

    /// policy is compress-on-server
    int serverCompresses;
    /// policy is exact
    int exactFormat;
    
    // functions which really do the connection stuff
    void executeConnect();
    void executeDisconnect();
    void executeOpenDB(const char*);
    void executeCloseDB();
    void executeBeginTA(bool rw);
    void executeCommitTA();
    void executeAbortTA();
    int  executeExecuteQuery( const char* query, r_Set< r_Ref_Any >& result ) throw( r_Error );
    GetElementRes* executeGetNextElement();
    int  executeEndTransfer();
    GetMDDRes*  executeGetNextMDD();
    GetTileRes* executeGetNextTile();
    void executeExecuteUpdateQuery(const char *query) throw(r_Error);
    int  executeStartInsertTransMDD(r_GMarray* mdd);
    int  executeInsertTile(bool persistent, RPCMarray* tile);
    void executeEndInsertMDD(bool persistent);
    int  executeInitUpdate();
    int  executeStartInsertPersMDD( const char* collName, r_GMarray* mar );
    int  executeInsertMDD(const char* collName, r_GMarray* mar, RPCMarray *rpcMarray); 
    int  executeInsertCollection( const char* collName, const char* typeName, const r_OId& oid );
    r_Ref_Any executeGetCollByNameOrOId ( const char* collName, const r_OId& oid ) throw( r_Error );
    r_Ref_Any executeGetCollOIdsByNameOrOId ( const char* collName, const r_OId& oid ) throw( r_Error );
    int   executeSetFormat( bool transferFormat, r_Data_Format format, const char* formatParams);
    r_OId executeGetNewOId( unsigned short objType ) throw(r_Error);
    unsigned short executeGetObjectType( const r_OId& oid ) throw(r_Error);
    char* executeGetTypeStructure( const char* typeName, r_Type_Type typeType ) throw(r_Error);

    vector<r_OId> executeGetOIdsFromCollection ( const char* collName, const r_OId& oid ) throw( r_Error );

    void turboOpenDB(const char*);
    void turboBeginTA(bool rw);
    void turboCommitTA();
    void turboAbortTA();
    
    bool useTurbo;
    
    // returns only if transaction is open and rw, otherwise throws
    void checkForRwTransaction() throw (r_Error);
    
    // varianta locala
    void sendRequestGetAnswer() throw (r_Error);
    
    int sendAndReturnStatus() throw (r_Error);    
    
    bool detectErrors();
    // doesn't return if there is an error
    void reassemble_r_Error() throw (r_Error);
    
    // a very internal helper for some functions
    void helper012d(const char* caller) throw (r_Error);
   };

#endif