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 LossyReceivingPort 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 369 of file common.h.


Constructor & Destructor Documentation

ReceivingPort::ReceivingPort  ) 
 

Constructor

Definition at line 429 of file common.cpp.

References pkt_.

00429                             : Port()
00430 {
00431   pkt_= new Packet(MAXBUFLENGTH);
00432 }


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 448 of file common.cpp.

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

00449 { 
00450   if (sockfd_ != 0) {
00451     return;
00452   }
00453   if ( myaddr_.isSet() == false) {
00454     setHostname("localhost");
00455     setPort(DEFAULT_RECV_PORT);
00456   }
00457   
00458   if ((sockfd_ = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
00459     throw "Error while opening UDP socket of a receiver";
00460   }
00461   Address *emptyAddr = new Address("", myaddr_.getPort());
00462   struct sockaddr* addr = setSockAddress(emptyAddr, &mySockAddress_);
00463   if (  bind(sockfd_, addr, sizeof(struct sockaddr_in))  < 0 ){
00464     throw "Scoket Bind Error occured in an UDP receiver";
00465   }
00466   //cout << "binding to port: " << myaddr_.getPort() << "......" << endl;
00467   // needs a dummy buffer for storing packets
00468   tmpBuffer_ =  new char[MAXBUFLENGTH];
00469 }

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

Reimplemented in LossyReceivingPort.

Definition at line 484 of file common.cpp.

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

Referenced by LossyReceivingPort::receivePacket().

00485 {
00486   struct sockaddr_in tmpSockAddr;
00487   int length = sizeof(struct sockaddr);
00488   int len = (int)recvfrom(sockfd_, tmpBuffer_, MAXBUFLENGTH, 0, (struct sockaddr*)&tmpSockAddr,(socklen_t *)&length); 
00489   if (len == -1) 
00490   {
00491            perror("recvfrom");
00492            return false;
00493   }         
00494   decodeSockAddress( &itsaddr_, &tmpSockAddr);
00495   pkt_->extractHeader(tmpBuffer_);
00496   //tmpBuffer_ pointer shall not be moved/shifted,it will reused by the receiving port.
00497   pkt_->fillPayload(len-1-pkt_->getHeaderSize(), tmpBuffer_+pkt_->getHeaderSize()+1 );
00498   return pkt_;
00499 }


Member Data Documentation

Packet* ReceivingPort::pkt_ [protected]
 

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

Definition at line 382 of file common.h.

Referenced by receivePacket(), and ReceivingPort().


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