ns-3 Direct Code Execution
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
dce-node-context.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2011 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: Frederic Urbani <frederic.urbani@inria.fr>
19  *
20  */
21 #include "dce-node-context.h"
22 #include "ns3/log.h"
23 #include "ns3/object.h"
24 #include "ns3/ptr.h"
25 #include "ns3/node.h"
26 #include "ns3/node-list.h"
27 #include "ns3/names.h"
28 #include "utils.h"
29 #include <string.h>
30 #include "process.h"
31 #include "dce-manager.h"
32 
33 NS_LOG_COMPONENT_DEFINE ("DceNodeContext");
34 
35 namespace ns3 {
36 
37 NS_OBJECT_ENSURE_REGISTERED (DceNodeContext);
38 
39 TypeId
41 {
42  static TypeId tid = TypeId ("ns3::DceNodeContext").SetParent<Object> ()
43  .AddConstructor<DceNodeContext> ();
44 
45  return tid;
46 }
47 TypeId
49 {
50  return DceNodeContext::GetTypeId ();
51 }
53 {
54  m_randomCtx = NormalVariable (0, 2 ^ 32 - 1);
55  m_rndBuffer = m_randomCtx.GetInteger ();
56  m_rndOffset = 0;
57 }
58 
60 {
61 }
62 
63 int
64 DceNodeContext::UName (struct utsname *buf)
65 {
66  if (0 == m_sysName.length ())
67  {
68  uint32_t nodeId = UtilsGetNodeId ();
69  Ptr<Node> node = NodeList::GetNode (nodeId);
70  NS_ASSERT (node != 0);
71  std::string nn = Names::FindName (node);
72  std::ostringstream oss;
73 
74  if (nn.length () == 0)
75  {
76  oss << "NODE_" << nodeId;
77  nn = oss.str ();
78 
79  oss.str ("");
80  oss.clear ();
81  }
82  m_sysName = nn + "'s OS";
83  m_nodeName = nn;
84  m_release = "3"; // XXX
85  m_version = "12"; // XXX
86  oss << nodeId;
87  m_hardId = oss.str ();
88  }
89  memset (buf, 0, sizeof (struct utsname));
90 
91  memcpy (buf->sysname, m_sysName.c_str (), std::min ((int) m_sysName.length (), 64));
92  memcpy (buf->nodename, m_nodeName.c_str (), std::min ((int) m_nodeName.length (), 64));
93  memcpy (buf->release, m_release.c_str (), std::min ((int) m_release.length (), 64));
94  memcpy (buf->version, m_version.c_str (), std::min ((int) m_version.length (), 64));
95  memcpy (buf->machine, m_hardId.c_str (), std::min ((int) m_hardId.length (), 64));
96 
97  return 0;
98 }
99 
100 Ptr<DceNodeContext>
102 {
103  Thread *current = Current ();
104  NS_LOG_FUNCTION (current);
105  NS_ASSERT (current != 0);
106  DceManager *manager = current->process->manager;
107  NS_ASSERT (manager != 0);
108 
109  return manager->GetObject<DceNodeContext> ();
110 }
111 
112 int
113 DceNodeContext::RandomRead (void *buf, size_t count)
114 {
115  uint8_t *crsr = (uint8_t*)buf;
116  for (uint32_t i = 0; i < count; i++)
117  {
118  *crsr++ = GetNextRnd ();
119  }
120  return count;
121 }
122 
123 uint8_t
125 {
126  uint8_t v = ((uint8_t *) &m_rndBuffer) [ m_rndOffset++ ];
127  if (m_rndOffset >= 4)
128  {
129  m_rndOffset = 0;
130  m_rndBuffer = m_randomCtx.GetInteger ();
131  }
132  return v;
133 }
134 
135 } // namespace ns3