1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  *  Copyright 2018. Lawrence Livermore National Security, LLC.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  *
18  * Author: Steven Smith <smith84@llnl.gov>
19  */
20 
21 #include "mpi-test-fixtures.h"
22 
23 #include "ns3/simulator.h"
24 #include "ns3/packet.h"
25 #include "ns3/inet-socket-address.h"
26 #include "ns3/inet6-socket-address.h"
27 #include "ns3/address.h"
28 #include "ns3/ptr.h"
29 
30 #include "mpi.h"
31 
32 namespace ns3 {
33 
34 unsigned long SinkTracer::m_sinkCount = 0;
35 unsigned long SinkTracer::m_line = 0;
36 int SinkTracer::m_worldRank = -1;
37 int SinkTracer::m_worldSize = -1;
38 
39 void
Init(void)40 SinkTracer::Init (void)
41 {
42   m_sinkCount = 0;
43   m_line = 0;
44   MPI_Comm_rank(MPI_COMM_WORLD, &m_worldRank);
45   MPI_Comm_size (MPI_COMM_WORLD, &m_worldSize);
46 }
47 
48 
49 void
SinkTrace(const ns3::Ptr<const ns3::Packet> packet,const ns3::Address & srcAddress,const ns3::Address & destAddress)50 SinkTracer::SinkTrace (const ns3::Ptr<const ns3::Packet> packet,
51                             const ns3::Address &srcAddress,
52                             const ns3::Address &destAddress)
53 {
54   m_sinkCount++;
55 }
56 
57 void
Verify(unsigned long expectedCount)58 SinkTracer::Verify (unsigned long expectedCount)
59 {
60 
61   unsigned long globalCount;
62 
63 #ifdef NS3_MPI
64   MPI_Reduce(&m_sinkCount, &globalCount, 1, MPI_UNSIGNED_LONG, MPI_SUM, 0, MPI_COMM_WORLD);
65 #else
66   globalCount = m_sinkCount;
67 #endif
68 
69   if (expectedCount == globalCount)
70     {
71       RANK0COUT ("PASSED\n");
72     }
73   else
74     {
75       RANK0COUT ("FAILED  Observed sink traces (" << globalCount << ") not equal to expected (" << expectedCount << ")\n");
76     }
77 }
78 
79 std::string
FormatAddress(const ns3::Address address)80 SinkTracer::FormatAddress (const ns3::Address address)
81 {
82   std::stringstream ss;
83 
84   if (InetSocketAddress::IsMatchingType (address))
85     {
86       ss << InetSocketAddress::ConvertFrom(address).GetIpv4 ()
87          << ":"
88          << InetSocketAddress::ConvertFrom (address).GetPort ();
89     }
90   else if (Inet6SocketAddress::IsMatchingType (address))
91     {
92       ss << Inet6SocketAddress::ConvertFrom(address).GetIpv6 ()
93          << ":"
94          << Inet6SocketAddress::ConvertFrom (address).GetPort ();
95     }
96   return ss.str ();
97 }
98 
99 }  // namespace ns3
100