00001 #ifndef __NETWORKLAYER_H
00002 #define __NETWORKLAYER_H
00003
00004 #include <pthread.h>
00005 #include "NpPacket.h"
00006 #include "TpPacket.h"
00007 #include "ConnectionPacket.h"
00008 #include "DiscoveryPacket.h"
00009 #include "ImHerePacket.h"
00010 #include "FinPacket.h"
00011 #include "Node.h"
00012 #include "LinkLayerInterface.h"
00013 #include "VirtualCircuitTable.h"
00014 #include "LiveBroadcastTable.h"
00015 #include "PriorityPacketQueue.h"
00016 #include "NetworkLayerListener.h"
00017 #include "PacketHandlerInterface.h"
00018
00019 class NetworkLayer : public BasicObject, public ListenerInterface, public PacketHandlerInterface {
00020 public:
00021
00022 OS_SPEC_ENUM ReturnCode {
00023 NL_OK = 0,
00024 NL_ERROR = -1,
00025 NL_UNREACHABLE = -2,
00026 NL_BAD_ADDRESS = -3,
00027 NL_SEND_ERROR = -4
00028 };
00029
00030
00031 OS_SPEC_ENUM EventCode {
00032 DATA_EVENT,
00033 DISCONNECTION_EVENT,
00034 BREAK_EVENT,
00035 PASSIVE_CONNECTION_EVENT,
00036 ACTIVE_CONNECTION_EVENT
00037 };
00038
00039 NetworkLayer();
00040 NetworkLayer(NetworkLayerListener* networkLayerListener);
00041 virtual ~NetworkLayer();
00042
00043 LinkLayerInterface* getLli();
00044 LiveBroadcastTable* getLiveBroadcastTable();
00045 int makeVc(int connectionDescriptor);
00046 bool hasVc(int vcn);
00047 void ping();
00048 void discover();
00049
00050 void destroyVc(int vcn);
00051 int send(TpPacket* packet, int vcn);
00052 void signalBreak(int connectionDesc);
00053 void handlePacket(Node* fromNode, NpPacket* packet);
00054
00055
00056 void handleEvent(ObservableInterface* observed, int eventType, void* object);
00057 void destroyVcsTo(Node* node);
00058 virtual void toStream(std::ostream& out);
00059
00060 private:
00061 static void* processPackets(void* arg);
00062 void processPacketsImpl();
00063
00064 static void* pingThread(void*);
00065 void pingThreadImpl();
00066
00067 static void* discoveryThread(void* arg);
00068 void discoveryThreadImpl();
00069
00070 bool forwardPacket(Node* fromNode, NpPacket* packet);
00071 void handleControlPacket(Node* fromNode, NpPacket* packet);
00072 void handleBroadcastPacket(Node* fromNode, ConnectionPacket* packet);
00073 void handleConnectPacket(Node* fromNode, NpPacket* packet);
00074 void handleEstablishedPacket(Node* fromNode, NpPacket* packet);
00075 void handleFinPacket(Node* fromNode, NpPacket* packet);
00076 void handleDataPacket(Node* fromNode, NpPacket* packet);
00077 void handleDiscoveryPacket(Node* fromNode, NpPacket* packet);
00078 void handleImHerePacket(Node* fromNode, NpPacket* packet);
00079 void handlePingPacket(Node* fromNode, NpPacket* packet);
00080 void handlePongPacket(Node* fromNode, NpPacket* packet);
00081 int isThisPacketForMe(Node* fromNode, NpPacket* packet);
00082
00083 void connectForward(Node* fromNode, ConnectionPacket* packet);
00084 void connectTerminate(Node* fromNode, ConnectionPacket* packet);
00085
00086
00087
00088
00089
00091 VirtualCircuitTable* m_virtualCircuitTable;
00092
00094 LinkLayerInterface* m_linkLayerInterface;
00095
00097 LiveBroadcastTable* m_liveBroadcastTable;
00098
00100 NetworkLayerListener* m_networkLayerListener;
00101
00103 PriorityPacketQueue* m_packetQueue;
00104
00106 pthread_t m_processPacketThreadId;
00107
00109 pthread_t m_pingThreadId;
00110
00112 TimeValue m_beginPingTime;
00113
00115 Condition m_pingThreadInterrupt;
00116
00118 pthread_t m_discoveryThreadId;
00119
00121 Condition m_discoveryThreadInterrupt;
00122
00123 TRanrotBGenerator m_randomNumberGenerator;
00124 };
00125
00126 #endif // __NETWORKLAYER_H