JPSserver.cpp 4.25 KB
Newer Older
Ulrich Kemloh's avatar
Ulrich Kemloh committed
1 2 3 4 5 6 7 8
/*
 * JPSserver.cpp
 *
 *  Created on: Apr 21, 2015
 *      Author: piccolo
 */

#include "JPSserver.h"
Ulrich Kemloh's avatar
Ulrich Kemloh committed
9
#include "JPSclient.h"
10
#include "../IO/OutputHandler.h"
11 12 13
#include "../pedestrian/AgentsSourcesManager.h"
#include "../pedestrian/AgentsSource.h"
#include "../pedestrian/Pedestrian.h"
Ulrich Kemloh's avatar
Ulrich Kemloh committed
14
#include "../Simulation.h"
15 16

#include <iostream>
Ulrich Kemloh's avatar
Ulrich Kemloh committed
17 18 19 20 21
#include <thread>

//client stuff
#include <grpc++/channel_arguments.h>
#include <grpc++/create_channel.h>
22 23 24

#include "../pedestrian/AgentsQueue.h"

25 26 27 28 29
// external variables
extern OutputHandler* Log;

using namespace std;

Ulrich Kemloh's avatar
Ulrich Kemloh committed
30
JPSserver::JPSserver(Simulation& src): _SimManager(src)
Ulrich Kemloh's avatar
Ulrich Kemloh committed
31
{
Ulrich Kemloh's avatar
Ulrich Kemloh committed
32
     _doSimulation=false;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
33 34
}

Ulrich Kemloh's avatar
Ulrich Kemloh committed
35
JPSserver::~JPSserver()
Ulrich Kemloh's avatar
Ulrich Kemloh committed
36
{
37

Ulrich Kemloh's avatar
Ulrich Kemloh committed
38 39 40 41
}

void JPSserver::RunSimulation()
{
42 43 44
     //get the maximum number of agents
     _SimManager.RunHeader(_jpsClient->RequestMaxNumberAgents());
     do
Ulrich Kemloh's avatar
Ulrich Kemloh committed
45
     {
46
          if(_doSimulation)
Ulrich Kemloh's avatar
Ulrich Kemloh committed
47
          {
48
               _SimManager.RunBody(_maxSimTime);
Ulrich Kemloh's avatar
Ulrich Kemloh committed
49
               _doSimulation=false;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
50
               _jpsClient->NotifyEndOfSimulation();
Ulrich Kemloh's avatar
Ulrich Kemloh committed
51
          }
52

53 54 55 56 57
          //Log->Write("INFO:\tRPC::JPSserver idle for 3 seconds");
          //std::this_thread::sleep_for(std::chrono::milliseconds(500));
     }while(!_shutdown);

     _SimManager.RunFooter();
Ulrich Kemloh's avatar
Ulrich Kemloh committed
58 59
}

Ulrich Kemloh's avatar
Ulrich Kemloh committed
60
Status JPSserver::reqMATSim2ExternHasSpace(ServerContext* context __attribute__((unused)),
Ulrich Kemloh's avatar
Ulrich Kemloh committed
61 62 63
          const MATSim2ExternHasSpace* request,
          MATSim2ExternHasSpaceConfirmed* response)
{
64
     string nodeID=request->nodeid();
65
     //Log->Write("INFO:\tRPC::JPSserver I have space on node " + nodeID);
66 67
     response->set_hasspace(true);
     return Status::OK;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
68 69
}

Ulrich Kemloh's avatar
Ulrich Kemloh committed
70
Status JPSserver::reqMATSim2ExternPutAgent(ServerContext* context  __attribute__((unused)),
Ulrich Kemloh's avatar
Ulrich Kemloh committed
71
          const MATSim2ExternPutAgent* request,
Ulrich Kemloh's avatar
Ulrich Kemloh committed
72
          MATSim2ExternPutAgentConfirmed* response __attribute__((unused)))
Ulrich Kemloh's avatar
Ulrich Kemloh committed
73
{
74
     //Log->Write("INFO:\tRPC::JPSserver I have space on node " + nodeID);
75 76 77
     string agent_id=request->agent().id();
     string leave_node=request->agent().nodes(0);//just the first node
     string enter_node=request->agent().enternode();
78
     //Log->Write("INFO:\tRPC::JPSserver I am taking agent %s going to node %s ",agent_id.c_str(),leave_node.c_str());
79

80
     auto& agentSrcMng=_SimManager.GetAgentSrcManager();
Ulrich Kemloh's avatar
Ulrich Kemloh committed
81
     auto srcs=agentSrcMng.GetSources();
82 83 84 85 86 87

     for(auto&& src:srcs)
     {
          if(src->GetId()==std::stoi(enter_node))
          {
               std::vector<Pedestrian*> peds;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
88
               src->GenerateAgents(peds,1,agentSrcMng.GetBuilding());
89 90 91
               //there should be only one agent in this vector
               for(auto&& ped:peds)
               {
92
                    //TODO: there might be a race condition here if the client is sending agents out
93
                    //TODO map the agents back, not necessary if jupedsim is reset after each iteration
94
                    _jpsClient->MapMatsimAgentToJPSagent(ped->GetID(),agent_id);
95 96 97 98
                    ped->SetFinalDestination(std::stoi(leave_node));
                    //schedule the agent
                    src->AddToPool(ped);
               }
Ulrich Kemloh's avatar
Ulrich Kemloh committed
99
               agentSrcMng.ProcessAllSources();
100 101
          }
     }
102 103

     return Status::OK;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
104 105
}

106 107
Status JPSserver::reqExternDoSimStep(ServerContext* context __attribute__((unused)),
          const ExternDoSimStep* request, ExternDoSimStepReceived* response __attribute__((unused)))
Ulrich Kemloh's avatar
Ulrich Kemloh committed
108
{
Ulrich Kemloh's avatar
Ulrich Kemloh committed
109 110
     double from =request->fromtime();
     double to=request->totime();
111
     Log->Write("INFO:\tRPC::JPSserver Simulation step from %.2f to %.2f seconds",from,to);
Ulrich Kemloh's avatar
Ulrich Kemloh committed
112
     _doSimulation=true;
113
     _maxSimTime=to;
114
     return Status::OK;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
115 116
}

117 118 119
Status JPSserver::reqExternOnPrepareSim(ServerContext* context __attribute__((unused)),
          const ExternOnPrepareSim* request __attribute__((unused)),
          ExternOnPrepareSimConfirmed* response __attribute__((unused)))
Ulrich Kemloh's avatar
Ulrich Kemloh committed
120
{
121
     Log->Write("INFO:\tRPC::JPSserver I am ready for doing the simulation");
Ulrich Kemloh's avatar
Ulrich Kemloh committed
122

123
     return Status::OK;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
124 125
}

Ulrich Kemloh's avatar
Ulrich Kemloh committed
126 127 128 129
Status JPSserver::reqExternAfterSim(
          ServerContext* context __attribute__((unused)),
          const ExternAfterSim* request __attribute__((unused)),
          ExternAfterSimConfirmed* response __attribute__((unused)))
Ulrich Kemloh's avatar
Ulrich Kemloh committed
130
{
131
     Log->Write("INFO:\tRPC::JPSserver I received shutdown order. Good bye");
132
     _shutdown=true;
133
     return Status::OK;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
134
}
Ulrich Kemloh's avatar
Ulrich Kemloh committed
135 136 137 138 139

void JPSserver::SetDuplexClient(std::shared_ptr<JPSclient>& client)
{
     _jpsClient=client;
}