Files
unison/src/visualizer/model/visual-simulator-impl.cc

234 lines
4.9 KiB
C++
Raw Normal View History

/*
* Copyright (c) 2010 Gustavo Carneiro
*
* 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
*
* Author: Gustavo Carneiro <gjcarneiro@gmail.com> <gjc@inescporto.pt>
*/
#include <Python.h>
#undef HAVE_PTHREAD_H
#undef HAVE_SYS_STAT_H
#include "visual-simulator-impl.h"
2022-10-07 20:08:35 +00:00
#include "ns3/default-simulator-impl.h"
#include "ns3/log.h"
#include "ns3/packet-metadata.h"
2022-10-07 20:08:35 +00:00
namespace ns3
{
2022-10-07 20:08:35 +00:00
NS_LOG_COMPONENT_DEFINE("VisualSimulatorImpl");
2022-10-07 20:08:35 +00:00
NS_OBJECT_ENSURE_REGISTERED(VisualSimulatorImpl);
namespace
2011-05-13 15:01:44 -04:00
{
2022-01-05 20:58:23 -06:00
/**
* Get an object factory configured to the default simulator implementation
* \return an object factory.
*/
2011-05-13 15:01:44 -04:00
ObjectFactory
2022-10-07 20:08:35 +00:00
GetDefaultSimulatorImplFactory()
2011-05-13 15:01:44 -04:00
{
2022-10-07 20:08:35 +00:00
ObjectFactory factory;
factory.SetTypeId(DefaultSimulatorImpl::GetTypeId());
return factory;
2011-05-13 15:01:44 -04:00
}
2022-10-07 20:08:35 +00:00
} // namespace
TypeId
2022-10-07 20:08:35 +00:00
VisualSimulatorImpl::GetTypeId(void)
{
2022-10-07 20:08:35 +00:00
static TypeId tid =
TypeId("ns3::VisualSimulatorImpl")
.SetParent<SimulatorImpl>()
.SetGroupName("Visualizer")
.AddConstructor<VisualSimulatorImpl>()
.AddAttribute(
"SimulatorImplFactory",
"Factory for the underlying simulator implementation used by the visualizer.",
ObjectFactoryValue(GetDefaultSimulatorImplFactory()),
MakeObjectFactoryAccessor(&VisualSimulatorImpl::m_simulatorImplFactory),
MakeObjectFactoryChecker());
return tid;
}
2022-10-07 20:08:35 +00:00
VisualSimulatorImpl::VisualSimulatorImpl()
{
2022-10-07 20:08:35 +00:00
PacketMetadata::Enable();
}
2022-10-07 20:08:35 +00:00
VisualSimulatorImpl::~VisualSimulatorImpl()
2011-05-13 15:01:44 -04:00
{
}
2011-05-13 15:01:44 -04:00
void
2022-10-07 20:08:35 +00:00
VisualSimulatorImpl::DoDispose(void)
{
2022-10-07 20:08:35 +00:00
if (m_simulator)
{
2022-10-07 20:08:35 +00:00
m_simulator->Dispose();
m_simulator = NULL;
}
2022-10-07 20:08:35 +00:00
SimulatorImpl::DoDispose();
}
void
2022-10-07 20:08:35 +00:00
VisualSimulatorImpl::NotifyConstructionCompleted()
{
2022-10-07 20:08:35 +00:00
m_simulator = m_simulatorImplFactory.Create<SimulatorImpl>();
}
void
2022-10-07 20:08:35 +00:00
VisualSimulatorImpl::Destroy()
{
2022-10-07 20:08:35 +00:00
m_simulator->Destroy();
}
void
2022-10-07 20:08:35 +00:00
VisualSimulatorImpl::SetScheduler(ObjectFactory schedulerFactory)
{
2022-10-07 20:08:35 +00:00
m_simulator->SetScheduler(schedulerFactory);
}
// System ID for non-distributed simulation is always zero
uint32_t
2022-10-07 20:08:35 +00:00
VisualSimulatorImpl::GetSystemId(void) const
{
2022-10-07 20:08:35 +00:00
return m_simulator->GetSystemId();
}
bool
2022-10-07 20:08:35 +00:00
VisualSimulatorImpl::IsFinished(void) const
{
2022-10-07 20:08:35 +00:00
return m_simulator->IsFinished();
}
void
2022-10-07 20:08:35 +00:00
VisualSimulatorImpl::Run(void)
{
2022-10-07 20:08:35 +00:00
if (!Py_IsInitialized())
{
2022-10-07 20:08:35 +00:00
const wchar_t* argv[] = {L"python", NULL};
Py_Initialize();
PySys_SetArgv(1, (wchar_t**)argv);
PyRun_SimpleString("import visualizer\n"
"visualizer.start();\n");
2012-04-24 17:29:28 +01:00
}
2022-10-07 20:08:35 +00:00
else
2012-04-24 17:29:28 +01:00
{
2022-10-07 20:08:35 +00:00
PyGILState_STATE __py_gil_state = PyGILState_Ensure();
2022-10-07 20:08:35 +00:00
PyRun_SimpleString("import visualizer\n"
"visualizer.start();\n");
2012-04-24 17:29:28 +01:00
2022-10-07 20:08:35 +00:00
PyGILState_Release(__py_gil_state);
}
}
void
2022-10-07 20:08:35 +00:00
VisualSimulatorImpl::Stop(void)
{
2022-10-07 20:08:35 +00:00
m_simulator->Stop();
}
void
2022-10-07 20:08:35 +00:00
VisualSimulatorImpl::Stop(const Time& delay)
{
2022-10-07 20:08:35 +00:00
m_simulator->Stop(delay);
}
//
// Schedule an event for a _relative_ time in the future.
//
EventId
2022-10-07 20:08:35 +00:00
VisualSimulatorImpl::Schedule(const Time& delay, EventImpl* event)
{
2022-10-07 20:08:35 +00:00
return m_simulator->Schedule(delay, event);
}
void
2022-10-07 20:08:35 +00:00
VisualSimulatorImpl::ScheduleWithContext(uint32_t context, const Time& delay, EventImpl* event)
{
2022-10-07 20:08:35 +00:00
m_simulator->ScheduleWithContext(context, delay, event);
}
EventId
2022-10-07 20:08:35 +00:00
VisualSimulatorImpl::ScheduleNow(EventImpl* event)
{
2022-10-07 20:08:35 +00:00
return m_simulator->ScheduleNow(event);
}
EventId
2022-10-07 20:08:35 +00:00
VisualSimulatorImpl::ScheduleDestroy(EventImpl* event)
{
2022-10-07 20:08:35 +00:00
return m_simulator->ScheduleDestroy(event);
}
Time
2022-10-07 20:08:35 +00:00
VisualSimulatorImpl::Now(void) const
{
2022-10-07 20:08:35 +00:00
return m_simulator->Now();
}
Time
2022-10-07 20:08:35 +00:00
VisualSimulatorImpl::GetDelayLeft(const EventId& id) const
{
2022-10-07 20:08:35 +00:00
return m_simulator->GetDelayLeft(id);
}
void
2022-10-07 20:08:35 +00:00
VisualSimulatorImpl::Remove(const EventId& id)
{
2022-10-07 20:08:35 +00:00
m_simulator->Remove(id);
}
void
2022-10-07 20:08:35 +00:00
VisualSimulatorImpl::Cancel(const EventId& id)
{
2022-10-07 20:08:35 +00:00
m_simulator->Cancel(id);
}
bool
2022-10-07 20:08:35 +00:00
VisualSimulatorImpl::IsExpired(const EventId& id) const
{
2022-10-07 20:08:35 +00:00
return m_simulator->IsExpired(id);
}
Time
2022-10-07 20:08:35 +00:00
VisualSimulatorImpl::GetMaximumSimulationTime(void) const
{
2022-10-07 20:08:35 +00:00
return m_simulator->GetMaximumSimulationTime();
}
uint32_t
2022-10-07 20:08:35 +00:00
VisualSimulatorImpl::GetContext(void) const
{
2022-10-07 20:08:35 +00:00
return m_simulator->GetContext();
}
2018-10-19 00:59:43 -04:00
uint64_t
2022-10-07 20:08:35 +00:00
VisualSimulatorImpl::GetEventCount(void) const
2018-10-19 00:59:43 -04:00
{
2022-10-07 20:08:35 +00:00
return m_simulator->GetEventCount();
2018-10-19 00:59:43 -04:00
}
void
2022-10-07 20:08:35 +00:00
VisualSimulatorImpl::RunRealSimulator(void)
{
2022-10-07 20:08:35 +00:00
m_simulator->Run();
}
} // namespace ns3