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

#include "JPSclient.h"
9 10
#include "../pedestrian/Pedestrian.h"
#include "../geometry/Building.h"
11
#include "../pedestrian/AgentsQueueIn.h"
Ulrich Kemloh's avatar
Ulrich Kemloh committed
12

13 14
#include "MATSimInterface.pb.h"

15
using namespace std;
16

Ulrich Kemloh's avatar
Ulrich Kemloh committed
17 18 19 20 21 22 23 24 25
JPSclient::JPSclient(std::shared_ptr<ChannelInterface> channel)
{
     //communication channel to matsim
     _matsimChannel = MATSimInterfaceService::NewStub(channel);

     //communication channel to JuPedsim
     // stub for testing and to be removed in the final version
     _jupedsimChannel= ExternInterfaceService::NewStub(channel);
}
26 27 28 29 30 31

JPSclient::~JPSclient()
{
}

void JPSclient::ProcessAgentQueue(Building* building)
Ulrich Kemloh's avatar
Ulrich Kemloh committed
32
{
33
     std::vector<Pedestrian*> peds;
34
     //cout<<"size:"<<AgentsQueueOut::Size()<<endl;
35 36 37
     AgentsQueueOut::GetandClear(peds);
     for (auto && ped:peds)
     {
38 39
          //if the agent has waited enough
          if (_counter[ped->GetID()]<=0)
40
          {
41 42
               //remove the pedestrian only if successfully sent
               if(SendAgentToMatsim(ped)==true)
43
               {
44 45 46 47 48 49 50
                    building->DeletePedestrian(ped);
               }
               else
               {
                    AgentsQueueOut::Add(ped);
                    //reschedule after 100 timesteps
                    _counter[ped->GetID()]=100;
51 52
               }
          }
53 54 55
          else
          {
               AgentsQueueOut::Add(ped);
56
               _counter[ped->GetID()]--;
57
          }
58
     }
Ulrich Kemloh's avatar
Ulrich Kemloh committed
59 60
}

61 62
bool JPSclient::SendAgentToMatsim(Pedestrian* ped)
{
Ulrich Kemloh's avatar
Ulrich Kemloh committed
63 64 65 66 67
     ClientContext context;
     Extern2MATSim request;
     Extern2MATSimConfirmed reply;

     string leave_node=std::to_string(ped->GetFinalDestination());
68 69
     //string agent_id=std::to_string(ped->GetID());
     string agent_id=_mapMatsimID2JPSID[ped->GetID()];
Ulrich Kemloh's avatar
Ulrich Kemloh committed
70 71 72

     request.mutable_agent()->set_id(agent_id);
     request.mutable_agent()->set_leavenode(leave_node);
73

Ulrich Kemloh's avatar
Ulrich Kemloh committed
74 75
     Status status =_matsimChannel->reqExtern2MATSim(&context, request, &reply);

76 77 78 79 80
     if(status.IsOk())
     {
          return reply.accepted();
     }
     return false;
81 82 83 84 85 86 87 88 89
}

bool JPSclient::HasSpaceOnJuPedSim(int nodeID)
{
     MATSim2ExternHasSpace request;
     request.set_nodeid(std::to_string(nodeID));
     MATSim2ExternHasSpaceConfirmed reply;
     ClientContext context;

Ulrich Kemloh's avatar
Ulrich Kemloh committed
90
     Status status = _jupedsimChannel->reqMATSim2ExternHasSpace(&context, request, &reply);
Ulrich Kemloh's avatar
Ulrich Kemloh committed
91 92 93 94 95 96 97 98 99 100 101

     if(status.IsOk())
     {
          return reply.hasspace();
     }
     else
     {
          Log->Write("ERROR:\t Rpc call failed");
     }

     return false;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
102
}
103

Ulrich Kemloh's avatar
Ulrich Kemloh committed
104
bool JPSclient::NotifyExternalService(const std::string& host, int port)
Ulrich Kemloh's avatar
Ulrich Kemloh committed
105 106 107 108
{
     ClientContext context;
     ExternalConnect request;
     ExternalConnectConfirmed reply;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
109 110
     request.set_host(host);
     request.set_port(port);
Ulrich Kemloh's avatar
Ulrich Kemloh committed
111 112
     Status status =_matsimChannel->reqExternalConnect(&context, request, &reply);
     return status.IsOk();
113 114 115 116 117 118 119
}

bool JPSclient::SendAgentToJuPedSim(Pedestrian* ped)
{
     ClientContext context;
     MATSim2ExternPutAgent request;
     MATSim2ExternPutAgentConfirmed reply;
120 121 122 123 124
     string leave_node_id=std::to_string(ped->GetFinalDestination());
     string enter_node_id="-1";
     if(leave_node_id=="3") enter_node_id="1";
     if(leave_node_id=="4") enter_node_id="2";

125 126

     request.mutable_agent()->set_id(std::to_string(ped->GetID()));
127
     request.mutable_agent()->set_enternode(enter_node_id);
128
     //only one final destination is supported
129 130
     request.mutable_agent()->add_nodes(leave_node_id);
     //request.mutable_agent()->add_nodes("0");
131

Ulrich Kemloh's avatar
Ulrich Kemloh committed
132
     Status status =_jupedsimChannel->reqMATSim2ExternPutAgent(&context, request, &reply);
133 134 135
     return status.IsOk();

}
Ulrich Kemloh's avatar
Ulrich Kemloh committed
136 137 138 139 140 141 142

bool JPSclient::NotifyEndOfSimulation()
{
     ClientContext context;
     ExternSimStepFinished request;
     ExternSimStepFinishedReceived reply;
     Status status =_matsimChannel->reqExternSimStepFinished(&context, request, &reply);
Ulrich Kemloh's avatar
Ulrich Kemloh committed
143
     //Log->Write("INFO:\tRPC::JPSserver simulation step finished");
Ulrich Kemloh's avatar
Ulrich Kemloh committed
144 145 146
     //std::cout<<"Details: "<<status.details()<<endl;
     return status.IsOk();
}
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161

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;
}