Commit daf7d627 authored by Ulrich Kemloh's avatar Ulrich Kemloh

New Server and Client implementation

parent f4e725b5
......@@ -78,13 +78,13 @@ bool JPSclient::SendAgentToMatsim(Pedestrian* ped)
Extern2MATSimConfirmed reply;
string leave_node=std::to_string(ped->GetFinalDestination());
string agent_id=std::to_string(ped->GetID());
//string agent_id=std::to_string(ped->GetID());
string agent_id=_mapMatsimID2JPSID[ped->GetID()];
request.mutable_agent()->set_id(agent_id);
request.mutable_agent()->set_leavenode(leave_node);
Status status =_matsimChannel->reqExtern2MATSim(&context, request, &reply);
std::cout<<"Details: "<<status.details()<<endl;
return status.IsOk();
}
......@@ -151,7 +151,22 @@ bool JPSclient::NotifyEndOfSimulation()
ExternSimStepFinishedReceived reply;
Status status =_matsimChannel->reqExternSimStepFinished(&context, request, &reply);
Log->Write("INFO:\t Simulation step finished");
Log->Write("INFO:\tRPC::JPSserver simulation step finished");
//std::cout<<"Details: "<<status.details()<<endl;
return status.IsOk();
}
int JPSclient::RequestMaxNumberAgents()
{
ClientContext context;
MaximumNumberOfAgentsConfirmed request;
MaximumNumberOfAgents reply;
Status status =_matsimChannel->reqMaximumNumberOfAgents(&context, request, &reply);
if(status.IsOk())
{
return reply.number();
}
Log->Write("ERROR:\t RPC JPSClient call failed <RequestMaxNumberAgents>");
return -1;
}
......@@ -83,6 +83,22 @@ public:
bool NotifyEndOfSimulation();
/**
* Request the maximal number of agents
* @return
*/
int RequestMaxNumberAgents();
/**
* JuPedsim uses integer for the agent ID, MATSim uses string.
* @param jpsID, the jupedsim internal ID
* @param matsimID, the matsim string id
*/
void MapMatsimAgentToJPSagent(int jpsID, std::string matsimID)
{
_mapMatsimID2JPSID[jpsID]=matsimID;
}
private:
bool HasSpaceOnMatsim(int nodeID);
bool HasSpaceOnJuPedSim(int nodeID);
......@@ -91,6 +107,8 @@ private:
private:
std::unique_ptr<ExternInterfaceService::Stub> _jupedsimChannel;
std::unique_ptr<MATSimInterfaceService::Stub> _matsimChannel;
//map the matsim agent id to the jupedsim agent id
std::map<int,std::string> _mapMatsimID2JPSID;
};
#endif /* MATSIM_JPSCLIENT_H_ */
......@@ -44,21 +44,24 @@ JPSserver::~JPSserver()
void JPSserver::RunSimulation()
{
while(true)
//get the maximum number of agents
_SimManager.RunHeader(_jpsClient->RequestMaxNumberAgents());
do
{
if(!_doSimulation)
if(_doSimulation)
{
Log->Write("INFO:\tRPC::JPSserver starting a new simulation");
_SimManager.RunSimulation(20);
_SimManager.RunBody(_maxSimTime);
_doSimulation=false;
//TODO: notify simulation finished
_jpsClient->NotifyEndOfSimulation();
//exit(0);
}
Log->Write("INFO:\tRPC::JPSserver idle for 3 seconds");
std::this_thread::sleep_for(std::chrono::milliseconds(3000));
}
//Log->Write("INFO:\tRPC::JPSserver idle for 3 seconds");
//std::this_thread::sleep_for(std::chrono::milliseconds(500));
}while(!_shutdown);
_SimManager.RunFooter();
}
Status JPSserver::reqMATSim2ExternHasSpace(ServerContext* context,
......@@ -97,7 +100,10 @@ Status JPSserver::reqMATSim2ExternPutAgent(ServerContext* context,
//there should be only one agent in this vector
for(auto&& ped:peds)
{
ped->SetID(std::stoi(agent_id));
//ped->SetID(std::stoi(agent_id));
//_mapMatsimID2JPSID[agent_id]=ped->GetID();
//TODO: there might be a race condition here if the client is sending agents out
_jpsClient->MapMatsimAgentToJPSagent(ped->GetID(),agent_id);
ped->SetFinalDestination(std::stoi(leave_node));
//schedule the agent
src->AddToPool(ped);
......@@ -110,21 +116,22 @@ Status JPSserver::reqMATSim2ExternPutAgent(ServerContext* context,
return Status::OK;
}
Status JPSserver::reqExternDoSimStep(ServerContext* context,
const ExternDoSimStep* request, ExternDoSimStepReceived* response)
Status JPSserver::reqExternDoSimStep(ServerContext* context __attribute__((unused)),
const ExternDoSimStep* request, ExternDoSimStepReceived* response __attribute__((unused)))
{
double from =request->fromtime();
double to=request->totime();
Log->Write("INFO:\tRPC::JPSserver I will perform a simulation step from %f to %f seconds",from,to);
_doSimulation=true;
_maxSimTime=to;
return Status::OK;
}
Status JPSserver::reqExternOnPrepareSim(ServerContext* context,
const ExternOnPrepareSim* request,
ExternOnPrepareSimConfirmed* response)
Status JPSserver::reqExternOnPrepareSim(ServerContext* context __attribute__((unused)),
const ExternOnPrepareSim* request __attribute__((unused)),
ExternOnPrepareSimConfirmed* response __attribute__((unused)))
{
Log->Write("INFO:\tRPC::JPSserver I am ready for doing the simulation");
Log->Write("INFO:\tRPC::JPSserver I am ready for doing the simulation");
return Status::OK;
}
......@@ -133,6 +140,7 @@ Status JPSserver::reqExternAfterSim(ServerContext* context,
const ExternAfterSim* request, ExternAfterSimConfirmed* response)
{
Log->Write("INFO:\tRPC::JPSserver I received shutdown order. But can I do that ?");
_shutdown=true;
return Status::OK;
}
......
......@@ -57,9 +57,12 @@ public:
private:
Simulation& _SimManager;
//bool _doSimulation=false;
bool _shutdown=false;
std::atomic<bool> _doSimulation;
std::atomic<double> _maxSimTime;
std::shared_ptr<JPSclient> _jpsClient;
int _maxAgents=0;
//std::map<std::string, int> _mapMatsimID2JPSID;
};
#endif /* MATSIM_JPSSERVER_H_ */
......@@ -40,7 +40,7 @@
using namespace std;
#define MAX_AGENT_COUNT 1000
#define MAX_AGENT_COUNT 100000
LCGrid::LCGrid(double boundaries[4], double cellsize, int nPeds)
{
......
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