View | Details | Raw Unified | Return to bug 2410
Collapse All | Expand All

(-)548f801bd282 (+142 lines)
Added Link Here 
1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2
/*
3
 * This program is free software; you can redistribute it and/or modify
4
 * it under the terms of the GNU General Public License version 2 as
5
 * published by the Free Software Foundation;
6
 *
7
 * This program is distributed in the hope that it will be useful,
8
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
 * GNU General Public License for more details.
11
 *
12
 * You should have received a copy of the GNU General Public License
13
 * along with this program; if not, write to the Free Software
14
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
15
 *
16
 * Author: Hossam Khader <hossamkhader@gmail.com>
17
 */
18
19
#include "ns3/core-module.h"
20
#include "ns3/node-container.h"
21
#include "ns3/mobility-helper.h"
22
#include "ns3/mobility-model.h"
23
#include "ns3/basic-energy-source-helper.h"
24
#include "ns3/energy-source-container.h"
25
#include "ns3/uan-helper.h"
26
#include "ns3/uan-channel.h"
27
#include "ns3/acoustic-modem-energy-model-helper.h"
28
#include "ns3/uan-application-helper.h"
29
#include "ns3/uan-application.h"
30
31
using namespace ns3;
32
33
NS_LOG_COMPONENT_DEFINE ("UanApplicationExample");
34
35
void SetupPositions ();
36
void SetupEnergy ();
37
void SetupCommunications ();
38
void PrintReceivedPacket (UanAddress src, Buffer data);
39
void SetupApplications ();
40
void SendPacket ();
41
void Run ();
42
43
NodeContainer nodeContainer;
44
45
void
46
SetupPositions ()
47
{
48
  MobilityHelper mobilityHelper;
49
  mobilityHelper.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
50
  mobilityHelper.Install (nodeContainer);
51
  nodeContainer.Get (0)->GetObject<MobilityModel> ()->SetPosition (Vector (0, 0, 0));
52
  nodeContainer.Get (1)->GetObject<MobilityModel> ()->SetPosition (Vector (100, 0, 0));
53
  nodeContainer.Get (2)->GetObject<MobilityModel> ()->SetPosition (Vector (-100, 0, 0));
54
}
55
56
void
57
SetupEnergy ()
58
{
59
  BasicEnergySourceHelper energySourceHelper;
60
  energySourceHelper.Set ("BasicEnergySourceInitialEnergyJ", DoubleValue (900000));
61
  energySourceHelper.Install (nodeContainer);
62
}
63
64
void
65
SetupCommunications ()
66
{
67
  Ptr<UanChannel> channel = CreateObject<UanChannel> ();
68
  UanHelper uanHelper;
69
  NetDeviceContainer netDeviceContainer = uanHelper.Install (nodeContainer, channel);
70
  EnergySourceContainer energySourceContainer;
71
  NodeContainer::Iterator iterator = nodeContainer.Begin ();
72
  while (iterator != nodeContainer.End ())
73
    {
74
      energySourceContainer.Add ((*iterator)->GetObject<EnergySourceContainer> ()->Get (0));
75
      iterator++;
76
    }
77
  AcousticModemEnergyModelHelper acousticModemEnergyModelHelper;
78
  acousticModemEnergyModelHelper.Install (netDeviceContainer, energySourceContainer);
79
}
80
81
void
82
PrintReceivedPacket (UanAddress src, Buffer data)
83
{
84
  std::cout << Simulator::Now ().GetSeconds () << "|" << "Node#" << (int)(src.GetAsInt ()) << "|Energy:" << (int)((data.PeekData ())[0]) << "%" << std::endl;
85
}
86
87
void
88
SetupApplications ()
89
{
90
  UanApplicationHelper uanApplicationHelper;
91
  ApplicationContainer applicationContainer;
92
  applicationContainer = uanApplicationHelper.Install (nodeContainer);
93
  applicationContainer.Start (Seconds (0));
94
  NodeContainer::Iterator node = nodeContainer.Begin ();
95
  while (node != nodeContainer.End ())
96
    {
97
      Ptr<UanApplication> uanApplication = (*node)->GetApplication (0)->GetObject<UanApplication> ();
98
      uanApplication->SetPacketReceivedCallback (MakeCallback<void, UanAddress, Buffer>(&PrintReceivedPacket));
99
      node++;
100
    }
101
}
102
103
void
104
SendPacket ()
105
{
106
  NodeContainer::Iterator node = nodeContainer.Begin ();
107
  while (node != nodeContainer.End ())
108
    {
109
      uint8_t energy = ((*node)->GetObject<EnergySourceContainer> ()->Get (0)->GetEnergyFraction ()) * 100;
110
      Ptr<UanApplication> uanApplication = (*node)->GetApplication (0)->GetObject<UanApplication> ();
111
      Buffer dataBuffer;
112
      Buffer::Iterator i = dataBuffer.Begin ();
113
      i.Write (&energy, 1);
114
      Simulator::Schedule (Seconds ((*node)->GetId ()), &UanApplication::Send, uanApplication, UanAddress (0), dataBuffer);
115
      node++;
116
    }
117
  Simulator::Schedule (Seconds (5), &SendPacket);
118
}
119
120
void
121
Run ()
122
{
123
  nodeContainer.Create (3);
124
  SetupPositions ();
125
  SetupEnergy ();
126
  SetupCommunications ();
127
  SetupApplications ();
128
  SendPacket ();
129
}
130
131
int
132
main (int argc, char *argv[])
133
{
134
  CommandLine cmd;
135
  cmd.Parse (argc, argv);
136
  Run ();
137
  Simulator::Stop (Hours (24));
138
  Simulator::Run ();
139
  Simulator::Destroy ();
140
141
  return 0;
142
}
(-)a/src/uan/examples/wscript (+4 lines)
 Lines 6-8    Link Here 
6
6
7
    obj = bld.create_ns3_program('uan-rc-example', ['internet', 'mobility', 'stats', 'applications', 'uan'])
7
    obj = bld.create_ns3_program('uan-rc-example', ['internet', 'mobility', 'stats', 'applications', 'uan'])
8
    obj.source = 'uan-rc-example.cc'
8
    obj.source = 'uan-rc-example.cc'
9
	
10
	obj = bld.create_ns3_program('uan-application-example', ['internet', 'mobility', 'stats', 'applications', 'uan'])
11
    obj.source = 'uan-application-example.cc'
12
	
(-)548f801bd282 (+81 lines)
Added Link Here 
1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2
/*
3
 * This program is free software; you can redistribute it and/or modify
4
 * it under the terms of the GNU General Public License version 2 as
5
 * published by the Free Software Foundation;
6
 *
7
 * This program is distributed in the hope that it will be useful,
8
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
 * GNU General Public License for more details.
11
 *
12
 * You should have received a copy of the GNU General Public License
13
 * along with this program; if not, write to the Free Software
14
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
15
 * 
16
 * Author: Hossam Khader <hossamkhader@gmail.com>
17
 */
18
19
#include "uan-application-helper.h"
20
#include "ns3/names.h"
21
#include "ns3/packet-socket-factory.h"
22
#include "ns3/packet-socket-helper.h"
23
#include "ns3/net-device.h"
24
#include "ns3/address.h"
25
#include "ns3/uan-application.h"
26
27
28
namespace ns3 {
29
30
UanApplicationHelper::UanApplicationHelper ()
31
{
32
  m_factory.SetTypeId ("ns3::UanApplication");
33
}
34
35
void 
36
UanApplicationHelper::SetAttribute (std::string name, const AttributeValue &value)
37
{
38
  m_factory.Set (name, value);
39
}
40
41
ApplicationContainer
42
UanApplicationHelper::Install (Ptr<Node> node) const
43
{
44
  return ApplicationContainer (InstallPriv (node));
45
}
46
47
ApplicationContainer
48
UanApplicationHelper::Install (std::string nodeName) const
49
{
50
  Ptr<Node> node = Names::Find<Node> (nodeName);
51
  return ApplicationContainer (InstallPriv (node));
52
}
53
54
ApplicationContainer
55
UanApplicationHelper::Install (NodeContainer c) const
56
{
57
  ApplicationContainer apps;
58
  for (NodeContainer::Iterator i = c.Begin (); i != c.End (); ++i)
59
    {
60
      apps.Add (InstallPriv (*i));
61
    }
62
63
  return apps;
64
}
65
66
Ptr<Application>
67
UanApplicationHelper::InstallPriv (Ptr<Node> node) const
68
{
69
  if(node->GetObject<PacketSocketFactory> () == NULL)
70
    {
71
      PacketSocketHelper packetSocketHelper;
72
      packetSocketHelper.Install (node);
73
    }
74
75
  Ptr<Application> app = m_factory.Create<Application> ();
76
  node->AddApplication (app);
77
78
  return app;
79
}
80
81
} // namespace ns3
(-)548f801bd282 (+92 lines)
Added Link Here 
1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2
/*
3
 * This program is free software; you can redistribute it and/or modify
4
 * it under the terms of the GNU General Public License version 2 as
5
 * published by the Free Software Foundation;
6
 *
7
 * This program is distributed in the hope that it will be useful,
8
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
 * GNU General Public License for more details.
11
 *
12
 * You should have received a copy of the GNU General Public License
13
 * along with this program; if not, write to the Free Software
14
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
15
 * 
16
 * Author: Hossam Khader <hossamkhader@gmail.com>
17
 */
18
19
#ifndef UANAPPLICATION_HELPER_H
20
#define UANAPPLICATION_HELPER_H
21
22
23
#include "ns3/object-factory.h"
24
#include "ns3/attribute.h"
25
#include "ns3/node-container.h"
26
#include "ns3/application-container.h"
27
28
namespace ns3 {
29
30
/**
31
 * \ingroup uan
32
 * \brief A helper to make it easier to instantiate an ns3::UanApplication 
33
 * on a set of nodes.
34
 */
35
class UanApplicationHelper
36
{
37
public:
38
  /**
39
   * Create an UanApplicationHelper to make it easier to work with UanApplication
40
   */
41
  UanApplicationHelper ();
42
43
  /**
44
   * Helper function used to set the underlying application attributes.
45
   */
46
  void SetAttribute (std::string name, const AttributeValue &value);
47
48
  /**
49
   * Install an ns3::UanApplication on each node of the input container
50
   * configured with all the attributes set with SetAttribute.
51
   *
52
   * \param c NodeContainer of the set of nodes on which an UanApplication 
53
   * will be installed.
54
   * \returns Container of Ptr to the applications installed.
55
   */
56
  ApplicationContainer Install (NodeContainer c) const;
57
58
  /**
59
   * Install an ns3::UanApplication on the node configured with all the 
60
   * attributes set with SetAttribute.
61
   *
62
   * \param node The node on which an UanApplication will be installed.
63
   * \returns Container of Ptr to the applications installed.
64
   */
65
  ApplicationContainer Install (Ptr<Node> node) const;
66
67
  /**
68
   * Install an ns3::UanApplication on the node configured with all the 
69
   * attributes set with SetAttribute.
70
   *
71
   * \param nodeName The node on which an UanApplication will be installed.
72
   * \returns Container of Ptr to the applications installed.
73
   */
74
  ApplicationContainer Install (std::string nodeName) const;
75
76
private:
77
  /**
78
   * Install an ns3::UanApplication on the node configured with all the 
79
   * attributes set with SetAttribute.
80
   *
81
   * \param node The node on which an UanApplication will be installed.
82
   * \returns Ptr to the application installed.
83
   */
84
  Ptr<Application> InstallPriv (Ptr<Node> node) const;
85
86
  ObjectFactory m_factory; //!< Object factory.
87
};
88
89
} // namespace ns3
90
91
#endif /* UANAPPLICATION_HELPER_H */
92
(-)548f801bd282 (+188 lines)
Added Link Here 
1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2
/*
3
 * This program is free software; you can redistribute it and/or modify
4
 * it under the terms of the GNU General Public License version 2 as
5
 * published by the Free Software Foundation;
6
 *
7
 * This program is distributed in the hope that it will be useful,
8
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
 * GNU General Public License for more details.
11
 *
12
 * You should have received a copy of the GNU General Public License
13
 * along with this program; if not, write to the Free Software
14
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
15
 * 
16
 * Author: Hossam Khader <hossamkhader@gmail.com>
17
 */
18
#include "uan-application.h"
19
#include "ns3/log.h"
20
#include "ns3/socket-factory.h"
21
#include "ns3/packet-socket-address.h"
22
#include "ns3/packet-socket.h"
23
24
namespace ns3 {
25
26
NS_LOG_COMPONENT_DEFINE ("UanApplication");
27
28
NS_OBJECT_ENSURE_REGISTERED (UanApplication);
29
30
TypeId 
31
UanApplication::GetTypeId (void)
32
{
33
  static TypeId tid = TypeId ("ns3::UanApplication")
34
    .SetParent<Application> ()
35
    .SetGroupName ("Applications")
36
    .AddConstructor<UanApplication> ()
37
  ;
38
  return tid;
39
}
40
41
UanApplication::UanApplication ()
42
{
43
  NS_LOG_FUNCTION (this);
44
  m_socket = NULL;
45
  m_packetReceivedCallback.Nullify ();
46
}
47
UanApplication::~UanApplication ()
48
{
49
  NS_LOG_FUNCTION (this);
50
}
51
52
void
53
UanApplication::DoDispose (void)
54
{
55
  NS_LOG_FUNCTION (this);
56
  m_socket = NULL;
57
  m_packetReceivedCallback.Nullify ();
58
  Application::DoDispose ();
59
}
60
61
uint32_t
62
UanApplication::GetApplicationId (void) const
63
{
64
  NS_LOG_FUNCTION (this);
65
  Ptr<Node> node = GetNode ();
66
  for (uint32_t i = 0; i < node->GetNApplications (); ++i)
67
    {
68
      if (node->GetApplication (i) == this)
69
        {
70
          return i;
71
        }
72
    }
73
  NS_ASSERT_MSG (false, "forgot to add application to node");
74
  return 0;
75
}
76
77
78
void 
79
UanApplication::Send (UanAddress dst, const Buffer dataBuffer)
80
{
81
  NS_LOG_FUNCTION (this);
82
  Address address = GetNode ()->GetDevice (0)->GetAddress ();
83
  uint8_t *address_buffer = new uint8_t[address.GetLength ()];
84
  address.CopyTo (address_buffer);
85
  UanAddress local_address = UanAddress (address_buffer[address.GetLength () -1]);
86
  delete [] address_buffer;
87
88
  const uint8_t * data = dataBuffer.PeekData ();
89
90
  if(dst.GetAsInt () != local_address.GetAsInt ())
91
    { 
92
      PacketSocketAddress socketAddress;
93
      socketAddress.SetSingleDevice (GetNode ()->GetDevice (0)->GetIfIndex ());
94
      socketAddress.SetPhysicalAddress (dst);
95
      socketAddress.SetProtocol (0);
96
      Ptr<Socket> socket = Socket::CreateSocket (GetNode (), TypeId::LookupByName ("ns3::PacketSocketFactory"));
97
      Ptr<Packet> p = Create<Packet> (data, dataBuffer.GetSize () + 1);
98
      NS_ASSERT (socket != NULL);
99
      socket->Bind ();
100
      socket->Connect (socketAddress);
101
      socket->Send (p);
102
      socket->Close ();
103
    }
104
}
105
106
void
107
UanApplication::Receive (Ptr<Socket> socket)
108
{
109
  NS_LOG_FUNCTION (this << socket);
110
  NS_ASSERT (m_socket != NULL);
111
  while (m_socket->GetRxAvailable () > 0)
112
    {
113
      Ptr<Packet> p = m_socket->Recv ();
114
      uint8_t *buffer = new uint8_t[p->GetSize ()];
115
      p->CopyData (buffer, p->GetSize ());
116
      UanAddress src_address;
117
      PacketTagIterator i = p->GetPacketTagIterator ();
118
      while (i.HasNext ())
119
        {
120
          PacketTagIterator::Item item = i.Next ();
121
          if (item.GetTypeId () == TypeId::LookupByName ("ns3::SocketAddressTag"))
122
            {
123
              SocketAddressTag tag;
124
              item.GetTag (tag);
125
              Address address = tag.GetAddress ();
126
              uint8_t *address_buffer = new uint8_t[address.GetLength ()];
127
              address.CopyTo (address_buffer);
128
              src_address = UanAddress (address_buffer[address.GetLength () -1]);
129
              delete [] address_buffer;
130
            }
131
        }
132
      if(!m_packetReceivedCallback.IsNull ())
133
        {
134
          Buffer dataBuffer;
135
          Buffer::Iterator iterator = dataBuffer.Begin ();
136
          iterator.Write (buffer, p->GetSize ());
137
          m_packetReceivedCallback (src_address, dataBuffer);
138
        }
139
      delete[] buffer;
140
    }
141
142
} 
143
144
void 
145
UanApplication::StartApplication (void)
146
{
147
  NS_LOG_FUNCTION (this);
148
  PacketSocketAddress socketAddress;
149
  socketAddress.SetSingleDevice (GetNode ()->GetDevice (0)->GetIfIndex ());
150
  socketAddress.SetPhysicalAddress (UanAddress (UanAddress::GetBroadcast ()));
151
  socketAddress.SetProtocol (0);
152
  if (!m_socket)
153
    {
154
      m_socket = Socket::CreateSocket (GetNode (), TypeId::LookupByName ("ns3::PacketSocketFactory"));
155
    }
156
  m_socket->Bind ();
157
  m_socket->Connect (socketAddress);
158
  m_socket->SetRecvCallback (MakeCallback (&UanApplication::Receive, this));
159
}
160
 
161
void 
162
UanApplication::StopApplication (void)
163
{
164
  NS_LOG_FUNCTION (this);
165
  if(m_socket != NULL)
166
    {
167
      m_socket->Close ();
168
    }
169
  else
170
    {
171
      NS_LOG_WARN ("UanApplication found null socket to close in StopApplication");
172
    }
173
}
174
175
void
176
UanApplication::SetPacketReceivedCallback (PacketReceivedCallback callback)
177
{
178
  NS_LOG_FUNCTION (this);
179
  if (callback.IsNull ())
180
    {
181
      NS_LOG_DEBUG ("AuvWaypointMobilityModel:Setting NULL packet received callback!");
182
    }
183
  m_packetReceivedCallback = callback;
184
}
185
186
187
188
} // namespace ns3
(-)548f801bd282 (+91 lines)
Added Link Here 
1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2
/*
3
 * This program is free software; you can redistribute it and/or modify
4
 * it under the terms of the GNU General Public License version 2 as
5
 * published by the Free Software Foundation;
6
 *
7
 * This program is distributed in the hope that it will be useful,
8
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
 * GNU General Public License for more details.
11
 *
12
 * You should have received a copy of the GNU General Public License
13
 * along with this program; if not, write to the Free Software
14
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
15
 * 
16
 * Author: Hossam Khader <hossamkhader@gmail.com>
17
 */
18
19
#ifndef UANAPPLICATION_H
20
#define UANAPPLICATION_H
21
22
23
#include "ns3/application.h"
24
#include "ns3/packet.h"
25
#include "ns3/uan-address.h"
26
#include "ns3/socket.h"
27
28
namespace ns3 {
29
30
/**
31
 * \ingroup uan
32
 * \brief a simple application to be used in UAN networks
33
 */
34
class UanApplication : public Application
35
{
36
public:
37
38
  typedef Callback<void, UanAddress, Buffer> PacketReceivedCallback;
39
40
  /**
41
   * \brief Get the type ID.
42
   * \return the object TypeId
43
   */
44
  static TypeId GetTypeId (void);
45
46
  /**
47
   * create a uan application
48
   */
49
  UanApplication ();
50
  virtual ~UanApplication ();
51
52
  /**
53
   * \brief Send a packet to the destination
54
   * \param dst the destination UAN address
55
   * \param dataBuffer the data to be sent
56
   */
57
  void Send (UanAddress dst, const Buffer dataBuffer);
58
59
  /**
60
   * \brief Set a callback when a packet is received
61
   * \param callback the packet received callback
62
   */
63
  void SetPacketReceivedCallback (PacketReceivedCallback callback);
64
65
private:
66
67
  // inherited from Application base class.
68
  virtual void StartApplication (void);
69
  virtual void StopApplication (void);
70
  virtual void DoDispose (void);
71
  /**
72
   * \brief Return the application ID in the node.
73
   * \returns the application id
74
   */
75
  uint32_t GetApplicationId (void) const;
76
  /**
77
   * \brief Receive a packet
78
   * \param socket the receiving socket
79
   *
80
   * This function is called by lower layers through a callback.
81
   */
82
  void Receive (Ptr<Socket> socket);
83
84
  Ptr<Socket> m_socket; //!< Associated socket
85
  PacketReceivedCallback m_packetReceivedCallback;
86
87
};
88
89
} // namespace ns3
90
91
#endif /* UANAPPLICATION_H */
(-)a/src/uan/wscript (-1 / +5 lines)
 Lines 25-32    Link Here 
25
        'model/uan-phy.cc',
25
        'model/uan-phy.cc',
26
        'model/uan-noise-model.cc',
26
        'model/uan-noise-model.cc',
27
        'model/acoustic-modem-energy-model.cc',
27
        'model/acoustic-modem-energy-model.cc',
28
		'model/uan-application.cc',
28
        'helper/uan-helper.cc',
29
        'helper/uan-helper.cc',
29
        'helper/acoustic-modem-energy-model-helper.cc',
30
        'helper/acoustic-modem-energy-model-helper.cc',
31
		'helper/uan-application-helper.cc',
30
        ]
32
        ]
31
33
32
    module_test = bld.create_ns3_module_test_library('uan')
34
    module_test = bld.create_ns3_module_test_library('uan')
 Lines 58-66    Link Here 
58
        'model/uan-header-rc.h',
60
        'model/uan-header-rc.h',
59
        'model/uan-mac-rc.h',
61
        'model/uan-mac-rc.h',
60
        'model/acoustic-modem-energy-model.h',
62
        'model/acoustic-modem-energy-model.h',
63
		'model/uan-application.h',
64
		'model/uan-mac-rc-gw.h',
61
        'helper/uan-helper.h',
65
        'helper/uan-helper.h',
62
        'helper/acoustic-modem-energy-model-helper.h',
66
        'helper/acoustic-modem-energy-model-helper.h',
63
        'model/uan-mac-rc-gw.h',
67
		'helper/uan-application-helper.h',
64
        ]
68
        ]
65
69
66
    if (bld.env['ENABLE_EXAMPLES']):
70
    if (bld.env['ENABLE_EXAMPLES']):

Return to bug 2410