naoqi_node.cpp
Go to the documentation of this file.
00001 #include <boost/program_options.hpp>
00002 #include <alerror/alerror.h>
00003 #include <alcommon/albrokermanager.h>
00004 
00005 #include <ros/ros.h> // for logging macros
00006 
00007 #include <iostream>
00008 
00009 #include <naoqi_driver/naoqi_node.h>
00010 
00011 using namespace std;
00012 
00013 NaoqiNode::NaoqiNode(int argc, char ** argv) :
00014     m_pip("nao.local"),
00015     m_ip("0.0.0.0"),
00016     m_port(0),
00017     m_pport(9559),
00018     m_brokerName("NaoROSBroker")
00019 {
00020     parse_command_line(argc, argv);
00021 }
00022 
00023 NaoqiNode::~NaoqiNode()
00024 {}
00025 
00026 void NaoqiNode::parse_command_line(int argc, char ** argv)
00027 {
00028    string pip;
00029    string ip;
00030    int pport;
00031    int port;
00032    boost::program_options::options_description desc("Configuration");
00033    desc.add_options()
00034       ("help", "show this help message")
00035       ("ip", boost::program_options::value<string>(&ip)->default_value(m_ip),
00036        "IP/hostname of the broker")
00037       ("port", boost::program_options::value<int>(&port)->default_value(m_port),
00038        "Port of the broker")
00039       ("pip", boost::program_options::value<string>(&pip)->default_value(m_pip),
00040        "IP/hostname of parent broker")
00041       ("pport", boost::program_options::value<int>(&pport)->default_value(m_pport),
00042        "port of parent broker")
00043       ;
00044    boost::program_options::variables_map vm;
00045    boost::program_options::store(boost::program_options::parse_command_line(argc, argv, desc), vm);
00046    boost::program_options::notify(vm);
00047    m_port = vm["port"].as<int>();
00048    m_pport = vm["pport"].as<int>();
00049    m_pip = vm["pip"].as<string>();
00050    m_ip = vm["ip"].as<string>();
00051    ROS_DEBUG_STREAM("pip is " << m_pip);
00052    ROS_DEBUG_STREAM("ip is " << m_ip);
00053    ROS_DEBUG_STREAM("port is " << m_port);
00054    ROS_DEBUG_STREAM("pport is " << m_pport);
00055 
00056    if (vm.count("help")) {
00057       cout << desc << "\n";
00058       return ;
00059    }
00060 }
00061 
00062 bool NaoqiNode::connectLocalNaoQi() {
00063    try
00064    {
00065       // The manager might not exist in this process wich causes this to crash.
00066       boost::shared_ptr<AL::ALBrokerManager> manager =
00067         AL::ALBrokerManager::getInstance();
00068       // This will crash if there is no broker registered.
00069       m_broker = manager->getRandomBroker();
00070    }
00071    catch(...)
00072    {
00073       // There is no broker manager running in this process.
00074       return false;
00075    }
00076 
00077    // Fill in members
00078    m_ip = m_broker->getIP();
00079    m_port = m_broker->getPort();
00080    m_pip = m_broker->getParentIP();
00081    m_pport = m_broker->getParentPort();
00082    m_brokerName = m_broker->getName();
00083 
00084   return true;
00085 }
00086 
00087 bool NaoqiNode::connectNaoQi()
00088 {
00089    // Need this to for SOAP serialization of floats to work
00090    setlocale(LC_NUMERIC, "C");
00091    try
00092    {
00093       m_broker = AL::ALBroker::createBroker(m_brokerName, m_ip, m_port, m_pip, m_pport, false);
00094    }
00095    catch(const AL::ALError& e)
00096    {
00097       ROS_ERROR( "Failed to connect broker to: %s:%d",m_pip.c_str(),m_port);
00098       //AL::ALBrokerManager::getInstance()->killAllBroker();
00099       //AL::ALBrokerManager::kill();
00100       return false;
00101    }
00102    ROS_INFO("NAOqi broker ready.");
00103    return true;
00104 }
00105 


naoqi_driver
Author(s): Armin Hornung, Armin Hornung, Stefan Osswald, Daniel Maier, Miguel Sarabia, Severin Lemaignan
autogenerated on Fri Jul 3 2015 12:51:45