YARP
Yet Another Robot Platform
os/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 <stdio.h>
#include <yarp/os/all.h>
using namespace yarp::os;
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");
}
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 (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.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:72
yarp::os::Bottle::toString
std::string toString() const override
Gives a human-readable textual representation of the bottle.
Definition: Bottle.cpp:214
yarp::os::Property::put
void put(const std::string &key, const std::string &value)
Associate the given key with the given string.
Definition: Property.cpp:998
all.h
VOCAB_NOT
constexpr yarp::conf::vocab32_t VOCAB_NOT
Definition: GenericVocabs.h:24
main
int main(int argc, char *argv[])
Definition: yarpros.cpp:259
VOCAB_GET
constexpr yarp::conf::vocab32_t VOCAB_GET
Definition: GenericVocabs.h:16
yarp::os::Property::find
Value & find(const std::string &key) const override
Gets a value corresponding to a given keyword.
Definition: Property.cpp:1034
yarp::os::Port::open
bool open(const std::string &name) override
Start port operation, with a specific name, with automatically-chosen network parameters.
Definition: Port.cpp:81
yarp::os::Port
Definition: Port.h:49
VOCAB_SET
constexpr yarp::conf::vocab32_t VOCAB_SET
Definition: GenericVocabs.h:15
yarp::os::Property::fromCommand
void fromCommand(int argc, char *argv[], bool skipFirst=true, bool wipe=true)
Interprets a list of command arguments as a list of properties.
Definition: Property.cpp:1057
VOCAB_REMOVE
constexpr yarp::conf::vocab32_t VOCAB_REMOVE
Definition: GenericVocabs.h:25
yarp::os::Bottle::get
Value & get(size_type index) const
Reads a Value v from a certain part of the list.
Definition: Bottle.cpp:249
yarp::os::Value::isNull
bool isNull() const override
Checks if the object is invalid.
Definition: Value.cpp:383
yarp::os::Vocab::encode
NetInt32 encode(const std::string &str)
Convert a string into a vocabulary identifier.
Definition: Vocab.cpp:14
yarp::os::Property::check
bool check(const std::string &key) const override
Check if there exists a property of the given name.
Definition: Property.cpp:1024
yarp::os::Bottle::addVocab
void addVocab(int x)
Places a vocabulary item in the bottle, at the end of the list.
Definition: Bottle.cpp:167
yarp::os::Port::read
bool read(PortReader &reader, bool willReply=false) override
Read an object from the port.
Definition: Port.cpp:474
yarp::os::Port::reply
bool reply(PortWriter &writer) override
Send an object as a reply to an object read from the port.
Definition: Port.cpp:491
yarp::os
Definition: AbstractCarrier.h:17
yarp::os::Network
Definition: Network.h:781
yarp
Definition: numeric.h:47
yarp::os::Bottle::add
void add(const Value &value)
Add a Value to the bottle, at the end of the list.
Definition: Bottle.cpp:339
VOCAB_IS
constexpr yarp::conf::vocab32_t VOCAB_IS
Definition: GenericVocabs.h:17
yarp::os::Value::toString
std::string toString() const override
Return a standard text representation of the content of the object.
Definition: Value.cpp:359
yarp::os::Value
A single value (typically within a Bottle).
Definition: Value.h:46
GenericVocabs.h
yarp::os::Property::unput
void unput(const std::string &key)
Remove the association from the given key to a value, if present.
Definition: Property.cpp:1029
yarp::os::Property
A class for storing options and configuration information.
Definition: Property.h:34