|
INET Framework for OMNeT++/OMNEST
|
00001 // 00002 // Copyright (C) 2005 Andras Varga 00003 // 00004 // This program is free software; you can redistribute it and/or 00005 // modify it under the terms of the GNU Lesser General Public License 00006 // as published by the Free Software Foundation; either version 2 00007 // of the License, or (at your option) any later version. 00008 // 00009 // This program is distributed in the hope that it will be useful, 00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 // GNU Lesser General Public License for more details. 00013 // 00014 // You should have received a copy of the GNU Lesser General Public License 00015 // along with this program; if not, see <http://www.gnu.org/licenses/>. 00016 // 00017 00018 00019 #include <omnetpp.h> 00020 #include "UDPAppBase.h" 00021 #include "UDPSocket.h" 00022 #include "UDPControlInfo_m.h" 00023 00024 void UDPAppBase::bindToPort(int port) 00025 { 00026 EV << "Binding to UDP port " << port << endl; 00027 00028 // TODO UDPAppBase should be ported to use UDPSocket sometime, but for now 00029 // we just manage the UDP socket by hand... 00030 cMessage *msg = new cMessage("UDP_C_BIND", UDP_C_BIND); 00031 UDPControlInfo *ctrl = new UDPControlInfo(); 00032 ctrl->setSrcPort(port); 00033 ctrl->setSockId(UDPSocket::generateSocketId()); 00034 msg->setControlInfo(ctrl); 00035 send(msg, "udpOut"); 00036 } 00037 00038 void UDPAppBase::sendToUDP(cPacket *msg, int srcPort, const IPvXAddress& destAddr, int destPort) 00039 { 00040 // send message to UDP, with the appropriate control info attached 00041 msg->setKind(UDP_C_DATA); 00042 00043 UDPControlInfo *ctrl = new UDPControlInfo(); 00044 ctrl->setSrcPort(srcPort); 00045 ctrl->setDestAddr(destAddr); 00046 ctrl->setDestPort(destPort); 00047 msg->setControlInfo(ctrl); 00048 00049 EV << "Sending packet: "; 00050 printPacket(msg); 00051 00052 send(msg, "udpOut"); 00053 } 00054 00055 void UDPAppBase::printPacket(cPacket *msg) 00056 { 00057 UDPControlInfo *ctrl = check_and_cast<UDPControlInfo *>(msg->getControlInfo()); 00058 00059 IPvXAddress srcAddr = ctrl->getSrcAddr(); 00060 IPvXAddress destAddr = ctrl->getDestAddr(); 00061 int srcPort = ctrl->getSrcPort(); 00062 int destPort = ctrl->getDestPort(); 00063 00064 ev << msg << " (" << msg->getByteLength() << " bytes)" << endl; 00065 ev << srcAddr << " :" << srcPort << " --> " << destAddr << ":" << destPort << endl; 00066 } 00067 00068