File: ArClientFileUtils.h

package info (click to toggle)
libaria 2.8.0%2Brepack-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 13,628 kB
  • ctags: 16,574
  • sloc: cpp: 135,490; makefile: 925; python: 597; java: 570; ansic: 182
file content (367 lines) | stat: -rw-r--r-- 14,422 bytes parent folder | download | duplicates (2)
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
#ifndef ARCLIENTFILEUTILS_H
#define ARCLIENTFILEUTILS_H

#include "Aria.h"
#include "ArClientBase.h"

/// The item type that the ArCLientFileLister gets back
class ArClientFileListerItem
{
public:
  /// Constructor
  ArClientFileListerItem(const char *name, time_t atime, time_t mtime,
			 ArTypes::UByte4 size)
    { myName = name; myATime = atime; myMTime = mtime; mySize = size; }
  /// Copy Constructor
  ArClientFileListerItem(const ArClientFileListerItem &item)
    { myName = item.myName; myATime = item.myATime; myMTime = item.myMTime; 
    mySize = item.mySize; }
  /// Assignment operator
  AREXPORT ArClientFileListerItem &operator=(const 
					     ArClientFileListerItem &item)
    {
      if (this != &item) 
      {
	myName = item.myName; myATime = item.myATime; myMTime = item.myMTime; 
	mySize = item.mySize; 
      }
      return *this;
    }
    /// Destructor
  virtual ~ArClientFileListerItem() {}
  /// Gets the name of the list item
  const char *getName(void) const { return myName.c_str(); }
  /// Gets the time this list item was last accessed in seconds from 1970
  time_t getLastAccessedTime(void) const { return myATime; }
  /// Gets the time this list item was last modified in seconds from 1970
  time_t getLastModifiedTime(void) const { return myMTime; }
  /// Gets the size of this list item in bytes
  ArTypes::UByte4 getSize(void) const { return mySize; }
protected:
  std::string myName;
  time_t myATime;
  time_t myMTime;
  ArTypes::UByte4 mySize; 
};

/// Class for getting file list information from the server
/**
   This class will interact with the ArServerFileLister and find out
   whats in directories and change directories and such.  This class
   is all that should be used to get the information from
   ArServerFileLister, the API between the two is fairly volatile and
   will remain so.  If you need more functionality let us know and
   we'll add it if its reasonable.
   
   When you change directories or change to the top dir it doesn't
   happen right away, but when it happens the update callbacks will be
   called, 0 as the int for the callback means everything is good,
   positive error messages are from the server (1 == tried to go
   outside allowed area, 2 == no such directory), negative are from
   this class (-1 == got directory but it wasn't what we wanted (if
   you wait the right one might come in, like if someone selects one
   dir then the other)).
**/
class ArClientFileLister
{
public:
  /// Constructor
  AREXPORT ArClientFileLister(ArClientBase *client);
  /// Destructor
  AREXPORT virtual ~ArClientFileLister();
  /// Sees if the server supports what this class needs
  AREXPORT bool isAvailable(void);      
  /// Goes to the top directory
  AREXPORT void changeToTopDir(void);
  /// Goes to this directory in the current directory
  AREXPORT void changeToDir(const char *dir);
  /// Goes up one directory from current directory
  AREXPORT void upOneDir(void);
  /// Goes to this absolute directory
  AREXPORT void changeToAbsDir(const char *dir);
  /// Gets the name of the directory we're in
  AREXPORT const char *getCurrentDir(void) const;
  /// Gets the name of the directory that we're currently waiting for
  AREXPORT const char *getWaitingForDir(void) const;

  /// Gets the directories in the current directory
  AREXPORT std::list<ArClientFileListerItem> getDirectories(void) const;
  /// Gets the files in the current directory
  AREXPORT std::list<ArClientFileListerItem> getFiles(void) const;
  /// Adds a callback for when we get the desired directory info 
  AREXPORT void addUpdatedCallback(ArFunctor1<int> *functor, 
				   ArListPos::Pos position = ArListPos::LAST);
  /// Removes a callback for when we get the desired directory info 
  AREXPORT void remUpdatedCallback(ArFunctor1<int> *functor);
  /// Gets the last time we were updated
  AREXPORT ArTime getLastUpdated(void);
  /// Gets the last time we requested an update
  AREXPORT ArTime getLastRequested(void);
  /// Logs the current directory
  AREXPORT void log(bool withTimes);
protected:
  AREXPORT void netGetDirListing(ArNetPacket *packet);
  AREXPORT void netGetDirListingMultiplePackets(ArNetPacket *packet);
  AREXPORT void callUpdatedCallbacks(int val);
  AREXPORT void logList(
	  std::list<ArClientFileListerItem> *logThis,
	  bool withTimes);
  void getDirListing(const char *dir);
  ArMutex myDataMutex;
  ArMutex myCallbackMutex;
  ArClientBase *myClient;
  char myCurrentDir[2048];
  char myWaitingForDir[2048];
  bool myLastDirMatched;
  bool myNewDirListing;
  ArTime myLastRequested;
  ArTime myLastUpdated;
  std::list<ArClientFileListerItem> myDirectories;
  std::list<ArClientFileListerItem> myFiles;
  std::list<ArFunctor1<int> *> myUpdatedCallbacks;
  ArFunctor1C<ArClientFileLister, ArNetPacket *> myGetDirListingCB;
  ArFunctor1C<ArClientFileLister, 
	      ArNetPacket *> myGetDirListingMultiplePacketsCB;
};


/// Class for getting files from the server
/**
   This class will interact with the ArServerFileToClient and get a
   file on the server.  If you want to find out what files are on the
   server use ArClientFileLister.

   When get a file it doesn't happen right away, but when the file is
   received (or failes) the fileGotten callbacks will be called, 0 as
   the int for the callback means everything is good, positive error
   messages are from the server (1 == tried to go outside allowed
   area, 2 == no such directory, 3 == empty file name, 4 == problem
   reading file), negative are from this class (-1 == got directory
   but it wasn't what we wanted (if you wait the right one might come
   in, like if someone selects one dir then the other), -2 == can't
   open file to put result into).
**/
class ArClientFileToClient
{
public:
  /// Constructor
  AREXPORT ArClientFileToClient(ArClientBase *client);
  /// Destructor
  AREXPORT virtual ~ArClientFileToClient();
  /// Sees if the server supports what this class needs
  AREXPORT bool isAvailable(void);  

  AREXPORT bool isAvailableSetTimestamp(void);

  /// Get the file from a directory 
  AREXPORT bool getFileFromDirectory(const char *directory, 
                                     const char *fileName, 
                                     const char *clientFileName,
                                     bool isSetTimestamp = false);
  /// Cancels getting a file
  AREXPORT void cancelGet(void);
  /// If we're getting a file now
  AREXPORT bool isWaitingForFile(void);
  /// Gets the directory we're getting from
  AREXPORT const char *getDirectory(void);
  /// Gets the filename we're getting
  AREXPORT const char *getFileName(void);
  /// Gets the filename we'll save the gotten file in
  AREXPORT const char *getClientFileName(void);
  /// Adds a callback for when we get the desired file (or fail)
  AREXPORT void addFileReceivedCallback(ArFunctor1<int> *functor, 
				   ArListPos::Pos position = ArListPos::LAST);
  /// Removes a callback for when we get the desired file (or fail)
  AREXPORT void remFileReceivedCallback(ArFunctor1<int> *functor);

  
  /// Gets the last time we finished getting a file
  AREXPORT ArTime getLastReceived(void);
  /// Gets the last time we asked for a file
  AREXPORT ArTime getLastRequested(void);

protected:

  AREXPORT void netGetFile(ArNetPacket *packet);
  AREXPORT void netGetFileWithTimestamp(ArNetPacket *packet);
  AREXPORT void doGetFile(ArNetPacket *packet,
                          bool isSetTimestamp);
  AREXPORT void callFileReceivedCallbacks(int val);


protected:

  ArMutex myDataMutex;
  ArMutex myCallbackMutex;
  ArClientBase *myClient;
  bool myIsWaitingForFile;
  std::string myDirectory;
  std::string myFileName;
  std::string myWholeFileName;
  std::string myClientFileName;
  FILE *myFile;

  ArTime myLastRequested;
  ArTime myLastReceived;

  std::list<ArFunctor1<int> *> myFileReceivedCallbacks;

  ArFunctor1C<ArClientFileToClient, ArNetPacket *> myGetFileCB;
  ArFunctor1C<ArClientFileToClient, ArNetPacket *> myGetFileWithTimestampCB;
};

/// Class for putting files to the server
/**
   This class will interact with the ArServerFileFromClient and put a
   file on to the server.  If you want to find out what files are on
   the server use ArClientFileLister.

   When get a file it doesn't happen right away, but when the file is
   received (or failes) the fileGotten callbacks will be called, 0 as
   the int for the callback means everything is good, positive error
   messages are from the server (0 = good (got file), 1 = getting
   file, 2 = tried to go outside allowed area, 3 = bad directory, 4 =
   empty file name (or other problem with fileName), 5 = can't write
   temp file, 6 = error moving file from temp to perm, 7 = another
   client putting file, 8 = timeout (no activity for 15 seconds) and
   another client wanted to put the file, 9 = client adding to,
   finishing, or canceling a file the server doesn't have), negative
   would be from this class but there aren't any of those yet
**/
class ArClientFileFromClient
{
public:
  /// Constructor
  AREXPORT ArClientFileFromClient(ArClientBase *client);
  /// Destructor
  AREXPORT virtual ~ArClientFileFromClient();
  /// Sees if the server supports what this class needs
  AREXPORT bool isAvailable(void);      
  /// Sees if the server supports what this class needs to send slowly
  AREXPORT bool isAvailableSlow(void);      
  /// Sees if the server supports what this class needs to send fast
  AREXPORT bool isAvailableFast(void);      
  /// Sees if the server supports the ability to set the file timestamp
  AREXPORT bool isAvailableSetTimestamp(void);

  /// Enum that describes the speed to send at
  enum SendSpeed 
  {
    SPEED_AUTO, ///< Send it fast if available, if not then send it slow
    SPEED_FAST, ///< Send it fast
    SPEED_SLOW ///< Send it slow
  };
  /// Puts the specified client file on the server in the given directory and file
  AREXPORT bool putFileToDirectory(const char *directory, 
 				                           const char *fileName, 
				                           const char *clientFileName,
				                           SendSpeed sendSpeed = SPEED_AUTO, 
                                   bool isSetTimestamp = false);
  /// Cancels putting a file
  AREXPORT void cancelPut(void);

  /// If we're waiting for completion now
  AREXPORT bool isWaitingForReturn(void);
  /// Gets the directory we're putting to
  AREXPORT const char *getDirectory(void);
  /// Gets the filename we're putting
  AREXPORT const char *getFileName(void);
  /// Gets the filename we're taking from the client
  AREXPORT const char *getClientFileName(void);
  /// Adds a callback for when we get the desired file (or fail)
  AREXPORT void addFileSentCallback(ArFunctor1<int> *functor, 
				   ArListPos::Pos position = ArListPos::LAST);
  /// Removes a callback for when we get the desired file (or fail)
  AREXPORT void remFileSentCallback(ArFunctor1<int> *functor);
  /// Gets the last time we finished putting a file
  AREXPORT ArTime getLastCompletedSend(void);
  /// Gets the last time we started putting a file
  AREXPORT ArTime getLastStartedSend(void);
protected:
  AREXPORT void netPutFile(ArNetPacket *packet);
  AREXPORT void callFileSentCallbacks(int val);

  ArMutex myDataMutex;
  ArMutex myCallbackMutex;
  ArClientBase *myClient;
  bool myIsWaitingForReturn;
  bool myInterleaved;
  bool myTimestamp;
  std::string myCommandName;
  std::string myDirectory;
  std::string myFileName;
  std::string myWholeFileName;
  std::string myClientFileName;
  
  bool myReadyForNextPacket;
  FILE *myFile;
  ArTime myLastStartedSend;
  ArTime myLastCompletedSend;
  std::list<ArFunctor1<int> *> myFileSentCallbacks;
  ArFunctor1C<ArClientFileFromClient, ArNetPacket *> myPutFileCB;
};


/// Class for deleting a file on the server
/**
   This class will interact with the ArServerFileFromClient and put a
   file on to the server.  If you want to find out what files are on
   the server use ArClientFileLister.

   When get a file it doesn't happen right away, but when the file is
   received (or failes) the fileGotten callbacks will be called, 0 as
   the int for the callback means everything is good, positive error
   messages are from the server (0 = good (got file), 1 = getting
   file, 2 = tried to go outside allowed area, 3 = bad directory, 4 =
   empty file name (or other problem with fileName), 5 = can't write
   temp file, 6 = error moving file from temp to perm, 7 = another
   client putting file, 8 = timeout (no activity for 15 seconds) and
   another client wanted to put the file, 9 = client adding to,
   finishing, or canceling a file the server doesn't have), negative
   would be from this class but there aren't any of those yet
**/
class ArClientDeleteFileOnServer
{
public:
  /// Constructor
  AREXPORT ArClientDeleteFileOnServer(ArClientBase *client);
  /// Destructor
  AREXPORT virtual ~ArClientDeleteFileOnServer();
  /// Sees if the server supports what this class needs
  AREXPORT bool isAvailable(void);      
  /// Get the file from a directory 
  AREXPORT bool deleteFileFromDirectory(const char *directory, 
					const char *fileName);
  /// If we're waiting for completion now
  AREXPORT bool isWaitingForReturn(void);
  /// Gets the directory we're putting to
  AREXPORT const char *getDirectory(void);
  /// Gets the filename we're putting
  AREXPORT const char *getFileName(void);
  /// Adds a callback for when we get the desired file (or fail)
  AREXPORT void addFileDeletedCallback(ArFunctor1<int> *functor, 
				   ArListPos::Pos position = ArListPos::LAST);
  /// Removes a callback for when we get the desired file (or fail)
  AREXPORT void remFileDeletedCallback(ArFunctor1<int> *functor);
  /// Gets the last time we finished putting a file
  AREXPORT ArTime getLastCompletedSend(void);
  /// Gets the last time we started putting a file
  AREXPORT ArTime getLastStartedSend(void);
protected:
  AREXPORT void netDeleteFile(ArNetPacket *packet);
  AREXPORT void callFileDeletedCallbacks(int val);
  ArMutex myDataMutex;
  ArMutex myCallbackMutex;
  ArClientBase *myClient;
  bool myIsWaitingForReturn;
  std::string myDirectory;
  std::string myFileName;
  std::string myWholeFileName;
  FILE *myFile;
  ArTime myLastStartedSend;
  ArTime myLastCompletedSend;
  std::list<ArFunctor1<int> *> myFileDeletedCallbacks;
  ArFunctor1C<ArClientDeleteFileOnServer, ArNetPacket *> myDeleteFileCB;
};

#endif //ARCLIENTFILEUTILS_H