00001 #ifndef __LINKLAYERINTERFACE_H
00002 #define __LINKLAYERINTERFACE_H
00003
00004 #include <stdio.h>
00005 #include <string>
00006 #include "Node.h"
00007 #include "ConnectionTableEntry.h"
00008 #include "NpPacket.h"
00009 #include "ListenerInterface.h"
00010 #include "ConnectionPacket.h"
00011 #include "PacketHandlerInterface.h"
00012 #include "Condition.h"
00013
00014 using namespace std;
00015
00016
00017 class LinkLayerInterface : public BasicObject, public ListenerInterface, public ObservableInterface
00018 {
00019 public:
00020 OS_SPEC_ENUM ReturnCode {
00021 LLI_OK = 0,
00022 LLI_ERROR = -1,
00023 LLI_BAD_ADDRESS = -2,
00024 LLI_NEXT_HOP_UNREACHABLE = -3,
00025 LLI_RECEIVE_ERROR = -4,
00026 LLI_NOT_CONNECTED = -5
00027 };
00028
00029 OS_SPEC_ENUM EventCode {
00030 CONNECTION_OPENED,
00031 CONNECTION_CLOSED
00032 };
00033
00034
00035
00036 LinkLayerInterface();
00037 virtual ~LinkLayerInterface();
00038
00039
00040
00041
00042 int getNumConnections();
00043 bool isConnected();
00044 bool isConnectedTo(IpAddress ipAddr);
00045 Node* getRandomNeighbor();
00046 Node* getRandomNeighbor(Node* node);
00047 bool getConnection(int index, Node* retVal);
00048 Node* getConnection(int index);
00049
00050
00051
00052
00053 void registerPacketHandler(PacketHandlerInterface* packetHandler);
00054
00055
00056
00057
00058
00059 void handleEvent(ObservableInterface* observed, int event, void* object);
00060
00061
00062
00063
00064 int connect(Node* node);
00065 void changeListeningPortTo(int port);
00066 int sendPacket(NpPacket* packet, Node* node);
00067 int sendPacket(ConnectionPacket* packet);
00068 int broadcast(NpPacket* packet);
00069 int broadcast(NpPacket* packet, Node* node);
00070 int close(Node*);
00071
00072
00073
00074
00075 virtual void toStream(std::ostream& out);
00076
00077 protected:
00078
00079
00080
00081 static void* listen(void*);
00082 void listenImpl();
00083
00084 static void* peerConnect(void*);
00085 void peerConnectImpl();
00086
00087 static void* pollAllSockets(void*);
00088 void pollAllSocketsImpl(void);
00089
00090
00091
00092
00093 void add(ConnectionTableEntry*);
00094 void remove(int connTableIndex);
00095
00096
00097
00098
00099 ConnectionTableEntry* accept();
00100 void closeLink(int connTableIndex);
00101
00102
00103
00104
00105
00107 Condition m_connectionClosed;
00108
00110 Mutex m_connectionTableLock;
00111
00113 vector<ConnectionTableEntry*> m_connectionTable;
00114
00116 pthread_t m_listenThreadId;
00117
00119 pthread_t m_peerConnectThreadId;
00120
00121
00122 pthread_t m_receivePacketThreadId;
00123
00125 Condition m_newConnection;
00126
00128 Condition m_nodeAdded;
00129
00131 fd_set m_socketSet;
00132
00136 int m_highestFd;
00137
00139 PacketHandlerInterface* m_packetHandler;
00140
00142 ListenerInterface* m_connectionListener;
00143
00145 string m_state;
00146
00147 TRanrotBGenerator m_randomNumberGenerator;
00148 };
00149
00150 #endif