YARP
Yet Another Robot Platform
Server.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2020 Istituto Italiano di Tecnologia (IIT)
3  * Copyright (C) 2006-2010 RobotCub Consortium
4  * All rights reserved.
5  *
6  * This software may be modified and distributed under the terms of the
7  * BSD-3-Clause license. See the accompanying LICENSE file for details.
8  */
9 
10 #include <yarp/serversql/Server.h>
11 
12 #include <yarp/os/Bottle.h>
13 #include <yarp/os/Contact.h>
14 #include <yarp/os/Port.h>
15 #include <yarp/os/Property.h>
16 #include <yarp/os/SystemClock.h>
17 #include <yarp/os/Log.h>
18 #include <yarp/os/LogStream.h>
24 
25 #include <cstdio>
26 #include <cmath>
27 
28 using yarp::os::Bottle;
29 using yarp::os::Contact;
30 using yarp::os::Port;
31 using yarp::os::Property;
37 
38 YARP_SERVERSQL_LOG_COMPONENT(SERVER, "yarp.serversql.Server")
39 
40 
41 int yarp::serversql::Server::run(int argc, char** argv)
42 {
43  Property options;
44  options.fromCommand(argc, argv, false);
45 
46  bool verbose = options.check("verbose");
47  bool silent = options.check("silent");
48 
49  if (verbose) {
51  } else if (silent) {
53  }
54 
55  yCInfo(SERVER, R"===(
56  ==========================
57  | __ __ ___ ____ ____ |
58  | \ \/ // || _ \ | _ \ |
59  | \ // /| || |/ / | |/ / |
60  | / // ___ || _ \ | _/ |
61  | /_//_/ |_||_| \_\|_| |
62  | |
63  ==========================)===");
64  yCInfo(SERVER);
65 
66  if (options.check("help")) {
67  yCInfo(SERVER, "Welcome to the YARP name server.\n");
68  yCInfo(SERVER, " --write Write IP address and socket on the configuration file.\n");
69  yCInfo(SERVER, " --config filename.conf Load options from a file.\n");
70  yCInfo(SERVER, " --portdb ports.db Store port information in named database.\n");
71  yCInfo(SERVER, " Must not be on an NFS file system.\n");
72  yCInfo(SERVER, " Set to :memory: to store in memory (faster).\n");
73  yCInfo(SERVER, " --subdb subs.db Store subscription information in named database.\n");
74  yCInfo(SERVER, " Must not be on an NFS file system.\n");
75  yCInfo(SERVER, " Set to :memory: to store in memory (faster).\n");
76  yCInfo(SERVER, " --ip IP.AD.DR.ESS Set IP address of server.\n");
77  yCInfo(SERVER, " --socket NNNNN Set port number of server.\n");
78  yCInfo(SERVER, " --web dir Serve web resources from given directory.\n");
79  yCInfo(SERVER, " --no-web-cache Reload pages from file for each request.\n");
80  yCInfo(SERVER, " --ros Delegate pub/sub to ROS name server.\n");
81  yCInfo(SERVER, " --silent Start in silent mode.\n");
82  //this->stop();
83 
84  return 0;
85  } else {
86  yCInfo(SERVER, "Call with --help for information on available options\n");
87  }
88 
90  if (!nc.open(options)) {
91  return 1;
92  }
93 
94  bool ok = false;
95  NameServerManager name(nc);
96  BootstrapServer fallback(name);
97  Port server;
98  Contact alt;
99  Bottle cmd;
100  Bottle reply;
101  double messageCounter(0);
102  double pollingRate(.1);
103 
104  name.setPort(server);
105  server.setReaderCreator(name);
106 
107  ok = server.open(nc.where(),false);
108  if (!ok) {
109  yCError(SERVER, "Name server failed to open\n");
110  return 1;
111  }
112 
113  fallback.start();
114 
115 
116  // Repeat registrations for the server and fallback server -
117  // these registrations are more complete.
118  yCInfo(SERVER, "Registering name server with itself");
119  nc.preregister(nc.where());
120  nc.preregister(fallback.where());
121 
122  alt = nc.whereDelegate();
123 
124  if (alt.isValid()) {
125  nc.preregister(alt);
126  }
127  nc.goPublic();
128 
129  //Setting nameserver property
130  cmd.addString("set");
131  cmd.addString(server.getName());
132  cmd.addString("nameserver");
133  cmd.addString("true");
134 
135  NameClient::getNameClient().send(cmd, reply);
136 
137  yCInfo(SERVER, "Name server can be browsed at http:/%s",
138  nc.where().toURI(false).c_str());
139  yCInfo(SERVER, "Ok. Ready!");
140 
141  while(!shouldStop) {
142  messageCounter += pollingRate;
143  SystemClock::delaySystem(pollingRate);
144  double dummy;
145 
146  if(std::modf(messageCounter / 600.0, &dummy) < .00001) {
147  yCInfo(SERVER, "Name server running happily");
148  }
149  }
150 
151  yCInfo(SERVER, "closing yarp server");
152  server.close();
153  return 0;
154 }
yarp::os::Port::close
void close() override
Stop port activity.
Definition: Port.cpp:357
LogStream.h
yarp::os::Bottle
A simple collection of objects that can be described and transmitted in a portable way.
Definition: Bottle.h:73
yarp::serversql::impl::NameServerContainer
Definition: NameServerContainer.h:29
yarp::serversql::impl::NameServerContainer::where
const yarp::os::Contact & where()
Definition: NameServerContainer.cpp:59
SystemClock.h
yarp::name::NameServerManager::setPort
void setPort(yarp::os::Port &port)
Definition: NameServerManager.h:43
YARP_SERVERSQL_LOG_COMPONENT
#define YARP_SERVERSQL_LOG_COMPONENT(name, name_string)
Definition: LogComponent.h:37
Port.h
yarp::os::Log::WarningType
@ WarningType
Definition: Log.h:80
NameServerContainer.h
Server.h
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:82
yarp::serversql::impl::LogComponent::setMinumumLogType
void setMinumumLogType(yarp::os::Log::LogType minumumLogType)
Definition: LogComponent.cpp:44
yarp::serversql::impl::NameServerContainer::preregister
void preregister(const yarp::os::Contact &c)
Definition: NameServerContainer.cpp:72
yarp::serversql::impl::NameServerContainer::whereDelegate
yarp::os::Contact whereDelegate()
Definition: NameServerContainer.cpp:64
yarp::os::Contact::toURI
std::string toURI(bool includeCarrier=true) const
Get a representation of the Contact as a URI.
Definition: Contact.cpp:316
NameServerManager.h
yarp::os::Port
A mini-server for network communication.
Definition: Port.h:50
Log.h
LogComponent.h
BootstrapServer.h
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
yarp::os::SystemClock
Definition: SystemClock.h:18
Property.h
yarp::serversql::impl::ComposedNameService::goPublic
void goPublic() override
Definition: ComposedNameService.h:57
yarp::serversql::Server
Definition: Server.h:19
yarp::name::BootstrapServer::where
yarp::os::Contact where()
Definition: BootstrapServer.cpp:197
yarp::name::BootstrapServer::start
bool start()
Definition: BootstrapServer.cpp:90
NameClient.h
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::Contactable::getName
virtual std::string getName() const
Get name of port.
Definition: Contactable.cpp:17
yarp::os::Port::setReaderCreator
void setReaderCreator(PortReaderCreator &creator)
Set a creator for readers for port data.
Definition: Port.cpp:519
yarp::os::Bottle::addString
void addString(const char *str)
Places a string in the bottle, at the end of the list.
Definition: Bottle.cpp:173
yarp::serversql::impl::NameServerContainer::open
bool open(yarp::os::Searchable &options)
Definition: NameServerContainer.cpp:78
yCError
#define yCError(component,...)
Definition: LogComponent.h:157
SERVER
const yarp::os::LogComponent & SERVER()
Definition: Server.cpp:38
yarp::name::NameServerManager
Manage the name server.
Definition: NameServerManager.h:32
yarp::os::impl::NameClient
Client for YARP name server.
Definition: NameClient.h:35
yCInfo
#define yCInfo(component,...)
Definition: LogComponent.h:135
yarp::os::Contact::isValid
bool isValid() const
Checks if a Contact is tagged as valid.
Definition: Contact.cpp:301
yarp
The main, catch-all namespace for YARP.
Definition: environment.h:18
yarp::os::Contact
Represents how to reach a part of a YARP network.
Definition: Contact.h:39
Contact.h
yarp::name::BootstrapServer
Multicast and file-based mechanisms for finding the name server.
Definition: BootstrapServer.h:28
yarp::os::Log::DebugType
@ DebugType
Definition: Log.h:78
Bottle.h
yarp::os::Property
A class for storing options and configuration information.
Definition: Property.h:37