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_NETWORK_H__6B5DFBFC_F919_4991_9301_8342407B2A37__INCLUDED_)
00028 #define AFX_NETWORK_H__6B5DFBFC_F919_4991_9301_8342407B2A37__INCLUDED_
00029
00030 #if _MSC_VER > 1000
00031 #pragma once
00032 #endif // _MSC_VER > 1000
00033
00034
00035
00036
00037
00038
00039 #include "standard.h"
00040 #include <stdlib.h>
00041 #include <stdio.h>
00042 #include "JSemaphore.h"
00043 #include "JSocket.h"
00044
00045 #include "types.h"
00046 #include "TCPLocation.h"
00047 #include "Message.h"
00048 #include "JTime.h"
00049
00050 #include "ObjectCollection.h"
00051
00052
00053 #include "NetProtocol.h"
00054 #include "NetMessageProtocol.h"
00055 #include "NetOriginalMessageProtocol.h"
00056 #include "NetHTTPProtocol.h"
00057 #include "NetTelnetProtocol.h"
00058 #include "NetworkConnection.h"
00059 #include "JThread.h"
00060
00061 namespace cmlabs {
00062
00063 class ConnectionProfile;
00064
00065 class Network : public JThread, public Object, public TCPReceiver
00066 {
00067 public:
00068 Network(TCPReceiver* recv = NULL);
00069 virtual ~Network();
00070 bool init(int portNum = -1);
00071 void run();
00072
00073 bool allowConnections(bool allow);
00074 bool allowNetConnections;
00075
00076 Object* clone() const {return NULL;}
00077 bool isInitialized;
00078 bool shouldContinue;
00079 bool isDoneRunning;
00080 JString getLocalHostname();
00081 JString getLocalIPAddress();
00082 Collection getLocalIPAddresses();
00083 JString getRemoteHostname();
00084 JString getIPAddressFromName(const JString& name);
00085 IPNumber getIPNumberFromName(const JString& name);
00086 int getConnectionCount();
00087 int getSendingConnectionCount(const JString& name);
00088 int getReceivingConnectionCount(const JString& name);
00089 JString print() ;
00090
00091 NetworkConnection* openConnection(const TCPLocation& loc);
00092 NetworkConnection* openConnection(const TCPLocation& loc, NetProtocol* netprotocol);
00093 NetworkConnection* createNetworkConnectionTo(const TCPLocation& loc, TCPReceiver* rec);
00094 NetworkConnection* openIncomingConnection(JSocket* sock, NetProtocol* protocol);
00095
00096
00097 NetworkConnection* openReceivingConnection(const TCPLocation& loc);
00098 NetworkConnection* openReceivingConnection(const TCPLocation& loc, NetProtocol* netprotocol);
00099 bool checkReceivingConnections();
00100 bool checkOpenConnections();
00101 bool checkPrimaryConnection();
00102 bool setPrimaryConnection(const JString& addr, int port, const JString& name);
00103 bool setPrimaryConnection(const TCPLocation& loc);
00104 bool isPrimaryConnection(const JString& name);
00105 bool tryReconnectingLostConnections();
00106
00107 NetworkConnection* findResetConnection();
00108 NetworkConnection* findConnectedConnection(const TCPLocation& loc, NetProtocol* netprotocol);
00109 NetworkConnection* findConnectedConnection(const JString& name);
00110 bool isCurrentlyConnectedTo(const JString& name);
00111
00112 bool addProtocol(NetProtocol* protocol);
00113 bool removeProtocol(NetProtocol* protocol);
00114 bool removeAllProtocols();
00115 NetProtocol* detectProtocol(JSocket* socket, long timeout);
00116 NetProtocol* getPreferredNetProtocol();
00117 bool setPreferredNetProtocol(NetProtocol* prefProtocol);
00118
00119 bool trainNetworkProfile(const TCPLocation& loc, ConnectionProfile* profile);
00120
00121
00122 bool unitTest();
00123 Message* receiveTCPInput(Message* msg);
00124 Message* netObjectReceive(Message* msg, NetworkConnection* con);
00125 JString getName();
00126 int getPort();
00127
00128 TCPReceiver* receiver;
00129 NetProtocol* preferredNetProtocol;
00130 int netCheckTimeout;
00131 NetworkConnection* primaryCon;
00132 TCPLocation* primaryLoc;
00133
00134 ObjectDictionary* remoteReceiverCons;
00135
00136 protected:
00137
00138 JSocket* mySocket;
00139 int port;
00140 JString localIPAddress;
00141 Collection localIPAddresses;
00142 JString localHostname;
00143
00144 NetProtocol* netProtocols;
00145 JMutex primaryMutex;
00146
00147 ObjectDictionary lostSendConnections;
00148 ObjectDictionary lostReceiveConnections;
00149
00150 ObjectCollection* cons;
00151 ObjectDictionary* receiverCons;
00152 void deleteConnections();
00153 };
00154
00155 }
00156
00157
00158
00159 #endif // !defined(AFX_NETWORK_H__6B5DFBFC_F919_4991_9301_8342407B2A37__INCLUDED_)