Sophie

Sophie

distrib > Fedora > 14 > x86_64 > media > updates > by-pkgid > 727fa15453fcace956b835e2377d4269 > files > 741

player-doc-3.0.2-5.fc14.noarch.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
   "http://www.w3.org/TR/html4/strict.dtd">

<html>
<!-- $Id: header.html 8799 2010-06-28 04:12:42Z jpgr87 $ -->

<HEAD>


<meta HTTP-EQUIV="Content-Type" CONTENT="text/html;CHARSET=utf-8">
<meta name="keywords" content="stage, robot, simulation, player, player/stage">
<link href="doxygen.css" rel="stylesheet" type="text/css">

<style type="text/css">

.floatright { float: right; margin: 0 0 1em 1em; }

body {
  font-family: sans-serif;
  #font-family: Geneva, Verdana, Helvetica, Arial, sans-serif;
  background-color: #FFF;
  color:#000;
}


a:link { 
	color: #A00;
}

a:visited { 
	color: #800;
}

a { text-decoration: none; }
a:hover { text-decoration: underline; }


.timestamp { text-align:right; background-color: #DDD; font-size:75%;}

h1 { 
  font-size:160%; 
}

h2 {
  font-size:110%;
  #color: #FFF;
  #background-color: #666;
  #padding:3px;
}

h3 { text-align:left; }

img {
  border: 0;
}

ul.menu { 
    position:relative;
    left:-2.5em;
    margin-bottom:0px;
    margin-top:0px;
}

ul.menu1 { 
    position:relative;
    left:-2.1em;
    margin-bottom:0px;
    margin-top:0px;
}

li.menu { 
    list-style-type: none;
    position:relative;
    #left:-0.5em;
}


#sidebar { position: absolute; left:0px; padding:2em; top:0em; width:12em;}

#content { position: absolute; left:12em; top:0em; padding-left:3em; padding-right:3em; padding-bottom:2em; margin-top:1em; margin-right:2em; }

div.box { background-color:#EEE; border: 1px solid #000; padding: 0.5ex 0.4em 0.5ex 0.6em; margin:1em;  }
div.title { font-weight:bold; background-color:#eee; margin-bottom:2px;}

div.topbar { position: absolute; top:0px; left:9em; margin:1em; }

</style>

<TITLE>Player Manual</TITLE>

</HEAD>

<body>

<div id="sidebar"> 

<h2 style="text-align:center;">
<a href="index.html">
<img width=140 src="http://playerstage.sourceforge.net/images/player_button_v3.png" alt="Player logo"><br></a>
</h2>



<div class="box">
<div class=title>Player</div>

<ul class=menu>
<li class=menu><a href="index.html">Frontpage</a>
<li class=menu><a href="modules.html">Contents</a>
</ul>
</div>

<div class="box">
<div class=title>User</div>

<ul class=menu>
<li class=menu><a href="install.html">Installation</a>
<li class=menu><a href="start.html">Quick start</a>
<li class=menu><a href="supported_hardware.html">Supported&nbsp;devices</a>
<li class=menu><a href="group__tutorials.html">Tutorials</a>
<li class=menu><a href="group__utils.html">Utilities</a>
<li class=menu><a href="group__clientlibs.html">Client&nbsp;libraries</a>
<li class=menu><a href="http://playerstage.sourceforge.net/wiki/Basic_FAQ">FAQ</a>
<li class=menu><a href="help.html">Help</a>

</ul>
</div>

<div class=box>
<div class="title">Developer</div>
<ul class=menu>
<li class=menu><a href="architecture.html">Architecture</a>
<li class=menu><a href="group__libplayercore.html">libplayercore</a>
<ul class=menu1>
<li class=menu><a href="group__interfaces.html">interfaces</a></li>
</ul>
<li class=menu><a href="group__libplayerdrivers.html">libplayerdrivers</a>
<ul class=menu1>
<li class=menu><a href="group__drivers.html">drivers</a></li>
</ul>
<li class=menu><a href="group__libplayercommon.html">libplayercommon</a>
<li class=menu><a href="group__libplayerutils.html">libplayerutils</a>
<li class=menu><a href="group__libplayersd.html">libplayersd</a>
<li class=menu><a href="group__libplayertcp.html">libplayertcp</a>
<li class=menu><a href="group__libplayerxdr.html">libplayerxdr</a>
<li class=menu><a href="todo.html">TODO</a>
</ul>
</div>

<div class=box>
<!-- <a href="http://sourceforge.net"><img border=0 src="http://sourceforge.net/sflogo.php?group_id=42445&type=1"></a> -->
<div class="title">Online</div>
<a href="http://playerstage.sourceforge.net">Homepage</a><br>
<a href="http://sourceforge.net/project/showfiles.php?group_id=42445">Download</a><br>
<a href="http://sourceforge.net/projects/playerstage">Project</a><br>
<a href="http://sourceforge.net/tracker/?group_id=42445">Bugs</a><br>
<a href="http://sourceforge.net/mail/?group_id=42445">Help</a>
</div>


</div>

<div id="content" >
<!-- Generated by Doxygen 1.7.1 -->
<div class="header">
  <div class="headertitle">
<h1>Signals &amp; multithreading<br/>
<small>
[<a class="el" href="group__player__clientlib__cplusplus.html">libplayerc++</a>]</small>
</h1>  </div>
</div>
<div class="contents">

<p>Boost signal and thread support.  
<a href="#_details">More...</a></p>
<table class="memberdecls">
</table>
<p>Boost signal and thread support. </p>
<p>Along with providing access to the basic C functions of <a class="el" href="group__player__clientlib__libplayerc.html">libplayerc</a> in a C++ fashion, libplayerc++ also provides additional functionality along the lines of signaling and multithreading. The multithreaded ability of libplayerc++ allieves the developer from having to worry about allotting time to handle messaging. It also allows for the PlayerClient to act as a messaging loop for event driven programs. The signaling and multithreading ability of libplayerc++ is built from the <a href="http://www.boost.org">Boost c++ libraries</a>. This is relevant because we will be using boost semantincs for connecting the signals to the client. Much of this functionality can best be illustrated through the use of an example:</p>
<div class="fragment"><pre class="fragment"><span class="comment">/*</span>
<span class="comment">Copyright (c) 2005, Brad Kratochvil, Toby Collett, Brian Gerkey, Andrew Howard, ...</span>
<span class="comment">All rights reserved.</span>
<span class="comment"></span>
<span class="comment">Redistribution and use in source and binary forms, with or without</span>
<span class="comment">modification, are permitted provided that the following conditions are met:</span>
<span class="comment"></span>
<span class="comment">    * Redistributions of source code must retain the above copyright notice,</span>
<span class="comment">      this list of conditions and the following disclaimer.</span>
<span class="comment">    * Redistributions in binary form must reproduce the above copyright notice,</span>
<span class="comment">      this list of conditions and the following disclaimer in the documentation</span>
<span class="comment">      and/or other materials provided with the distribution.</span>
<span class="comment">    * Neither the name of the Player Project nor the names of its contributors</span>
<span class="comment">      may be used to endorse or promote products derived from this software</span>
<span class="comment">      without specific prior written permission.</span>
<span class="comment"></span>
<span class="comment">THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS &quot;AS IS&quot; AND</span>
<span class="comment">ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED</span>
<span class="comment">WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE</span>
<span class="comment">DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR</span>
<span class="comment">ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES</span>
<span class="comment">(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;</span>
<span class="comment">LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON</span>
<span class="comment">ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT</span>
<span class="comment">(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS</span>
<span class="comment">SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.</span>
<span class="comment">*/</span>

<span class="preprocessor">#include &lt;iostream&gt;</span>
<span class="preprocessor">#include &lt;boost/signal.hpp&gt;</span>
<span class="preprocessor">#include &lt;boost/bind.hpp&gt;</span>
<span class="preprocessor">#include &lt;libplayerc++/playerc++.h&gt;</span>

<span class="comment">// we have a basic command line parser here</span>
<span class="preprocessor">#include &quot;args.h&quot;</span>

<span class="comment">// These are our callback functions.</span>
<span class="comment">// Currently, they all must return void.</span>
<span class="keywordtype">void</span> cb1()
  { std::cout &lt;&lt; <span class="stringliteral">&quot;cb1&quot;</span> &lt;&lt; std::endl; }

<span class="comment">// Callbacks can also be passed parameters</span>
<span class="keywordtype">void</span> cb2(uint32_t &amp;aI)
  { std::cout &lt;&lt; <span class="stringliteral">&quot;cb2 &quot;</span> &lt;&lt; ++aI &lt;&lt; std::endl; }

<span class="comment">// we can also have callbacks in objects</span>
<span class="keyword">class </span>TestCb
{
    <span class="keywordtype">int</span> mId;

  <span class="keyword">public</span>:

    TestCb(<span class="keywordtype">int</span> aId) : mId(aId) {};

    <span class="comment">// We&#39;ll output the ID of the TestCb, so we can see</span>
    <span class="comment">// which object is being called.</span>
    <span class="keywordtype">void</span> Cb()
      { std::cout &lt;&lt; <span class="stringliteral">&quot;TestCb &quot;</span> &lt;&lt; mId &lt;&lt; std::endl; }

    <span class="keywordtype">void</span> Cb(uint32_t aOpt)
      { std::cout &lt;&lt; <span class="stringliteral">&quot;TestCb &quot;</span> &lt;&lt; mId &lt;&lt; <span class="stringliteral">&quot; &quot;</span> &lt;&lt; aOpt &lt;&lt; std::endl; }

};

<span class="comment">// we&#39;ll use this to stop the client</span>
<span class="keywordtype">void</span> stop_cb(<a class="code" href="classPlayerCc_1_1PlayerClient.html" title="The PlayerClient is used for communicating with the player server.">PlayerCc::PlayerClient</a>* c, uint32_t &amp;i)
{
  <span class="comment">// after 10 iterations, stop the client</span>
  <span class="keywordflow">if</span> (++i&gt;10)
  {
    std::cout &lt;&lt; <span class="stringliteral">&quot;stop: &quot;</span> &lt;&lt; i &lt;&lt; std::endl;
    c-&gt;<a class="code" href="classPlayerCc_1_1PlayerClient.html#aa6187ca3b3fad11de5232c0773a813c4" title="Stops the Run() loop.">Stop</a>();
  }
}

<span class="keywordtype">int</span> main(<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span>** argv)
{
  parse_args(argc, argv);
  
  <span class="comment">// it&#39;s always good to put the code in a try block</span>
  <span class="comment">// libplayerc++ throws a PlayerError exception when</span>
  <span class="comment">// it runs into trouble</span>
  <span class="keywordflow">try</span>
  {
    <span class="comment">// let&#39;s setup a client</span>
    <span class="comment">// by default PlayerClient uses localhost and 6665</span>
    <a class="code" href="classPlayerCc_1_1PlayerClient.html" title="The PlayerClient is used for communicating with the player server.">PlayerCc::PlayerClient</a> client(gHostname, gPort);
    <a class="code" href="classPlayerCc_1_1CameraProxy.html" title="The CameraProxy class can be used to get images from a camera device.">PlayerCc::CameraProxy</a>  cp(&amp;client, 0);

    <span class="comment">// Here, we&#39;re connecting a signal to a function.</span>
    <span class="comment">// We keep the connection_t so we can later disconnect.</span>
    PlayerCc::ClientProxy::connection_t conn;
    conn = cp.ConnectReadSignal(&amp;cb1);

    <span class="comment">// Signals can also be connected without storing the connection_t,</span>
    <span class="comment">// but you can no longer disconnect them.</span>
    <span class="comment">// In order to use a callback with a argument, you need to bind</span>
    <span class="comment">// the argument to the callback.</span>
    uint32_t count = 0;
    cp.ConnectReadSignal(boost::bind(&amp;cb2, count));

    <span class="comment">// here we&#39;re connecting a signal to a member function</span>
    TestCb test1(1), test2(2);
    <span class="comment">// just like functions, member functions can also be bound</span>
    <span class="comment">// with or without arguments</span>
    cp.ConnectReadSignal(boost::bind(&amp;TestCb::Cb, boost::ref(test1)));
    cp.ConnectReadSignal(boost::bind(&amp;TestCb::Cb, boost::ref(test2), 1));

    <span class="comment">// now, we should see some signals each time Read() is called for the</span>
    <span class="comment">// object that receives the data</span>

    <span class="comment">// libplayerc++ has three different ways for handling messages from the</span>
    <span class="comment">// server.  The first is by manually calling Read() each time we would</span>
    <span class="comment">// like to process data.</span>
    std::cout &lt;&lt; <span class="stringliteral">&quot;Read()&quot;</span> &lt;&lt; std::endl;
    <span class="keywordflow">for</span> (uint32_t i=0; i&lt;10; ++i)
    {
      client.Read();
      <span class="comment">// an example of disconnecting a signal</span>
      <span class="keywordflow">if</span> (4==i)
        cp.DisconnectReadSignal(conn);
    }

    <span class="comment">// The PlayerClient can also be used as a messaging loop through the use of</span>
    <span class="comment">// a blocking Run() method.  The correct way to tell Run() to quit is</span>
    <span class="comment">// through a stop callback.  In this case, we only have a single thread,</span>
    <span class="comment">// so we don&#39;t have to worry about multithreading</span>
    std::cout &lt;&lt; <span class="stringliteral">&quot;Run()&quot;</span> &lt;&lt; std::endl;

    <span class="comment">// Let&#39;s connect our stop_cb() signal.  This signal tells the client</span>
    <span class="comment">// to exit after 10 iterations</span>
    uint32_t i = 0;
    conn = cp.ConnectReadSignal(boost::bind(&amp;stop_cb, &amp;client, i));

    <span class="comment">// Now, let&#39;s run the client.  This exits when the client-&gt;Stop() function</span>
    <span class="comment">// is called from the callback.</span>
    client.Run();

    <span class="comment">// We can also access the client in a multithreaded fashion.</span>
    std::cout &lt;&lt; <span class="stringliteral">&quot;StartThread()&quot;</span> &lt;&lt; std::endl;

    <span class="comment">// let&#39;s first disconnect our previous stop</span>
    cp.DisconnectReadSignal(conn);

    <span class="comment">// Start the thread, which will handle all message processing.</span>
    client.StartThread();

    <span class="comment">// Sleep for 5 seconds.  During this time, callbacks should be</span>
    <span class="comment">// fired on every read and output should show up on the display.</span>
    timespec sleep = {5, 0};
    nanosleep(&amp;sleep, NULL);

    <span class="comment">// Instead of sleeping here, we could also be sending commands and reading</span>
    <span class="comment">// directly from the proxy.</span>
    <span class="keywordflow">for</span> (uint32_t j=0; j&lt;10; ++j)
    {
      cp.SaveFrame(<span class="stringliteral">&quot;test&quot;</span>);
      <span class="comment">// all proxies have a iostream operator</span>
      std::cout &lt;&lt; cp &lt;&lt; std::endl;
    }

    <span class="comment">// Now, let&#39;s stop the thread.  This function only returns after</span>
    <span class="comment">// the thread has been stopped</span>
    client.StopThread();

    std::cout &lt;&lt; <span class="stringliteral">&quot;finished!&quot;</span> &lt;&lt; std::endl;
  }
  <span class="keywordflow">catch</span> (<a class="code" href="classPlayerCc_1_1PlayerError.html" title="The C++ exception class.">PlayerCc::PlayerError</a> &amp; e)
  {
    <span class="comment">// let&#39;s output the error</span>
    std::cerr &lt;&lt; e &lt;&lt; std::endl;
    <span class="keywordflow">return</span> -1;
  }
  <span class="keywordflow">return</span> 1;
}
</pre></div> </div>
<!-- render the modification time of the source file -->


<div class="timestamp">
<hr>

<table style="width:100%;">
<tr>
<td style="text-align:left;">
Last updated 12 September 2005 21:38:45
<!--
<td style="text-align:right;">
<a href="http://validator.w3.org/check/referer"><img style="vertical-align:middle;border:0;width:88px;height:31px"
          src="http://www.w3.org/Icons/valid-html401"
          alt="Valid HTML 4.01!"></a>

 <a href="http://jigsaw.w3.org/css-validator/">
  <img style="vertical-align:middle;border:0;width:88px;height:31px"
       src="http://jigsaw.w3.org/css-validator/images/vcss"
       alt="Valid CSS!">
 </a>
-->
</tr>
</table>
</div>


</tr>
</table>

</BODY>
</HTML>