JPSserver.cpp 3.04 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"
9
#include "../IO/OutputHandler.h"
10 11 12 13
#include "../pedestrian/AgentsSourcesManager.h"
#include "../pedestrian/AgentsSource.h"
#include "../pedestrian/AgentsQueue.h"
#include "../pedestrian/Pedestrian.h"
14 15 16 17 18 19 20 21 22 23


#include <iostream>

// external variables
extern OutputHandler* Log;


using namespace std;

Ulrich Kemloh's avatar
Ulrich Kemloh committed
24

25
JPSserver::JPSserver(AgentsSourcesManager& src): _agentSrcMng(src)
Ulrich Kemloh's avatar
Ulrich Kemloh committed
26 27 28 29 30
{
}

JPSserver::~JPSserver()
{
31

Ulrich Kemloh's avatar
Ulrich Kemloh committed
32 33 34 35 36 37
}

Status JPSserver::reqMATSim2ExternHasSpace(ServerContext* context,
          const MATSim2ExternHasSpace* request,
          MATSim2ExternHasSpaceConfirmed* response)
{
38 39 40 41 42
     string nodeID=request->nodeid();
     Log->Write("INFO:\tRPC::JPSserver I have space on node " + nodeID);

     response->set_hasspace(true);
     return Status::OK;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
43 44 45 46 47 48
}

Status JPSserver::reqMATSim2ExternPutAgent(ServerContext* context,
          const MATSim2ExternPutAgent* request,
          MATSim2ExternPutAgentConfirmed* response)
{
49
     //Log->Write("INFO:\tRPC::JPSserver I have space on node " + nodeID);
50 51 52
     string agent_id=request->agent().id();
     string leave_node=request->agent().nodes(0);//just the first node
     string enter_node=request->agent().enternode();
Ulrich Kemloh's avatar
Ulrich Kemloh committed
53
     Log->Write("INFO:\tRPC::JPSserver I am taking agent %s going to node %s ",agent_id.c_str(),leave_node.c_str());
54 55

     auto srcs=_agentSrcMng.GetSources();
Ulrich Kemloh's avatar
Ulrich Kemloh committed
56
     //cout<<"There are: "<<srcs.size()<<" options"<<endl;
57 58 59

     for(auto&& src:srcs)
     {
Ulrich Kemloh's avatar
Ulrich Kemloh committed
60
          //cout<<"size: "<<src->GetPoolSize()<<endl;
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
          if(src->GetId()==std::stoi(enter_node))
          {
               std::vector<Pedestrian*> peds;
               src->GenerateAgents(peds,1,_agentSrcMng.GetBuilding());
               //there should be only one agent in this vector
               for(auto&& ped:peds)
               {
                    ped->SetID(std::stoi(agent_id));
                    ped->SetFinalDestination(std::stoi(leave_node));
                    //schedule the agent
                    src->AddToPool(ped);
               }
               _agentSrcMng.ProcessAllSources();
               //AgentsQueue::Add(peds);
          }
     }
77 78 79 80 81 82

     //take the nodeID and the destination ID
     //find the corresponding source
     //generate a new agent and add to the source
     //call processAllSource on the AgentsoursceManager
     return Status::OK;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
83 84 85 86 87
}

Status JPSserver::reqExternDoSimStep(ServerContext* context,
          const ExternDoSimStep* request, ExternDoSimStepReceived* response)
{
88 89
     std::cout << "Performing simulation step" << std::endl;
     return Status::OK;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
90 91 92 93 94 95
}

Status JPSserver::reqExternOnPrepareSim(ServerContext* context,
          const ExternOnPrepareSim* request,
          ExternOnPrepareSimConfirmed* response)
{
96 97
     std::cout << "I am preparing the simulation" << std::endl;
     return Status::OK;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
98 99 100 101 102
}

Status JPSserver::reqExternAfterSim(ServerContext* context,
          const ExternAfterSim* request, ExternAfterSimConfirmed* response)
{
103 104
     std::cout << "Simulation step completed" << std::endl;
     return Status::OK;
Ulrich Kemloh's avatar
Ulrich Kemloh committed
105
}