User Manual, Developers Guide and API Documentation

Connection.cpp

Go to the documentation of this file.
00001 /*******************************************************************************
00002  * This file is part of openWNS (open Wireless Network Simulator)
00003  * _____________________________________________________________________________
00004  *
00005  * Copyright (C) 2004-2007
00006  * Chair of Communication Networks (ComNets)
00007  * Kopernikusstr. 16, D-52074 Aachen, Germany
00008  * phone: ++49-241-80-27910,
00009  * fax: ++49-241-80-22242
00010  * email: info@openwns.org
00011  * www: http://www.openwns.org
00012  * _____________________________________________________________________________
00013  *
00014  * openWNS is free software; you can redistribute it and/or modify it under the
00015  * terms of the GNU Lesser General Public License version 2 as published by the
00016  * Free Software Foundation;
00017  *
00018  * openWNS is distributed in the hope that it will be useful, but WITHOUT ANY
00019  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
00020  * A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
00021  * details.
00022  *
00023  * You should have received a copy of the GNU Lesser General Public License
00024  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00025  *
00026  ******************************************************************************/
00027 
00028 #include <SIMPLETL/service/Connection.hpp>
00029 #include <SIMPLETL/SimpleTL.hpp>
00030 #include <SIMPLETL/events/DataInd.hpp>
00031 #include <SIMPLETL/Medium.hpp>
00032 #include <SIMPLETL/PCI.hpp>
00033 #include <SIMPLETL/PDU.hpp>
00034 #include <WNS/module/Module.hpp>
00035 #include <WNS/logger/Master.hpp>
00036 #include <WNS/osi/PDU.hpp>
00037 #include <WNS/service/tl/DataHandler.hpp>
00038 #include <WNS/Assure.hpp>
00039 
00040 
00041 using namespace simpletl;
00042 
00043 Connection::Connection(wns::service::tl::ConnectionHandler* ch, wns::service::tl::FlowID _flowID,
00044                        Bit _headerSize)
00045     :
00046     datahandler(NULL),
00047     connectionhandler(ch),
00048     peer(NULL),
00049     flowID(_flowID),
00050     headerSize(_headerSize),
00051     eventscheduler(wns::simulator::getEventScheduler()),
00052     logger("SimpleTL", "Connection", wns::simulator::getMasterLogger())
00053 {
00054     wns::pyconfig::View pyco = wns::module::Module<SimpleTL>::getPyConfigView().getView("channel");
00055     channelcapacity = pyco.get<double>("capacity");
00056 }
00057 
00058 Connection::~Connection()
00059 {
00060     delete datahandler;
00061     datahandler = NULL;
00062     peer = NULL;
00063     connectionhandler = NULL;
00064 }
00065 
00066 void Connection::registerDataHandler(wns::service::tl::DataHandler* _dh)
00067 {
00068     datahandler = _dh;
00069 }
00070 
00071 void Connection::sendData(const wns::osi::PDUPtr& _pdu)
00072 {
00073     Bit payloadSize = _pdu->getLengthInBits();
00074     assure(payloadSize, "Trying to send an empty PDU!");
00075 
00076     simpletl::PCI* pci = new simpletl::PCI(PCI::unknown, headerSize, payloadSize, flowID);
00077     simpletl::PDUPtr pdu(new simpletl::PDU(pci, _pdu));
00078 
00079     simTimeType delay = (double)(headerSize+payloadSize) / (1000*channelcapacity);
00080 
00081     Medium::send(DataInd(getPeer(), pdu), delay);
00082 }
00083 
00084 void Connection::receiveData(const simpletl::PDUPtr& _pdu)
00085 {
00086     assureType(_pdu.getPtr()->getPCI(), simpletl::PCI*);
00087 
00088     if (static_cast<simpletl::PCI*>(_pdu.getPtr()->getPCI())->flowID == swappedFlowID())
00089     {
00090         // If the data handler is not available, this indicates that the connection has not been
00091         // established yet.
00092         if (datahandler)
00093         {
00094             MESSAGE_SINGLE(NORMAL, logger, "Received packet with flow ID: "
00095                            << static_cast<simpletl::PCI*>(_pdu.getPtr()->getPCI())->flowID);
00096             datahandler->onData(wns::osi::PDUPtr(_pdu.getPtr()->getUserData()));
00097         }
00098         else
00099         {
00100             MESSAGE_SINGLE(NORMAL, logger, "Opening connection for flow ID: "
00101                            << static_cast<simpletl::PCI*>(_pdu.getPtr()->getPCI())->flowID);
00102             connectionhandler->onConnectionEstablished(
00103                 static_cast<simpletl::PCI*>(_pdu.getPtr()->getPCI())->flowID.srcAddress, this);
00104             assure(datahandler, "No data handler available although connection established!");
00105                            datahandler->onData(wns::osi::PDUPtr(_pdu.getPtr()->getUserData()));
00106         }
00107     }
00108     // Packet belongs to an old session. It will be discarded.
00109     else
00110     {
00111         MESSAGE_BEGIN(NORMAL, logger, m, "Discarding packet. It belongs to an old session with flow ID: ");
00112         m << static_cast<simpletl::PCI*>(_pdu.getPtr()->getPCI())->flowID;
00113         m << " This connection has flow ID: " << swappedFlowID();
00114         MESSAGE_END();
00115     }
00116 }
00117 
00118 wns::service::tl::ConnectionHandler* Connection::getConnectionHandler()
00119 {
00120     assure(connectionhandler, "Connection handler not available!");
00121     return connectionhandler;
00122 }
00123 
00124 void Connection::setPeer(Connection* connection)
00125 {
00126     peer = connection;
00127 }
00128 
00129 Connection* Connection::getPeer()
00130 {
00131     assure(peer, "Peer entity not available!");
00132     return peer;
00133 }
00134 
00135 wns::service::tl::FlowID Connection::getFlowID()
00136 {
00137     return flowID;
00138 }
00139 
00140 wns::service::tl::FlowID Connection::swappedFlowID()
00141 {
00142     wns::service::tl::FlowID _flowID(flowID.dstAddress, flowID.dstPort, flowID.srcAddress, flowID.srcPort);
00143     return _flowID;
00144 }

Generated on Sun May 27 03:32:17 2012 for openWNS by  doxygen 1.5.5