Commit f0f563ba authored by Ulrich Kemloh's avatar Ulrich Kemloh

documentation

parent f61f7ef0
......@@ -143,7 +143,8 @@ bool Simulation::InitArgs(const ArgumentParser& args)
s.append("\tonline streaming enabled \n");
}
if (args.GetTrajectoriesFile().empty() == false) {
if (args.GetTrajectoriesFile().empty() == false)
{
switch (args.GetFileFormat())
{
case FORMAT_XML_PLAIN: {
......@@ -192,8 +193,6 @@ bool Simulation::InitArgs(const ArgumentParser& args)
}
}
_operationalModel = args.GetModel();
s.append(_operationalModel->GetDescription());
......@@ -229,10 +228,10 @@ bool Simulation::InitArgs(const ArgumentParser& args)
}
#ifdef _USE_PROTOCOL_BUFFER
//iniitalize the hybridmode if defined
//initialize the hybrid mode if defined
if(nullptr!=(_hybridSimManager=args.GetHybridSimManager()))
{
_hybridSimManager->Init(_building.get());
Log->Write("INFO:\t performing hybrid simulation");
}
#endif
......@@ -248,7 +247,7 @@ bool Simulation::InitArgs(const ArgumentParser& args)
ped->Setdt(_deltaT);
}
_nPeds = allPeds.size();
//pBuilding->WriteToErrorLog();
//_building->WriteToErrorLog();
//get the seed
_seed = args.GetSeed();
......@@ -284,7 +283,6 @@ int Simulation::RunStandardSimulation(double maxSimTime)
double t=RunBody(maxSimTime);
RunFooter();
return (int)t;
}
void Simulation::UpdateRoutesAndLocations()
......@@ -387,19 +385,19 @@ void Simulation::UpdateRoutesAndLocations()
}
}
#ifdef _USE_PROTOCOL_BUFFER
if (_hybridSimManager)
{
AgentsQueueOut::Add(pedsToRemove);
}
else
#endif
{
// remove the pedestrians that have left the building
for (unsigned int p = 0; p < pedsToRemove.size(); p++)
{
_building->DeletePedestrian(pedsToRemove[p]);
}
}
// temporary fix for the safest path router
......@@ -434,7 +432,6 @@ void Simulation::PrintStatistics()
goal->GetLastPassingTime());
}
}
Log->Write("\n");
}
......@@ -497,7 +494,6 @@ int Simulation::RunBody(double maxSimTime)
//someone might have left the building
_nPeds = _building->GetAllPedestrians().size();
// update the global time
Pedestrian::SetGlobalTime(t);
......@@ -542,11 +538,8 @@ void Simulation::RunFooter()
}
}
void Simulation::ProcessAgentsQueue()
{
//incoming pedestrians
vector<Pedestrian*> peds;
AgentsQueueIn::GetandClear(peds);
......@@ -567,3 +560,8 @@ AgentsSourcesManager& Simulation::GetAgentSrcManager()
{
return _agentSrcManager;
}
Building* Simulation::GetBuilding()
{
return _building.get();
}
......@@ -53,7 +53,7 @@
#include "events/EventManager.h"
#include "pedestrian/AgentsSourcesManager.h"
//Forward declarations TO DO
//Forward declarations
//class AgentsSourcesManager;
class EventManager;
class HybridSimulationManager;
......@@ -156,6 +156,10 @@ public:
*/
void ProcessAgentsQueue();
/**
* @return a pointer to the building object
*/
Building* GetBuilding();
};
#endif /*SIMULATION_H_*/
......@@ -57,52 +57,26 @@ HybridSimulationManager::HybridSimulationManager(const std::string& server,
char hostname[1024];
gethostname(hostname, 1024);
_internalServerName=std::string(hostname);
//GOOGLE_PROTOBUF_VERIFY_VERSION;
//grpc_init();
//_rpcClient = std::unique_ptr<JPSclient>(new JPSclient( grpc::CreateChannel("localhost:9999",
// grpc::InsecureCredentials(), ChannelArguments())));
// std::string server_address(_serverName + ":" + std::to_string(_port));
// JPSserver service(_agentSrcMng);
//
// ServerBuilder builder;
// builder.AddListeningPort(server_address,
// grpc::InsecureServerCredentials());
// builder.RegisterService(&service);
// _rpcServer= builder.BuildAndStart();
// Log->Write("INFO:\tJuPedSim Server listening on " + server_address);
}
HybridSimulationManager::~HybridSimulationManager()
{
}
bool HybridSimulationManager::Init(Building* building)
{
_building = building;
return true;
}
bool HybridSimulationManager::Run(Simulation& sim)
{
//perform some initialisation stuff
GOOGLE_PROTOBUF_VERIFY_VERSION;
grpc_init();
//copy the building object
_building=sim.GetBuilding();
//string extern_service_address("zam597:9999");
string extern_service_address(_externalServerName + ":" + std::to_string(_externalServerPort));
///0.0.0.0 means to listen on all devices
string jupedsim_service_address("0.0.0.0:" + std::to_string(_internalServerPort));
//string jupedsim_service_address(_internalServerName + ":" + std::to_string(_internalServerPort));
//string jupedsim_service_address("0.0.0.0:9998")/*_serverName + ":" + std::to_string(_port)*/;
//create the client that will be running on its own thread
_rpcClient = std::shared_ptr<JPSclient>(new JPSclient( grpc::CreateChannel(extern_service_address,
......@@ -113,7 +87,7 @@ bool HybridSimulationManager::Run(Simulation& sim)
JPSserver jupedsimService(sim);
jupedsimService.SetDuplexClient(_rpcClient);
//MATSIMserver jupedsimService;
ServerBuilder builder;
builder.AddListeningPort(jupedsim_service_address,
......@@ -125,16 +99,17 @@ bool HybridSimulationManager::Run(Simulation& sim)
Log->Write("INFO:\tJuPedSim is up and running on " + jupedsim_service_address);
Log->Write("INFO:\tNotifying Matsim at " + extern_service_address);
//if(false==_rpcClient->NotifyExternalService(_exinternalServerName,_internalServerPort))
if(false==_rpcClient->NotifyExternalService(_internalServerName,_internalServerPort))
{
Log->Write("ERROR:\tNotification failed");
}
//starting the simulation thread and waiting
// Starting the simulation thread and waiting
// The simulation runs in a loop until the shutdown request
// is received from the external serveur
std::thread t2(&JPSserver::RunSimulation, &jupedsimService);
// This is not needed since t2 will not exit until shutdown is received.
//_rpcServer->Wait();
//TestWorkflow();
......@@ -153,60 +128,20 @@ bool HybridSimulationManager::Run(Simulation& sim)
return true;
}
//bool HybridSimulationManager::Run(Simulation& sim)
//{
// //perform some initialisation stuff
// GOOGLE_PROTOBUF_VERIFY_VERSION;
//
// grpc_init();
//
// //create the client that will be running on its own thread
// _rpcClient = std::unique_ptr<JPSclient>(new JPSclient( grpc::CreateChannel("localhost:9999",
// grpc::InsecureCredentials(), ChannelArguments())));
//
// //create the server
// std::string server_address(_serverName + ":" + std::to_string(_port));
// JPSserver service(_agentSrcMng);
//
// ServerBuilder builder;
// builder.AddListeningPort(server_address,
// grpc::InsecureServerCredentials());
// builder.RegisterService(&service);
//
// _rpcServer= builder.BuildAndStart();
// Log->Write("INFO:\tJuPedSim Server listening on " + server_address);
//
// _rpcServer->Wait();
// //create a socket and use it for the serveur and the client
// //std::thread t1(&HybridSimulationManager::RunClient, this);
// std::thread t2(&HybridSimulationManager::RunServer, this);
// //t1.join();
// t2.join();
//
// //clean up everything
// grpc_shutdown();
// google::protobuf::ShutdownProtobufLibrary();
// return true;
//}
void HybridSimulationManager::operator()()
{
//Run();
}
bool HybridSimulationManager::RunClient()
{
//check the message queue and send
string extern_service_address(
_externalServerName + ":" + std::to_string(_externalServerPort));
JPSclient client(
grpc::CreateChannel("localhost:9999",
grpc::CreateChannel(extern_service_address,
grpc::InsecureCredentials(), ChannelArguments()));
do
{
//check if agents enter/left a link and fire event
client.ProcessAgentQueue(_building);
//wait some time, before a new attempt
cout << "processing Requests for input:" << _shutdown << endl;
Log->Write("INFO:\t processing Requests for input: %d", _shutdown);
//ProcessOutgoingAgent();
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
} while (!_shutdown);
......@@ -249,13 +184,10 @@ bool HybridSimulationManager::RunServer()
void HybridSimulationManager::Shutdown()
{
_shutdown = true;
if (_rpcServer) _rpcServer->Shutdown();
//cout<<"shutting down: "<<_shutdown<<endl;
}
void HybridSimulationManager::ProcessIncomingAgent()
{
if (_rpcServer)
{
_rpcServer->Shutdown();
}
}
void HybridSimulationManager::TestWorkflow()
......@@ -276,10 +208,3 @@ std::string HybridSimulationManager::ToString()
return "INFO:\tHybrid Simulation working on [" + _externalServerName + ":"
+ std::to_string(_externalServerPort) + "]\n";
}
//void HybridSimulationManager::AttachSourceManager(const AgentsSourcesManager& src)
//{
// //TODO create a deep copy of the manager
// // and copy the sources without copying the generated pedestrian
// _agentSrcMng=src;
//}
......@@ -36,29 +36,41 @@ public:
*/
virtual ~HybridSimulationManager();
bool Init(Building* building);
/**
* Start the hybrid simulation
* @param sim, a reference to the simulation object
* @return true if everything went fine
*/
bool Run(Simulation& sim);
bool RunClient();
bool RunServer();
/**
* Shutdown the rpc server and client
* this is not actually used.
*/
void Shutdown();
/**
* @return a string description of the hybrid simulator
*/
std::string ToString();
/**
* Make the class "runnable" by overloading the operator
* @param value
*/
void operator()();
//void operator()();
/**
* Create another source manager
* send outgoing agents to the external server
*/
void AttachSourceManager(const AgentsSourcesManager& src);
void ProcessOutgoingAgent();
private:
void ProcessIncomingAgent();
/**
* Testing the system
*/
void TestWorkflow();
private:
......@@ -69,8 +81,6 @@ private:
std::string _externalServerName="localhost";
std::string _internalServerName="localhost";
Building* _building=nullptr;
//TODO: the method should be passed bz reference in the main function
//std::unique_ptr<grpc::Server> _rpcServer;
std::unique_ptr<grpc::Server> _rpcServer;
AgentsSourcesManager _agentSrcMng;
std::shared_ptr<JPSclient> _rpcClient;
......
......@@ -8,7 +8,7 @@
#include "JPSclient.h"
#include "../pedestrian/Pedestrian.h"
#include "../geometry/Building.h"
#include "../pedestrian/AgentsQueueIn.h"
#include "../pedestrian/AgentsQueue.h"
#include "MATSimInterface.pb.h"
......@@ -159,3 +159,30 @@ int JPSclient::RequestMaxNumberAgents()
Log->Write("ERROR:\t RPC JPSClient call failed <RequestMaxNumberAgents>");
return -1;
}
bool JPSclient::SendTrajectories(Building* building)
{
ClientContext context;
Extern2MATSimTrajectories request;
MATSim2ExternTrajectoriesReceived reply;
//set the time
request.set_time(Pedestrian::GetGlobalTime());
auto&& allPeds = building->GetAllPedestrians();
for(const auto& ped:allPeds)
{
auto&& agent = request.add_agent();
double phi = atan2(ped->GetEllipse().GetSinPhi(), ped->GetEllipse().GetCosPhi())*180/M_PI;
agent->set_angle(phi);
agent->set_color(ped->GetColor());
agent->set_id(ped->GetID());
agent->set_x(ped->GetPos()._x);
agent->set_y(ped->GetPos()._y);
agent->set_z(ped->GetElevation());
}
Status status =_matsimChannel->reqSendTrajectories(&context, request, &reply);
return status.IsOk();
//Log->Write("ERROR:\t RPC JPSClient call failed <reqSendTrajectories>");
}
......@@ -27,6 +27,7 @@ using grpc::ChannelInterface;
using grpc::ClientContext;
using org::matsim::hybrid::MATSim2ExternHasSpace;
using org::matsim::hybrid::MATSim2ExternHasSpaceConfirmed;
using org::matsim::hybrid::Extern2MATSimTrajectories_Agent;
using namespace org::matsim::hybrid;
......@@ -49,12 +50,13 @@ public:
virtual ~JPSclient();
/**
* Process the finished agents queue
* Process the finished agents queue and send them to the
* external server
*/
void ProcessAgentQueue(Building* building);
/**
* Shutdown the channel
* Shutdown the channels
*/
void Shutdown()
{
......@@ -62,10 +64,26 @@ public:
_matsimChannel.reset();
}
/**
* Notify the external service that JuPedsim is up, running
* and waiting for order.
* @param host, the hostname where jupedsim can be reached
* @param port, the port where jupedsim can be reached
* @return true if everything went fine
*/
bool NotifyExternalService(const std::string& host, int port);
/**
* Send a pedestrian to the external server
* @return true in the case of success
*/
bool SendAgentToMatsim(Pedestrian* ped);
/**
* Notify the external server about the end of the simulation.
* This can also be the end of the simulation step as requested by the
* @return true in case of success
*/
bool NotifyEndOfSimulation();
/**
......@@ -85,18 +103,32 @@ public:
_mapMatsimID2JPSID[jpsID]=matsimID;
}
/**
* Send trajectories after a simulation has been completed.
* return true if everything went fine
*/
bool SendTrajectories(Building* building);
//void SetBuilding(Building)
private:
// for testing the functionalities when there is no matsim server present
bool HasSpaceOnJuPedSim(int nodeID);
// for testing the functionalities when there is no matsim server present
bool SendAgentToJuPedSim(Pedestrian* ped);
private:
//call the remote methods on Jupedsim
std::unique_ptr<ExternInterfaceService::Stub> _jupedsimChannel;
//call the remote methods on matsim
std::unique_ptr<MATSimInterfaceService::Stub> _matsimChannel;
//map the matsim agent id to the jupedsim agent id
std::map<int,std::string> _mapMatsimID2JPSID;
//map the pedestrian id, to a time which is set
// if the pedestrian could not be transfered to matsim
std::map <int,int>_counter;
//building object
//Building* _building=nullptr;
};
#endif /* MATSIM_JPSCLIENT_H_ */
......@@ -46,8 +46,9 @@ void JPSserver::RunSimulation()
if(_doSimulation)
{
_SimManager.RunBody(_maxSimTime);
_doSimulation=false;
_jpsClient->SendTrajectories(_SimManager.GetBuilding());
_jpsClient->NotifyEndOfSimulation();
_doSimulation=false;
}
//Log->Write("INFO:\tRPC::JPSserver idle for 3 seconds");
......@@ -89,8 +90,9 @@ Status JPSserver::reqMATSim2ExternPutAgent(ServerContext* context __attribute__
//there should be only one agent in this vector
for(auto&& ped:peds)
{
//TODO: there might be a race condition here if the client is sending agents out
//TODO map the agents back, not necessary if jupedsim is reset after each iteration
//TODO: there might be a race condition here if the client is sending agents out at the same time
//TODO: map the agents back, not necessary if jupedsim is reset after each iteration
// because each incoming agent has an id and an agent will get two
_jpsClient->MapMatsimAgentToJPSagent(ped->GetID(),agent_id);
ped->SetFinalDestination(std::stoi(leave_node));
//schedule the agent
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment