NetP.h
Go to the documentation of this file.00001
00002
00003
00004
00005 #ifndef NETP_H
00006 #define NETP_H
00007
00008 #include <string>
00009 #include <cstring>
00010 #include <cmath>
00011 #include <sstream>
00012 #include <map>
00013 #include <set>
00014 #include <pvm3.h>
00015 #include <vector>
00016 #include <cstdlib>
00017 #include <unistd.h>
00018 #include <deque>
00019 #include <sys/timeb.h>
00020 #include <sys/time.h>
00021 #include <iostream>
00022 #include <pthread.h>
00023
00024 #include <fcntl.h>
00025 #include <arpa/inet.h>
00026 #include <netdb.h>
00027 #include <sys/socket.h>
00028 #include <sys/types.h>
00029 #include <errno.h>
00030 #include <netinet/in.h>
00031
00032 #include "PData.h"
00033
00034 using namespace std;
00035
00038 class NetP {
00039
00040 public:
00041
00043
00044
00047 NetP(string packettype);
00048
00050 NetP();
00051
00052 ~NetP();
00053
00059 void cleanUp();
00060
00064 int joinChannel(string channel);
00065
00069 int leaveChannel(string channel);
00070
00072
00074
00075
00078 int send();
00079
00083 string bReceive();
00084
00088 string nReceive();
00089
00095 string tReceive(double timeout_sec);
00096
00099 string bReceiveNewest();
00100
00104 string nReceiveNewest();
00105
00110 string tReceiveNewest(double timeout_sec);
00111
00115 int peek();
00116
00118
00120
00125 PData& data();
00126
00129 string getType();
00130
00135 double getTimeStamp();
00136
00139 static double currentTime();
00140
00143 int getSize();
00144
00147 int getTid();
00148
00151 string getHostname();
00152
00155 int getProcessID();
00156
00159 string toString();
00160
00162 friend ostream& operator<<(ostream& o, NetP& netp) {
00163 o << netp.toString();
00164 return o;
00165 }
00167
00169
00170
00175 void setBufferSize(long bytes);
00176
00185 static void setMaxPeriod(double seconds);
00186
00193 static void setMinPeriod(double seconds);
00194
00199 static void setUnthreaded();
00200
00206 static void LockPvmMutex();
00207
00209 static void UnlockPvmMutex();
00210
00214 static void Exit();
00215
00217
00219
00220
00222 static void DirectOffer(string channel);
00223
00225 static int DirectConnect(string channel, unsigned short int port = 51552);
00226
00228 static void DirectClose(string channel);
00229
00231 static void DirectClose(int socket);
00232
00234 static void DirectCloseAll();
00235
00238 static int DirectWrite(int socket, const char* buf, int len=-1);
00239
00242 static int DirectWrite(int socket, string buf);
00243
00246 static int DirectWrite(string channel, string buf);
00247
00250 static int DirectRead(int socket, char* buf, int len);
00251
00258 static int DirectConnectToServer(string hostname, string direct_id,
00259 unsigned short int port=51552);
00260
00263 static string DirectRead(int socket);
00264
00267 static string DirectRead(string channel);
00268
00270 static vector<string> ListOpenConnects();
00271
00273 static vector<string> ListPendingConnects();
00274
00279 static int GetSocketNum(string channel);
00280
00282 private:
00283
00284 static void Connect();
00285 static void Spawn();
00286 static void* NetLoop(void* arg);
00287 static void ReceiveMapAdd(string type, NetP* ptr);
00288 static void ReceiveMapDelete(string type);
00289 static NetP* ReceiveMapLookup(string type);
00290 static int ReceiveOnce();
00291 static int UnthreadedReceiveOnce();
00292
00293
00294 void pvmInit();
00295 void threadInit();
00296 void initPData();
00297 void clearPData();
00298 void destroyPData();
00299 void receiveBufferToPData();
00300 int hasQueuedReceive();
00301 void insertReceived(PData* pmeta, PData* pdata);
00302 void eatRecvBuffer(int remaining = 0);
00303
00304 PData& getMetaField(string index);
00305 void updateTimeStamp();
00306
00307
00308
00309
00310 static const char* MetaDataSetup;
00311 static int ProcessID;
00312 static string HostName;
00313
00314
00315 static int IsThreadCalled;
00316 static int IsThreadStarted;
00317 static int IsThreadRunning;
00318 static int RUN_THREAD;
00319 static int NO_THREAD_MODE;
00320 static pthread_t ThreadID;
00321
00322
00323 static int MyTid;
00324 static pthread_mutex_t PvmOpMutex;
00325
00326
00327 static double MinReceivePeriod;
00328 static double MaxReceivePeriod;
00329 static double ReceivePeriod;
00330 static long RECEIVE_TIMEOUT_US;
00331 static timeval TimeSyncDiff;
00332 static int TimeSyncDTID;
00333
00334
00335 static map<string, NetP*> ReceiveMap;
00336 static pthread_mutex_t ReceiveMapMutex;
00337 static char* ReceiveBuffer;
00338 static int ReceiveBufferSize;
00339 static pthread_mutex_t ReceiveBlockMutex;
00340
00341
00342 static map<string, int> DirectSockets;
00343 static set<string> DirectChannels;
00344
00345
00346
00347 int cleaned_up;
00348
00349 int is_send_type;
00350 string send_group;
00351 char* send_buffer;
00352
00353 set<string> groups;
00354 long recv_buffer_limit;
00355 long recv_buffer_size;
00356 pthread_mutex_t recv_buffer_mutex;
00357 pthread_cond_t recv_block_condition;
00358 deque<PData*> recv_buffer;
00359
00360 PData* _meta;
00361 PData* _data;
00362 pthread_mutex_t pdata_mutex;
00363 };
00364
00365
00366 #endif