Urbi SDK Remote for C++  2.7.5
timer.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 <libport/debug.hh>
00012 #include <iostream>
00013 #include <urbi/uobject.hh>
00014 
00015 GD_CATEGORY(Test.Timer);
00016 
00017 urbi::UObjectHub* thub;
00018 class timer: public urbi::UObject
00019 {
00020 public:
00021   pthread_t mainthread;
00022 
00023   inline void threadCheck()
00024   {
00025     aver(mainthread == pthread_self());
00026   }
00027 
00028   timer(const std::string& name)
00029     : urbi::UObject(name)
00030   {
00031     mainthread = pthread_self();
00032     UBindVar(timer, updated);
00033     UBindVar(timer, timerup);
00034     UBindVar(timer, hupdated);
00035     hupdated = 0;
00036     timerup = 0;
00037     updated = 0;
00038     UBindFunction(timer, setupUpdate);
00039     UBindFunction(timer, setupTimer);
00040     UBindFunction(timer, unsetupTimer);
00041     UBindFunction(timer, setupHubUpdate);
00042     UBindFunction(timer, init);
00043   }
00044   int init()
00045   {
00046     threadCheck();
00047     return 0;
00048   }
00049   int setupUpdate(int d)
00050   {
00051     threadCheck();
00052     USetUpdate(d);
00053     return 0;
00054   }
00055   std::string setupTimer(int d)
00056   {
00057     threadCheck();
00058     return *USetTimer(d, &timer::onTimer);
00059   }
00060   bool unsetupTimer(const std::string& s)
00061   {
00062     threadCheck();
00063     return removeTimer(urbi::TimerHandle(new std::string(s)));
00064   }
00065   int setupHubUpdate(int d)
00066   {
00067     threadCheck();
00068     thub->USetUpdate(d);
00069     return 0;
00070   }
00071   virtual int update()
00072   {
00073     threadCheck();
00074     updated = (int)updated + 1;
00075     return 0;
00076   }
00077   virtual int onTimer()
00078   {
00079     threadCheck();
00080     timerup = (int)timerup + 1;
00081     return 0;
00082   }
00083   urbi::UVar updated;
00084   urbi::UVar hupdated;
00085   urbi::UVar timerup;
00086 };
00087 
00088 class timerHub: public urbi::UObjectHub
00089 {
00090 public:
00091   timerHub(const std::string& name)
00092     : urbi::UObjectHub(name)
00093   {
00094     GD_INFO_DEBUG("timerhub started");
00095     thub = this;
00096   }
00097 
00098   virtual int update()
00099   {
00100     urbi::UVar v("timer","hupdated");
00101     v = 1;
00102     return 0;
00103   }
00104 };
00105 
00106 UStart(timer);
00107 UStartHub(timerHub);