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

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

/*
* SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
* SPDX-FileCopyrightText: 2006-2010 RobotCub Consortium
* SPDX-License-Identifier: BSD-3-Clause
*/
#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 (cmd.get(0).asVocab32()) {
case VOCAB_SET:
state.put(key, cmd.get(2));
break;
case VOCAB_GET:
break;
state.unput(key);
break;
}
Value& v = state.find(key);
response.addVocab32(v.isNull() ? VOCAB_NOT : VOCAB_IS);
response.add(cmd.get(1));
if (!v.isNull()) {
response.add(v);
}
port.reply(response);
}
return 0;
}
constexpr yarp::conf::vocab32_t VOCAB_IS
Definition: GenericVocabs.h:14
constexpr yarp::conf::vocab32_t VOCAB_REMOVE
Definition: GenericVocabs.h:22
constexpr yarp::conf::vocab32_t VOCAB_GET
Definition: GenericVocabs.h:13
constexpr yarp::conf::vocab32_t VOCAB_SET
Definition: GenericVocabs.h:12
constexpr yarp::conf::vocab32_t VOCAB_NOT
Definition: GenericVocabs.h:21
A simple collection of objects that can be described and transmitted in a portable way.
Definition: Bottle.h:74
Utilities for manipulating the YARP network, including initialization and shutdown.
Definition: Network.h:783
A mini-server for network communication.
Definition: Port.h:47
A class for storing options and configuration information.
Definition: Property.h:34
A single value (typically within a Bottle).
Definition: Value.h:45
The main, catch-all namespace for YARP.
Definition: dirs.h:16
int main(int argc, char *argv[])
Definition: yarpros.cpp:264