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

ReceivingPort Class Reference

A Receiving Port Class. More...

#include <common.h>

Inheritance diagram for ReceivingPort:

Port List of all members.

Public Member Functions

 ReceivingPort ()
void init ()
PacketreceivePacket ()
 The main receive function of receiving port to receive a single packet.

Protected Attributes

Packetpkt_
char * tmpBuffer_
 temporary buffer for packets

Detailed Description

A Receiving Port Class.

ReceivingPort is an abstract class for the interface to receive a packet. The main function for the receiving port:

Definition at line 228 of file common.h.


Constructor & Destructor Documentation

ReceivingPort::ReceivingPort  ) 
 

Constructor

Definition at line 351 of file common.cpp.

References pkt_.

00351                             : Port()
00352 {
00353   pkt_= new Packet(MAXBUFLENGTH);
00354 }


Member Function Documentation

void ReceivingPort::init  )  [virtual]
 

Init Funciton to initialize a socket port.

The port binds to its own address, generate a UDP socket.

Bind to local address is one important task in init() Here source address of node itself (myaddr_) does not really be used by bind function of port. The program use INADDR_ANY as the address filled in address parameters of bind(). So, we need an empty hostname with the port number.

When the UDP port is recieving, we need to create a default buffer to store received packet. The data in buffer will be copy to the corresponding flow once the packet's sender address is checked.

Implements Port.

Definition at line 370 of file common.cpp.

References Address::getPort(), Address::isSet(), Port::myaddr_, Port::mySockAddress_, Port::setHostname(), Port::setPort(), Port::setSockAddress(), Port::sockfd_, and tmpBuffer_.

00371 { 
00372   if (sockfd_ != 0) {
00373     return;
00374   }
00375   if ( myaddr_.isSet() == false) {
00376     setHostname("localhost");
00377     setPort(DEFAULT_RECV_PORT);
00378   }
00379   
00380   if ((sockfd_ = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
00381     throw "Error while opening UDP socket of a receiver";
00382   }
00383   Address *emptyAddr = new Address("", myaddr_.getPort());
00384   struct sockaddr* addr = setSockAddress(emptyAddr, &mySockAddress_);
00385   if (  bind(sockfd_, addr, sizeof(struct sockaddr_in))  < 0 ){
00386     throw "Scoket Bind Error occured in an UDP receiver";
00387   }
00388   //cout << "binding to port: " << myaddr_.getPort() << "......" << endl;
00389   // needs a dummy buffer for storing packets
00390   tmpBuffer_ =  new char[MAXBUFLENGTH];
00391 }

Packet * ReceivingPort::receivePacket  ) 
 

The main receive function of receiving port to receive a single packet.

The main receive function of a receiving port. First,check addresses. Then call recvfrom() to get a packet. after this, pkt_ variable stores information of the packet and tmpSockAddr stores the sender information. then, recast tmpSockAddr to itsaddr_. As the socket is not set as non-blocking, the recvfrom() call blocks usually, but if the main program use select() to do synchronized I/O Multiplexing, this call will not block.

The design structure allows a future implementation improvement.

packet size is the maximum allowed packet above UDP or buffer size

Definition at line 406 of file common.cpp.

References Port::decodeSockAddress(), Packet::fillPayload(), Port::itsaddr_, pkt_, Port::sockfd_, and tmpBuffer_.

00407 {
00408   struct sockaddr_in tmpSockAddr;
00409   int length = sizeof(struct sockaddr);
00410   int len = (int)recvfrom(sockfd_, tmpBuffer_, MAXBUFLENGTH, 0, (struct sockaddr*)&tmpSockAddr,(socklen_t *)&length); 
00411   if (len == -1) 
00412   {
00413            perror("recvfrom");
00414            return false;
00415   }         
00416   decodeSockAddress( &itsaddr_, &tmpSockAddr);
00417   pkt_->fillPayload(len, tmpBuffer_);
00418   return pkt_;
00419 }


Member Data Documentation

Packet* ReceivingPort::pkt_ [protected]
 

This pointer points to the packet. This packet is just received.

Definition at line 241 of file common.h.

Referenced by receivePacket(), and ReceivingPort().


The documentation for this class was generated from the following files:
Generated on Thu Jan 26 17:08:01 2006 for Common_Classes_Projects_ECE544 by  doxygen 1.4.2