Urbi SDK Remote for C++  2.7.5
liburbi.cc
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2008-2011, Gostai S.A.S.
00003  *
00004  * This software is provided "as is" without warranty of any kind,
00005  * either expressed or implied, including but not limited to the
00006  * implied warranties of fitness for a particular purpose.
00007  *
00008  * See the LICENSE file for more information.
00009  */
00010 
00011 #include <sstream>
00012 #include <urbi/uobject.hh>
00013 #include <urbi/usyncclient.hh>
00014 
00015 using namespace urbi;
00016 
00017 class liburbi : public UObject
00018 {
00019 public:
00020   liburbi(const std::string& n)
00021     : UObject(n)
00022     , cl(0)
00023     {
00024       UBindFunction(liburbi, init);
00025     }
00026   ~liburbi()
00027     {
00028       delete cl;
00029     }
00030   int init()
00031     {
00032       UBindFunction(liburbi, connect);
00033       UBindFunction(liburbi, disconnect);
00034       UBindFunction(liburbi, connectSame);
00035       UBindFunction(liburbi, send);
00036       UBindFunction(liburbi, setCallback);
00037       return 0;
00038     }
00039   int connect(const std::string& host, unsigned port)
00040     {
00041       delete cl;
00042       cl = new USyncClient(host, port);
00043       return cl->error();
00044     }
00045   int connectSame()
00046     {
00047       return connect(getDefaultClient()->getServerName(),
00048                      getDefaultClient()->getServerPort());
00049     }
00050   int disconnect()
00051     {
00052       delete cl;
00053       cl = 0;
00054       return 0;
00055     }
00056   int send(const std::string& msg)
00057     {
00058       cl->send("%s", msg.c_str());
00059       return cl->error();
00060     }
00061   int setCallback(const std::string& tag)
00062     {
00063       cl->setCallback(urbi::callback(*this, &liburbi::callback), tag.c_str());
00064       return 1;
00065     }
00066   UCallbackAction callback(const UMessage& msg)
00067     {
00068       std::stringstream ss;
00069       switch(msg.type)
00070       {
00071       case MESSAGE_DATA:
00072         ss << __name << ".onValue(" << *msg.value << "),";
00073         break;
00074       case MESSAGE_SYSTEM:
00075       case MESSAGE_ERROR:
00076         ss << __name << ".onError(\"" << msg.message << "\"),";
00077         break;
00078       }
00079       send(ss.str());
00080       return URBI_CONTINUE;
00081     }
00082 private:
00083   USyncClient* cl;
00084 };
00085 
00086 UStart(liburbi);