JPSclient.cpp 2.86 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 11
#include "../pedestrian/Pedestrian.h"
#include "../pedestrian/AgentsQueue.h"
#include "../geometry/Building.h"
Ulrich Kemloh's avatar
Ulrich Kemloh committed
12

13 14
#include "MATSimInterface.pb.h"

15
using namespace std;
16 17 18 19 20 21 22 23 24 25 26 27 28

JPSclient::JPSclient(std::shared_ptr<ChannelInterface> channel) :
               _stub(ExternInterfaceService::NewStub(channel))
     {
     //stub for testing and to be removed in the final version
     _stub_matsim=MATSimInterfaceService::NewStub(channel);
     }

JPSclient::~JPSclient()
{
}

void JPSclient::ProcessAgentQueue(Building* building)
Ulrich Kemloh's avatar
Ulrich Kemloh committed
29
{
30 31
     std::vector<Pedestrian*> peds;
     AgentsQueueOut::GetandClear(peds);
Ulrich Kemloh's avatar
Ulrich Kemloh committed
32

33 34 35
     for (auto && ped:peds)
     {

36
          if(HasSpaceOnJuPedSim(ped->GetFinalDestination())==true)
37 38 39 40 41 42 43 44 45 46 47 48
          {
               if(SendAgentToJuPedSim(ped)==false)
               {
                    Log->Write("ERROR:\t RPC:JPSclient request failed (send agent to jupedsim)");
               }
          }
          else
          {
               Log->Write("ERROR:\t RPC:JPSclient request failed (space on jupedsim)");
          }


49
          if(HasSpaceOnMatsim(ped->GetFinalDestination())==true)
50 51 52
          {
               SendAgentToMatsim(ped);
          }
Ulrich Kemloh's avatar
Ulrich Kemloh committed
53
          //std::cout<<"deleting from the client:"<<std::endl;
54 55
          building->DeletePedestrian(ped);
     }
Ulrich Kemloh's avatar
Ulrich Kemloh committed
56 57
}

58
bool JPSclient::HasSpaceOnMatsim(int nodeID)
Ulrich Kemloh's avatar
Ulrich Kemloh committed
59
{
60
     return false;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
61 62
}

63 64 65 66 67 68
bool JPSclient::SendAgentToMatsim(Pedestrian* ped)
{
     //pick the agent from the queue
     //hybrid::Extern2MATSim msg;
     //msg.mutable_agent()->set_id(std::to_string(ped->GetID()));
     //msg.mutable_agent()->set_leavenode(std::to_string(ped->GetExitIndex()));
69
     return false;
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94

}

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

     Status status = _stub->reqMATSim2ExternHasSpace(&context, request, &reply);
     return status.IsOk();

//     if (status.IsOk()) {
//          return true;
//     } else {
//          return "Rpc failed";
//     }
}

bool JPSclient::SendAgentToJuPedSim(Pedestrian* ped)
{
     ClientContext context;
     MATSim2ExternPutAgent request;
     MATSim2ExternPutAgentConfirmed reply;
95 96 97 98 99
     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";

100 101

     request.mutable_agent()->set_id(std::to_string(ped->GetID()));
102
     request.mutable_agent()->set_enternode(enter_node_id);
103
     //only one final destination is supported
104 105
     request.mutable_agent()->add_nodes(leave_node_id);
     //request.mutable_agent()->add_nodes("0");
106 107 108 109 110

     Status status =_stub->reqMATSim2ExternPutAgent(&context, request, &reply);
     return status.IsOk();

}