Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(32)

Unified Diff: src/nist/model/nist-udp-echo-server.cc

Issue 326890043: Public Safety Communication modeling tools based on ns-3
Patch Set: Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « src/nist/model/nist-udp-echo-server.h ('k') | src/nist/model/nist-udp-groupecho-server.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/nist/model/nist-udp-echo-server.cc
===================================================================
new file mode 100644
--- /dev/null
+++ b/src/nist/model/nist-udp-echo-server.cc
@@ -0,0 +1,198 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/*
+ * Copyright 2007 University of Washington
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation;
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ *
+ * Modified by: NIST
+ */
+
+#include "ns3/log.h"
+#include "ns3/ipv4-address.h"
+#include "ns3/ipv6-address.h"
+#include "ns3/address-utils.h"
+#include "ns3/nstime.h"
+#include "ns3/inet-socket-address.h"
+#include "ns3/inet6-socket-address.h"
+#include "ns3/socket.h"
+#include "ns3/udp-socket.h"
+#include "ns3/simulator.h"
+#include "ns3/socket-factory.h"
+#include "ns3/packet.h"
+#include "ns3/uinteger.h"
+
+#include "nist-udp-echo-server.h"
+
+namespace ns3 {
+
+NS_LOG_COMPONENT_DEFINE ("NistUdpEchoServerApplication");
+
+NS_OBJECT_ENSURE_REGISTERED (NistUdpEchoServer);
+
+TypeId
+NistUdpEchoServer::GetTypeId (void)
+{
+ static TypeId tid = TypeId ("ns3::NistUdpEchoServer")
+ .SetParent<Application> ()
+ .AddConstructor<NistUdpEchoServer> ()
+ .AddAttribute ("Port", "Port on which we listen for incoming packets.",
+ UintegerValue (9),
+ MakeUintegerAccessor (&NistUdpEchoServer::m_port),
+ MakeUintegerChecker<uint16_t> ())
+ .AddTraceSource ("Rx", "A packet has been received",
+ MakeTraceSourceAccessor (&NistUdpEchoServer::m_rxTrace),
+ "ns3::Packet::PacketAddressTracedCallback")
+ .AddTraceSource ("RxWithAddresses", "A packet has been received",
+ MakeTraceSourceAccessor (&NistUdpEchoServer::m_rxTraceWithAddresses),
+ "ns3::NistUdpEchoServer::PacketAddressTracedCallback")
+ ;
+ return tid;
+}
+
+NistUdpEchoServer::NistUdpEchoServer ()
+{
+ NS_LOG_FUNCTION (this);
+}
+
+NistUdpEchoServer::~NistUdpEchoServer()
+{
+ NS_LOG_FUNCTION (this);
+ m_socket = 0;
+ m_socket6 = 0;
+}
+
+void
+NistUdpEchoServer::DoDispose (void)
+{
+ NS_LOG_FUNCTION (this);
+ Application::DoDispose ();
+}
+
+void
+NistUdpEchoServer::StartApplication (void)
+{
+ NS_LOG_FUNCTION (this);
+
+ if (m_socket == 0)
+ {
+ TypeId tid = TypeId::LookupByName ("ns3::UdpSocketFactory");
+ m_socket = Socket::CreateSocket (GetNode (), tid);
+ InetSocketAddress local = InetSocketAddress (Ipv4Address::GetAny (), m_port);
+ m_socket->Bind (local);
+ if (addressUtils::IsMulticast (m_local))
+ {
+ Ptr<UdpSocket> udpSocket = DynamicCast<UdpSocket> (m_socket);
+ if (udpSocket)
+ {
+ // equivalent to setsockopt (MCAST_JOIN_GROUP)
+ udpSocket->MulticastJoinGroup (0, m_local);
+ }
+ else
+ {
+ NS_FATAL_ERROR ("Error: Failed to join multicast group");
+ }
+ }
+ }
+
+ if (m_socket6 == 0)
+ {
+ TypeId tid = TypeId::LookupByName ("ns3::UdpSocketFactory");
+ m_socket6 = Socket::CreateSocket (GetNode (), tid);
+ Inet6SocketAddress local6 = Inet6SocketAddress (Ipv6Address::GetAny (), m_port);
+ m_socket6->Bind (local6);
+ if (addressUtils::IsMulticast (local6))
+ {
+ Ptr<UdpSocket> udpSocket = DynamicCast<UdpSocket> (m_socket6);
+ if (udpSocket)
+ {
+ // equivalent to setsockopt (MCAST_JOIN_GROUP)
+ udpSocket->MulticastJoinGroup (0, local6);
+ }
+ else
+ {
+ NS_FATAL_ERROR ("Error: Failed to join multicast group");
+ }
+ }
+ }
+
+ m_socket->SetRecvCallback (MakeCallback (&NistUdpEchoServer::HandleRead, this));
+ m_socket6->SetRecvCallback (MakeCallback (&NistUdpEchoServer::HandleRead, this));
+}
+
+void
+NistUdpEchoServer::StopApplication ()
+{
+ NS_LOG_FUNCTION (this);
+
+ if (m_socket != 0)
+ {
+ m_socket->Close ();
+ m_socket->SetRecvCallback (MakeNullCallback<void, Ptr<Socket> > ());
+ }
+ if (m_socket6 != 0)
+ {
+ m_socket6->Close ();
+ m_socket6->SetRecvCallback (MakeNullCallback<void, Ptr<Socket> > ());
+ }
+}
+
+void
+NistUdpEchoServer::HandleRead (Ptr<Socket> socket)
+{
+ NS_LOG_FUNCTION (this << socket);
+
+ Ptr<Packet> packet;
+ Address from,localAddress;
+ while ((packet = socket->RecvFrom (from)))
+ {
+ socket->GetSockName (localAddress);
+ m_rxTrace (packet, from);
+ m_rxTraceWithAddresses (packet, from, localAddress);
+
+ if (InetSocketAddress::IsMatchingType (from))
+ {
+ NS_LOG_INFO ("At time " << Simulator::Now ().GetSeconds () << "s server received " << packet->GetSize () << " bytes from " <<
+ InetSocketAddress::ConvertFrom (from).GetIpv4 () << " port " <<
+ InetSocketAddress::ConvertFrom (from).GetPort ());
+ }
+ else if (Inet6SocketAddress::IsMatchingType (from))
+ {
+ NS_LOG_INFO ("At time " << Simulator::Now ().GetSeconds () << "s server received " << packet->GetSize () << " bytes from " <<
+ Inet6SocketAddress::ConvertFrom (from).GetIpv6 () << " port " <<
+ Inet6SocketAddress::ConvertFrom (from).GetPort ());
+ }
+
+ packet->RemoveAllPacketTags ();
+ packet->RemoveAllByteTags ();
+
+ NS_LOG_LOGIC ("Echoing packet");
+ socket->SendTo (packet, 0, from);
+
+ if (InetSocketAddress::IsMatchingType (from))
+ {
+ NS_LOG_INFO ("At time " << Simulator::Now ().GetSeconds () << "s server sent " << packet->GetSize () << " bytes to " <<
+ InetSocketAddress::ConvertFrom (from).GetIpv4 () << " port " <<
+ InetSocketAddress::ConvertFrom (from).GetPort ());
+ }
+ else if (Inet6SocketAddress::IsMatchingType (from))
+ {
+ NS_LOG_INFO ("At time " << Simulator::Now ().GetSeconds () << "s server sent " << packet->GetSize () << " bytes to " <<
+ Inet6SocketAddress::ConvertFrom (from).GetIpv6 () << " port " <<
+ Inet6SocketAddress::ConvertFrom (from).GetPort ());
+ }
+
+ }
+}
+
+} // Namespace ns3
« no previous file with comments | « src/nist/model/nist-udp-echo-server.h ('k') | src/nist/model/nist-udp-groupecho-server.h » ('j') | no next file with comments »

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b