Main Page | Class Hierarchy | Class List | File List | Class Members

common.h

00001 #ifndef COMMON_H
00002 #define COMMON_H
00003 
00004 #define MAX_HOSTNAME_LENGTH 256
00005 #define MAC_ADDR_LENGTH 6
00006 #define MAX_HEADER_SIZE 256
00007 #define DEFAULT_SEND_PORT 3000
00008 #define DEFAULT_RECV_PORT 4000
00009 #define MAXBUFLENGTH 10000
00010 #define MTU_SIZE 1500
00011 
00012 #include <string.h>
00013 #include <sys/socket.h>
00014 #include <sys/types.h>
00015 #include <unistd.h>
00016 #include <netinet/in.h>
00017 #include <netdb.h>
00018 #include <arpa/inet.h>
00019 #include <cstdlib>
00020 #include <ctime>
00021 
00022 #ifndef INADDR_NONE
00023 #  define INADDR_NONE (-1)
00024 #endif
00025 
00026 
00028 
00042 class PacketHdr{
00043  
00044  public:
00045   PacketHdr();
00049   inline void init(){memset(info_,0,MAX_HEADER_SIZE );}
00053   short getShortIntegerInfo(int position);
00057   int getIntegerInfo(int position);
00061   inline unsigned char* accessInfo(){return info_;}
00065   inline int getSize() { return length_; } 
00069   void setIntegerInfo(int a, int position);
00073   void setShortIntegerInfo(short b, int position);
00077   inline void setOctet( unsigned char c, int position){ *(info_+position)= c; length_++;}
00081   inline void setHeaderSize(int len){ length_ =len; }
00085   inline unsigned char getOctet(int position){ return info_[position];}
00086   
00087  protected :
00088    unsigned char* info_;   
00089    int length_; 
00090 };
00091 
00092 
00094 
00099 class Packet {
00100  
00101 public:
00105   static const int DEFAULT_PAYLOAD_SIZE = 512;
00106   Packet();
00107   Packet(int buffer_length);
00108   int fillPayload(int size, char *inputstream);  
00112   inline char* getPayload(){ return payload_;} 
00113   void setPayloadSize(int size);
00117   inline int getBufferSize(){return length_;}
00121   inline int getPayloadSize(){ return size_;}
00125   inline int getHeaderSize(){ return header_->getSize();}
00129   inline PacketHdr* accessHeader() { return header_;} 
00130   void extractHeader (char * streambuf);
00131   int makePacket (  char* streambuf);
00132  
00133 protected:               
00134       int size_;  
00135       int length_; 
00136       char* payload_; 
00137       PacketHdr * header_; 
00138 };
00139 
00141 
00148 class Address
00149 {
00150   
00151  public:
00153   Address();
00155   Address(const char* hostname, short port);
00156    
00161   inline bool isSet() { return (hostname_[0] != '\0' && port_ >= 0); }
00165   inline void setPort(const short port){ port_ = port; }
00167   inline short getPort(){ return port_; }
00169 
00172   inline void setHostname(const char* hostname) { 
00173     if (hostname == NULL) hostname_[0] = '\0'; else strcpy(hostname_, hostname); }
00175   inline char* getHostname() {return hostname_;} 
00177   inline   unsigned char* getHWAddr() { return  macaddr_;} 
00178   void setHWAddr( unsigned char* hwaddr); 
00179   void setHWAddrFromColonFormat(const char* colon_seperated_macaddr);
00180   char * convertHWAddrToColonFormat();
00182   Address *clone();
00186   inline bool isSame(Address* addr)
00187   {
00188     if ( port_==  addr->getPort()) return false;
00189     if ( strcmp( hostname_, addr->getHostname() ) == 0 ) return true;
00190     return false;
00191   } 
00192   bool isSameMACAddr(Address* addr);
00193 
00194 protected:
00195   char   hostname_[MAX_HOSTNAME_LENGTH]; 
00196   short port_;  
00197   char * ipaddr_;  
00198   unsigned char   macaddr_[MAC_ADDR_LENGTH]; 
00199   
00200 };
00201 
00203 
00214 class Port
00215 {
00216  public:
00218   Port();
00220   virtual ~Port(){}
00224   virtual void init()=0;
00226   void setAddress(Address *addr);
00228   void setRemoteAddress( Address *daddr);
00230   inline Address *getRemoteAddr(){return &itsaddr_;}
00232   inline void closePort(){close(sockfd_);}
00233  protected:
00235   struct sockaddr* setSockAddress(Address *addr, struct sockaddr_in *address);
00237   void decodeSockAddress ( Address *addr, struct sockaddr_in *address);
00239   inline void setHostname(const char* hostname){ myaddr_.setHostname(hostname);}
00241   inline void setPort(const short port){ myaddr_.setPort(port);}
00243   inline void setRemoteHostname(const char* hostname){itsaddr_.setHostname(hostname);}
00245   inline void setRemotePort(const short port){itsaddr_.setPort(port); }
00247   inline int getSock(){ return sockfd_;}
00248  
00249  protected:
00250   Address myaddr_;  
00251   Address itsaddr_; 
00252   struct sockaddr_in mySockAddress_;  
00253   struct sockaddr_in dstSockAddress_;  
00254   int sockfd_; 
00255 };
00256 
00257 
00259 
00268 class TxTimer{
00269 friend class SendingPort;
00270  public:
00271     TxTimer(SendingPort *txport);
00276     void startTimer(float delay);
00280     void stopTimer();
00281  public:
00285     static void *timerProc(void *arg); 
00286  protected:
00290     SendingPort *port_;
00294     struct timespec tdelay_;
00298     pthread_t tid_;
00299 };
00300 
00301 
00302 
00304 
00307 class SendingPort : public Port
00308 {
00309  public:
00310   SendingPort();
00312   SendingPort(char* hostname, short port);
00314   virtual ~SendingPort(){}
00318   void init();
00324   void sendPacket(Packet *pkt);
00328   inline void setBroadcast(){bcastflag_ = 1;}
00332   inline void setBroadcastOff(){bcastflag_ = 0;}
00338   virtual void timerHandler()=0;
00339 
00340 
00341  protected:
00343 
00346   int bcastflag_; 
00350   char *sendingbuf_;
00351 
00352  public:
00357    TxTimer timer_;
00358 };
00359 
00361 
00369 class ReceivingPort : public Port
00370 {
00371  public:
00372   ReceivingPort();
00373   virtual ~ReceivingPort(){}
00374   void init();
00376   Packet* receivePacket(); 
00377   
00378  protected:
00382    Packet *pkt_;   
00383    char *tmpBuffer_;  
00384 };
00385 
00387 
00390 class LossyReceivingPort: public ReceivingPort
00391 {
00392  public:
00393   LossyReceivingPort(float lossyratio);
00394   ~LossyReceivingPort(){}
00395   Packet* receivePacket();
00396  protected:
00397   float loss_ratio_; 
00398   int secdelay_;     
00399 };
00400 
00401 #endif
00402 
00403 

Generated on Fri Feb 17 17:55:49 2006 for Common_Classes_Projects_ECE544 by  doxygen 1.4.2