YARP
Yet Another Robot Platform
os/database/database.cpp

A toy "database" program for storing and fetching key-values, accessible from a port.

/*
* Copyright (C) 2006-2020 Istituto Italiano di Tecnologia (IIT)
* Copyright (C) 2006-2010 RobotCub Consortium
* All rights reserved.
*
* This software may be modified and distributed under the terms of the
* BSD-3-Clause license. See the accompanying LICENSE file for details.
*/
#include <yarp/os/Bottle.h>
#include <yarp/os/Port.h>
#include <yarp/os/Value.h>
#include <yarp/os/Vocab.h>
#include <cstdio>
int main(int argc, char* argv[])
{
if (argc <= 1) {
printf("This is a very simple database\n");
printf("Call as: %s --name /database\n", argv[0]);
printf("Then you can test it by running:\n");
printf(" yarp rpc /database\n");
printf("And typing things like:\n");
printf(" set x 24\n");
printf(" get x\n");
printf(" get y\n");
printf(" rm x\n");
printf(" get x\n");
printf(" set \"my favorite numbers\" (5 10 16)\n");
printf(" get \"my favorite numbers\"\n");
}
Network yarp;
Property option;
option.fromCommand(argc, argv);
Property state;
Port port;
port.open(option.check("name", Value("/database")).asString());
while (true) {
Bottle cmd;
Bottle response;
port.read(cmd, true); // true -> will reply
Bottle tmp;
tmp.add(cmd.get(1));
std::string key = tmp.toString();
switch (yarp::os::Vocab::encode(cmd.get(0).toString())) {
case VOCAB_SET:
state.put(key, cmd.get(2));
break;
case VOCAB_GET:
break;
state.unput(key);
break;
}
Value& v = state.find(key);
response.addVocab(v.isNull() ? VOCAB_NOT : VOCAB_IS);
response.add(cmd.get(1));
if (!v.isNull()) {
response.add(v);
}
port.reply(response);
}
return 0;
}
yarp::os::Bottle
A simple collection of objects that can be described and transmitted in a portable way.
Definition: Bottle.h:73
Network.h
VOCAB_NOT
constexpr yarp::conf::vocab32_t VOCAB_NOT
Definition: GenericVocabs.h:24
Port.h
main
int main(int argc, char *argv[])
Definition: yarpros.cpp:261
VOCAB_GET
constexpr yarp::conf::vocab32_t VOCAB_GET
Definition: GenericVocabs.h:16
yarp::os::Port
A mini-server for network communication.
Definition: Port.h:50
VOCAB_SET
constexpr yarp::conf::vocab32_t VOCAB_SET
Definition: GenericVocabs.h:15
VOCAB_REMOVE
constexpr yarp::conf::vocab32_t VOCAB_REMOVE
Definition: GenericVocabs.h:25
Property.h
yarp::os::Vocab::encode
NetInt32 encode(const std::string &str)
Convert a string into a vocabulary identifier.
Definition: Vocab.cpp:14
yarp::os::Network
Utilities for manipulating the YARP network, including initialization and shutdown.
Definition: Network.h:786
yarp
The main, catch-all namespace for YARP.
Definition: environment.h:18
Vocab.h
VOCAB_IS
constexpr yarp::conf::vocab32_t VOCAB_IS
Definition: GenericVocabs.h:17
yarp::os::Value
A single value (typically within a Bottle).
Definition: Value.h:47
GenericVocabs.h
Bottle.h
Value.h
yarp::os::Property
A class for storing options and configuration information.
Definition: Property.h:37