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

Port Class Reference

Port class abstacts functions of communication interfaces. More...

#include <common.h>

Inheritance diagram for Port:

ReceivingPort SendingPort LossyReceivingPort List of all members.

Public Member Functions

 Port ()
virtual ~Port ()
virtual void init ()=0
void setAddress (Address *addr)
 set the port's own address
void setRemoteAddress (Address *daddr)
 set the address of the port at the other end of communication link
AddressgetRemoteAddr ()
 get the address of the port at the other end of communication link
void closePort ()
 close the port

Protected Member Functions

sockaddr * setSockAddress (Address *addr, struct sockaddr_in *address)
 cast an Address to socket address format
void decodeSockAddress (Address *addr, struct sockaddr_in *address)
 cast a socket address to normal address format
void setHostname (const char *hostname)
 set hostname of local address
void setPort (const short port)
 set port of local address
void setRemoteHostname (const char *hostname)
 set hostname of remote address
void setRemotePort (const short port)
 set port no of a remote address
int getSock ()
 get the socket file descriptor

Protected Attributes

Address myaddr_
 The default address of mine.
Address itsaddr_
 The default address of the other end of communication link.
sockaddr_in mySockAddress_
 IN UDP. this will be INADDR_ANY, not really my IP addr.
sockaddr_in dstSockAddress_
 for every outgoing packet of a connection, the destination address.
int sockfd_
 socket file descriptor

Detailed Description

Port class abstacts functions of communication interfaces.

Port is an abstract class for the interface to send/receive a packet, whether UDP, TCP Socket, or IP raw socket. The common funcitons for a port are defiend here:

Port is also associated with a pair of address. One Port send to one Address only, no matter the address is unicast or broadcast.

Definition at line 214 of file common.h.

Constructor & Destructor Documentation

Port::Port  ) 



Definition at line 263 of file common.cpp.

00263           :sockfd_(0)
00264 {
00265 }

Member Function Documentation

void Port::decodeSockAddress Address addr,
struct sockaddr_in *  address

cast a socket address to normal address format

Function to interpreate hostname and port from the SocketAddress

Definition at line 325 of file common.cpp.

Referenced by ReceivingPort::receivePacket().

00326 {
00327   addr->setHostname(inet_ntoa(address->sin_addr));
00328   addr->setPort(ntohs(address->sin_port));  
00329 }

virtual void Port::init  )  [pure virtual]

Function to initialize the port

Implemented in SendingPort, and ReceivingPort.

struct sockaddr * Port::setSockAddress Address addr,
struct sockaddr_in *  address

cast an Address to socket address format

Fill sockaddr_in 'address' structure with information taken from 'addr' and return it cast to a 'struct sockaddr'. It handles following situations:

  • if hostname is given as empty "", then INADDR_ANY is used in return
  • if an IP address is given, then address could be set directly
  • if a hostname is given, call gethostbyname() to find the ip address of the hostname from DNS

Definition at line 287 of file common.cpp.

References Address::getHostname(), and Address::getPort().

Referenced by ReceivingPort::init(), SendingPort::init(), and SendingPort::sendPacket().

00288 {
00289   char *hostname;
00290   int port;
00291   unsigned int tmp;
00292   struct hostent *hp;
00294   hostname = addr->getHostname();
00295   port = addr->getPort();
00297   address->sin_family = AF_INET;
00298   address->sin_port   = htons((short)port);
00300   if (strcmp(hostname, "") == 0) {
00301     address->sin_addr.s_addr = htonl(INADDR_ANY);  
00302   } 
00303   else {
00304     //tmp = inet_addr(hostname);  // If an IP address is given
00305     tmp = inet_aton(hostname, &(address->sin_addr));
00306     //if(tmp != (unsigned long) INADDR_NONE){    
00307     //  address->sin_addr.s_addr = tmp;  
00308     //}
00309     if (tmp == 0) 
00310     {  // if a hostname is passed, call DNS
00311       if ((hp = gethostbyname(hostname)) == NULL) {
00312         herror("gethostbyname");
00313         throw "Error in Resolving hostname!" ;                           
00314       }
00315       memcpy((char *)&address->sin_addr, (char *)hp->h_addr, hp->h_length);
00316     }
00317   }
00318   return (sockaddr*)address;
00319 }

The documentation for this class was generated from the following files:
Generated on Fri Feb 17 17:55:49 2006 for Common_Classes_Projects_ECE544 by  doxygen 1.4.2