Skip to content

Commit

Permalink
Initial implementation for a gz service oneway command
Browse files Browse the repository at this point in the history
Signed-off-by: Carlos Agüero <caguero@openrobotics.org>
  • Loading branch information
caguero committed Jan 19, 2024
1 parent 16c3300 commit 80ff367
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 7 deletions.
52 changes: 51 additions & 1 deletion src/cmd/gz.cc
Original file line number Diff line number Diff line change
Expand Up @@ -260,10 +260,60 @@ extern "C" void cmdServiceReq(const char *_service,
else
std::cout << "Service call failed" << std::endl;
}
else
else if (_timeout > 100)
std::cerr << "Service call timed out" << std::endl;
}

//////////////////////////////////////////////////
extern "C" void cmdServiceReqOneway(const char *_service, const char *_reqType,
const char *_reqData)
{
if (!_service)
{
std::cerr << "Service name is null\n";
return;
}

if (!_reqType)
{
std::cerr << "Request type is null\n";
return;
}

if (!_reqData)
{
std::cerr << "Request data is null\n";
return;
}

// Create the request, and populate the field with _reqData
auto req = msgs::Factory::New(_reqType, _reqData);
if (!req)
{
std::cerr << "Unable to create request of type[" << _reqType << "] "
<< "with data[" << _reqData << "].\n";
return;
}

// Create the response.
auto rep = msgs::Factory::New("gz.msgs.Empty");
if (!rep)
{
std::cerr << "Unable to create response of type[gz.msgs.Empty].\n";
return;
}

// Create the node.
Node node;
bool result;
int timeout = 1000;

// Request the service.
bool executed = node.Request(_service, *req, timeout, *rep, result);
if (!executed && timeout > 1000)
std::cerr << "Service call failed" << std::endl;
}

//////////////////////////////////////////////////
extern "C" void cmdTopicEcho(const char *_topic,
const double _duration, int _count, MsgOutputFormat _outputFormat)
Expand Down
23 changes: 17 additions & 6 deletions src/cmd/gz.hh
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ extern "C" void cmdServiceList();
/// E.g.: cmdTopicPub("/foo", "gz.msgs.StringMsg",
/// "'data:\"Custom data\"');
extern "C" void cmdTopicPub(const char *_topic,
const char *_msgType,
const char *_msgData);
const char *_msgType,
const char *_msgData);

/// \brief External hook to execute 'gz service -r' from the command line.
/// \param[in] _service Service name.
Expand All @@ -58,10 +58,21 @@ extern "C" void cmdTopicPub(const char *_topic,
/// "gz.msgs.StringMsg", 1000,
/// "'data:\"Custom data\"');
extern "C" void cmdServiceReq(const char *_service,
const char *_reqType,
const char *_repType,
const int _timeout,
const char *_reqData);
const char *_reqType,
const char *_repType,
const int _timeout,
const char *_reqData);

/// \brief External hook to execute 'gz service -r' from the command line.
/// \param[in] _service Service name.
/// \param[in] _reqType Message type used in the request.
/// \param[in] _reqData Input data sent in the request.
/// The format expected is the same used by Protobuf DebugString().
/// E.g.: cmdServiceReq("/bar", "gz.msgs.StringMsg",
/// "gz.msgs.StringMsg");
extern "C" void cmdServiceReqOneway(const char *_service,
const char *_reqType,
const char *_reqData);

extern "C" {
/// \brief Enum used for specifing the message output format for functions
Expand Down
21 changes: 21 additions & 0 deletions src/cmd/service_main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ enum class ServiceCommand
kServiceList,
kServiceInfo,
kServiceReq,
kServiceReqOneway,
};

//////////////////////////////////////////////////
Expand Down Expand Up @@ -72,6 +73,10 @@ void runServiceCommand(const ServiceOptions &_opt)
_opt.reqType.c_str(), _opt.repType.c_str(),
_opt.timeout, _opt.reqData.c_str());
break;
case ServiceCommand::kServiceReqOneway:
cmdServiceReqOneway(_opt.service.c_str(),
_opt.reqType.c_str(), _opt.reqData.c_str());
break;
case ServiceCommand::kNone:
default:
// In the event that there is no command, display help
Expand Down Expand Up @@ -127,6 +132,22 @@ the same used by Protobuf DebugString(). E.g.:
->needs(repTypeOpt)
->needs(timeoutOpt);

command->add_option_function<std::string>("-o,--req-oneway",
[opt](const std::string &_reqData){
opt->command = ServiceCommand::kServiceReqOneway;
opt->reqData = _reqData;
},
R"(Request a service.
TEXT is the input data.
The format expected is
the same used by Protobuf DebugString(). E.g.:
gz service -s /echo \
--reqtype gz.msgs.StringMsg \
--req-oneway 'data: "Hello"'
)")
->needs(serviceOpt)
->needs(reqTypeOpt);

_app.callback([opt](){runServiceCommand(*opt); });
}

Expand Down

0 comments on commit 80ff367

Please sign in to comment.