00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #if !defined(AFX_NETWORKCONNECTION_H__D7F7AF51_1DA2_4001_AA17_12E69A8AAA3A__INCLUDED_)
00028 #define AFX_NETWORKCONNECTION_H__D7F7AF51_1DA2_4001_AA17_12E69A8AAA3A__INCLUDED_
00029
00030 #if _MSC_VER > 1000
00031 #pragma once
00032 #endif // _MSC_VER > 1000
00033
00034 #include "standard.h"
00035 #include <stdlib.h>
00036 #include <stdio.h>
00037
00038 #include "Object.h"
00039 #include "JThread.h"
00040 #include "JString.h"
00041 #include "JTime.h"
00042 #include "TCPReceiver.h"
00043 #include "TCPLocation.h"
00044 #include "NetProtocol.h"
00045 #include "JSocket.h"
00046 #include "HTML.h"
00047 #include "ObjectQueue.h"
00048 #include "TimeSeries.h"
00049
00050 namespace cmlabs {
00051
00052 class Network;
00053 class HTTPRequest;
00054 class HTMLPage;
00055
00056 class NetworkConnection : public JThread, public Object
00057 {
00058 public:
00059
00060 NetworkConnection(JSocket* con, NetProtocol* netProtocol, TCPReceiver* rec);
00061
00062
00063 NetworkConnection(const TCPLocation& loc, NetProtocol* netProtocol, TCPReceiver* rec);
00064
00065
00066 NetworkConnection(const JString& addr, int port, NetProtocol* netProtocol, TCPReceiver* rec);
00067
00068
00069 bool restart(JSocket* con, NetProtocol* netProtocol, TCPReceiver* rec);
00070
00071
00072 bool restart(const TCPLocation& loc, NetProtocol* netProtocol, TCPReceiver* rec);
00073
00074
00075 bool restart(const JString& addr, int port, NetProtocol* netProtocol, TCPReceiver* rec);
00076
00077 virtual ~NetworkConnection();
00078
00079 Object* clone() const {return NULL;}
00080 JString getLocalHostname();
00081 JString getLocalIPAddress();
00082 JString getRemoteHostname();
00083 TCPLocation getRemoteLocation();
00084 TCPLocation getLastConnectedTo();
00085 bool unitTest();
00086 void init(TCPReceiver* rec);
00087 bool initializeAsReceiver(TCPReceiver* rec);
00088 void run();
00089 bool terminate();
00090
00091 bool isErrorRecoverable();
00092 bool isConnected();
00093 bool isConnectedTo(const TCPLocation& loc);
00094 bool isConnectedTo(const JString& name);
00095 bool isPersistent();
00096 bool hasTerminated();
00097 bool isReset();
00098 bool isBroken();
00099
00100
00101 int read(char* buffer, int maxcount, long timeout);
00102 JString readString(unsigned int numBytes, unsigned int msTimeout);
00103 JString readln(unsigned int msTimeout);
00104 bool write(char* buffer, int length);
00105 bool writeString(const JString& str);
00106 bool writeln(const JString& str);
00107
00108 Message* sendReceiveObject(Message* msg, unsigned int msTimeout);
00109 Message* receiveObject(unsigned int msTimeout);
00110 bool sendObject(Message* msg, bool isReply);
00111 bool sendObjectWhenReady(Message* msg, int timeout = 3000);
00112
00113 double getSendProgress();
00114 double getReceiveProgress();
00115
00116 long msSinceLastRead();
00117 long msSinceLastWrite();
00118 long msSinceLastActivity();
00119
00120 bool shouldContinue;
00121 bool isDoneRunning;
00122 bool isTerminated;
00123 long timeout;
00124
00125 bool hasNoParent();
00126 bool reset();
00127
00128 JString print();
00129
00130 bool isReceiver();
00131 bool isLocalCallback();
00132 bool isRemoteCallback();
00133 bool isIncomingConnection();
00134
00135 bool setKeepAliveTimeout(long timeout);
00136 long getKeepAliveTimeout();
00137 bool useKeepAlive(bool doKeepAlive);
00138 bool sendKeepAlive();
00139 bool ping(const JString& from, const JString& to, int timeout = 5000);
00140
00141
00142 bool trainNetworkProfile();
00143 bool trainNetworkProfile(ConnectionProfile* profile);
00144 Message* performBenchmark(Message* msg);
00145 long pingRemotePeer();
00146 long uploadServerTest(long size);
00147 long downloadServerTest(long size);
00148 bool sendLocalProfile();
00149 bool getRemoteProfile();
00150
00151 bool allowMessageDropping(bool allow);
00152
00153 Network* parent;
00154
00155 TCPReceiver* receiver;
00156 NetProtocol* netprotocol;
00157 JString conID;
00158 JString remoteName;
00159 bool createdAsReceiver;
00160
00161 HTTPRequest* recHTTP;
00162
00163 ConnectionProfile localProfile;
00164 ConnectionProfile remoteProfile;
00165
00166 private:
00167 int count;
00168
00169 JSocket* mySocket;
00170 JMutex conMutex;
00171
00172 bool passive;
00173 void resetBuffer();
00174 JTime lastRead;
00175 JTime lastWrite;
00176 long keepAliveTimeout;
00177 TCPLocation lastConnectedTo;
00178 ObjectQueue receivedMessages;
00179 ObjectQueue toSendMessages;
00180 JSemaphore toSendSemaphore;
00181 int toSendMaxQueueSize;
00182 bool allowMessageDrop;
00183 };
00184
00185 }
00186
00187 #endif // !defined(AFX_NETWORKCONNECTION_H__D7F7AF51_1DA2_4001_AA17_12E69A8AAA3A__INCLUDED_)