1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2008 INRIA
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: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
19  */
20 #include "on-off-helper.h"
21 #include "ns3/inet-socket-address.h"
22 #include "ns3/packet-socket-address.h"
23 #include "ns3/string.h"
24 #include "ns3/data-rate.h"
25 #include "ns3/uinteger.h"
26 #include "ns3/names.h"
27 #include "ns3/random-variable-stream.h"
28 #include "ns3/onoff-application.h"
29 
30 namespace ns3 {
31 
OnOffHelper(std::string protocol,Address address)32 OnOffHelper::OnOffHelper (std::string protocol, Address address)
33 {
34   m_factory.SetTypeId ("ns3::OnOffApplication");
35   m_factory.Set ("Protocol", StringValue (protocol));
36   m_factory.Set ("Remote", AddressValue (address));
37 }
38 
39 void
SetAttribute(std::string name,const AttributeValue & value)40 OnOffHelper::SetAttribute (std::string name, const AttributeValue &value)
41 {
42   m_factory.Set (name, value);
43 }
44 
45 ApplicationContainer
Install(Ptr<Node> node) const46 OnOffHelper::Install (Ptr<Node> node) const
47 {
48   return ApplicationContainer (InstallPriv (node));
49 }
50 
51 ApplicationContainer
Install(std::string nodeName) const52 OnOffHelper::Install (std::string nodeName) const
53 {
54   Ptr<Node> node = Names::Find<Node> (nodeName);
55   return ApplicationContainer (InstallPriv (node));
56 }
57 
58 ApplicationContainer
Install(NodeContainer c) const59 OnOffHelper::Install (NodeContainer c) const
60 {
61   ApplicationContainer apps;
62   for (NodeContainer::Iterator i = c.Begin (); i != c.End (); ++i)
63     {
64       apps.Add (InstallPriv (*i));
65     }
66 
67   return apps;
68 }
69 
70 Ptr<Application>
InstallPriv(Ptr<Node> node) const71 OnOffHelper::InstallPriv (Ptr<Node> node) const
72 {
73   Ptr<Application> app = m_factory.Create<Application> ();
74   node->AddApplication (app);
75 
76   return app;
77 }
78 
79 int64_t
AssignStreams(NodeContainer c,int64_t stream)80 OnOffHelper::AssignStreams (NodeContainer c, int64_t stream)
81 {
82   int64_t currentStream = stream;
83   Ptr<Node> node;
84   for (NodeContainer::Iterator i = c.Begin (); i != c.End (); ++i)
85     {
86       node = (*i);
87       for (uint32_t j = 0; j < node->GetNApplications (); j++)
88         {
89           Ptr<OnOffApplication> onoff = DynamicCast<OnOffApplication> (node->GetApplication (j));
90           if (onoff)
91             {
92               currentStream += onoff->AssignStreams (currentStream);
93             }
94         }
95     }
96   return (currentStream - stream);
97 }
98 
99 void
SetConstantRate(DataRate dataRate,uint32_t packetSize)100 OnOffHelper::SetConstantRate (DataRate dataRate, uint32_t packetSize)
101 {
102   m_factory.Set ("OnTime", StringValue ("ns3::ConstantRandomVariable[Constant=1000]"));
103   m_factory.Set ("OffTime", StringValue ("ns3::ConstantRandomVariable[Constant=0]"));
104   m_factory.Set ("DataRate", DataRateValue (dataRate));
105   m_factory.Set ("PacketSize", UintegerValue (packetSize));
106 }
107 
108 } // namespace ns3
109