Check port interrupt
functionality in a rpc communication (see robotology/yarp#752).
We have two modalities:
-
Single module:
$ ./test-rpc-interrupt
-
Two modules:
$ ./test-rpc-interrupt --as client
$ ./test-rpc-interrupt --as server
Check port interrupt
functionality in a rpc communication (see robotology/yarp#752).
We have two modalities:
Single module:
$ ./test-rpc-interrupt
Two modules:
$ ./test-rpc-interrupt --as client
$ ./test-rpc-interrupt --as server
# Copyright: (C) 2016 iCub Facility - Istituto Italiano di Tecnologia | |
# Authors: Ugo Pattacini <ugo.pattacini@iit.it> | |
# CopyPolicy: Released under the terms of the GNU GPL v2.0. | |
cmake_minimum_required(VERSION 2.8.9) | |
project(test-rpc-interrupt) | |
find_package(YARP REQUIRED) | |
include_directories(${YARP_INCLUDE_DIRS}) | |
add_executable(${PROJECT_NAME} main.cpp) | |
target_link_libraries(${PROJECT_NAME} ${YARP_LIBRARIES}) |
/* | |
* Copyright (C) 2016 iCub Facility - Istituto Italiano di Tecnologia | |
* Author: Ugo Pattacini | |
* email: ugo.pattacini@iit.it | |
* Permission is granted to copy, distribute, and/or modify this program | |
* under the terms of the GNU General Public License, version 2 or any | |
* later version published by the Free Software Foundation. | |
* | |
* A copy of the license can be found at | |
* http://www.robotcub.org/icub/license/gpl.txt | |
* | |
* This program is distributed in the hope that it will be useful, but | |
* WITHOUT ANY WARRANTY; without even the implied warranty of | |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General | |
* Public License for more details | |
*/ | |
#include <string> | |
#include <yarp/os/all.h> | |
using namespace std; | |
using namespace yarp::os; | |
class TestRpcInterrupt : public RFModule | |
{ | |
bool isServer; | |
bool isClient; | |
RpcServer server; | |
RpcClient client; | |
bool client_go; | |
public: | |
bool configure(ResourceFinder &rf) | |
{ | |
string as=rf.check("as",Value("server+client")).asString(); | |
isServer=isClient=true; | |
if (as=="server") | |
isClient=false; | |
else if (as=="client") | |
isServer=false; | |
if (isServer) | |
{ | |
server.open("/test-rpc-interrupt/server"); | |
attach(server); | |
} | |
if (isClient) | |
client.open("/test-rpc-interrupt/client"); | |
client_go=false; | |
return true; | |
} | |
double getPeriod() | |
{ | |
return 0.01; | |
} | |
bool updateModule() | |
{ | |
if (isClient) | |
{ | |
if (!client_go) | |
{ | |
client_go=Network::connect("/test-rpc-interrupt/client", | |
"/test-rpc-interrupt/server"); | |
yInfo()<<"[client]: waiting for server to be up"; | |
} | |
else | |
{ | |
Bottle cmd,rep; | |
cmd.addString("tag"); | |
yInfo()<<"[client]: sending "<<cmd.toString(); | |
rep.addString("null"); | |
if (client.write(cmd,rep)) | |
yInfo()<<"[client]: received "<<rep.toString(); | |
else | |
yWarning()<<"[client]: something unexpected happened; rep=" | |
<<rep.toString(); | |
} | |
} | |
return true; | |
} | |
bool respond(const Bottle &cmd, Bottle &rep) | |
{ | |
yInfo()<<"[server]: received "<<cmd.toString(); | |
yInfo()<<"[server]: waiting for a lapse to expire"; | |
Time::delay(5.0); | |
yInfo()<<"[server]: lapse just expired"; | |
rep=cmd; | |
return true; | |
} | |
bool interruptModule() | |
{ | |
if (isClient) | |
{ | |
yInfo()<<"interrupting client"; | |
client.interrupt(); | |
yInfo()<<"client interrupted"; | |
} | |
return true; | |
} | |
bool close() | |
{ | |
if (client.asPort().isOpen()) | |
client.close(); | |
if (server.asPort().isOpen()) | |
server.close(); | |
return true; | |
} | |
}; | |
int main(int argc, char *argv[]) | |
{ | |
Network yarp; | |
if (!yarp.checkNetwork()) | |
{ | |
yError()<<"YARP not found"; | |
return 1; | |
} | |
ResourceFinder rf; | |
rf.configure(argc,argv); | |
TestRpcInterrupt mod; | |
return mod.runModule(rf); | |
} | |