Sophie

Sophie

distrib > * > 2010.0 > * > by-pkgid > 345aa895e80053137c21f8693106c3a0 > files > 169

gtkmm2.4-documentation-2.17.4-1mdv2010.0.noarch.rpm

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Plugs and Sockets Example</title>
<link rel="stylesheet" href="style.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.75.1">
<link rel="home" href="index.html" title="Programming with gtkmm">
<link rel="up" href="chapter-plugs-sockets.html" title="Chapter 20. Plugs and Sockets">
<link rel="prev" href="chapter-plugs-sockets.html" title="Chapter 20. Plugs and Sockets">
<link rel="next" href="chapter-chapter-timeouts.html" title="Chapter 21. Timeouts, I/O and Idle Functions">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<div class="navheader">
<table width="100%" summary="Navigation header">
<tr><th colspan="3" align="center">Plugs and Sockets Example</th></tr>
<tr>
<td width="20%" align="left">
<a accesskey="p" href="chapter-plugs-sockets.html"><img src="icons/prev.png" alt="Prev"></a> </td>
<th width="60%" align="center">Chapter 20. Plugs and Sockets</th>
<td width="20%" align="right"> <a accesskey="n" href="chapter-chapter-timeouts.html"><img src="icons/next.png" alt="Next"></a>
</td>
</tr>
</table>
<hr>
</div>
<div class="sect1" title="Plugs and Sockets Example">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="sec-plugs-sockets-example"></a>Plugs and Sockets Example</h2></div></div></div>
<p>
      The following is a simple example of using sockets and plugs.  The method
      of communication between processes is deliberately kept very simple: The
      <code class="classname">Plug</code> writes its ID out to a text file named
      <code class="filename">plug.id</code> and the process with the socket reads the ID
      from this files.  In a real program, you may want to use a more
      sophisticated method of inter-process communication.
    </p>
<p><a class="ulink" href="http://git.gnome.org/cgit/gtkmm-documentation/tree/examples/book/socket/" target="_top">Source Code</a></p>
<p>File: <code class="filename">socket.cc</code>
</p>
<pre class="programlisting">
#include &lt;iostream&gt;
#include &lt;fstream&gt;
#include &lt;gtkmm.h&gt;
#include &lt;gtkmm/socket.h&gt;

using namespace std;

const char* id_filename = "plug.id";

void plug_added()
{
  cout &lt;&lt; "A plug was added" &lt;&lt; endl;
}

bool plug_removed()
{
  cout &lt;&lt; "A Plug was removed" &lt;&lt; endl;
  return true;
}

class MySocketWindow : public Gtk::Window
{
  public:
    MySocketWindow()
    {
      ifstream infile(id_filename);
      if (infile)
      {
        Gtk::Socket* socket = Gtk::manage(new Gtk::Socket());
        add(*socket);
        socket-&gt;signal_plug_added().connect(sigc::ptr_fun(plug_added));
        socket-&gt;signal_plug_removed().connect(sigc::ptr_fun(plug_removed));
        Gdk::NativeWindow plug_id;
        infile &gt;&gt; plug_id;
        infile.close();
        socket-&gt;add_id(plug_id);
      }
      else
      {
        Gtk::Label* label = Gtk::manage(
            new Gtk::Label(
              "Plug id file not found.\n Make sure plug is running."));
        add(*label);
        set_size_request(150, 50);
      }
      show_all();
    }
};

int main(int argc, char** argv)
{
  Gtk::Main app(argc, argv);
  MySocketWindow win;
  app.run(win);
  return 0;
}
</pre>
<p>File: <code class="filename">plug.cc</code>
</p>
<pre class="programlisting">
#include &lt;iostream&gt;
#include &lt;fstream&gt;
#include &lt;gtkmm.h&gt;
#include &lt;gtkmm/plug.h&gt;
#include &lt;glib/gstdio.h&gt;

using namespace std;

const char* id_filename = "plug.id";

void on_embed()
{
  cout &lt;&lt; "I've been embedded." &lt;&lt; endl;
}

class MyPlug : public Gtk::Plug
{
  public:
    MyPlug() :
      m_label("I am the plug")
  {
    set_size_request(150, 100);
    add(m_label);
    signal_embedded().connect(sigc::ptr_fun(on_embed));
    show_all();
  }

  private:
    Gtk::Label m_label;
};


int main(int argc, char** argv)
{
  Gtk::Main app(argc, argv);
  MyPlug plug;

  ofstream out(id_filename);
  out &lt;&lt; plug.get_id();
  out.close();
  cout &lt;&lt; "The window ID is: " &lt;&lt; plug.get_id() &lt;&lt; endl;

  app.run(plug);

  // remove the ID file when the program exits
  g_remove(id_filename);
  return 0;
}
</pre>
<p>
      This example creates two executable programs: <code class="filename">socket</code>
      and <code class="filename">plug</code>.  The idea is that
      <code class="filename">socket</code> has an application window that will embed a
      widget from the <code class="filename">plug</code> program.  The way this example
      is designed, <code class="filename">plug</code> must be running first before
      starting <code class="filename">socket</code>.  To see the example in action,
      execute the following commands in order from within the example directory:
    </p>
<p>
      Start the <code class="filename">plug</code> program and send it to the background
      (or just use a different terminal).
    </p>
<pre class="screen">$ ./plug &amp;</pre>
<p>
      After which you should see something like the following:
    </p>
<pre class="screen">The window ID is: 69206019</pre>
<p>Then start the <code class="filename">socket</code> program:</p>
<pre class="screen">$ ./socket</pre>
<p>
      After starting <code class="filename">socket</code>, you should see the following
      output in the terminal:
    </p>
<pre class="screen">I've been embedded.
A plug was added</pre>
<p>
      The first line of output is from <code class="filename">plug</code>, after it has
      been notified that it has been embedded inside of a
      <code class="classname">Socket</code>.  The second line was emitted by
      <code class="filename">socket</code> in response to its
      <code class="methodname">plug_added</code> signal. If everything was done as
      described above, the <code class="filename">socket</code> window should look
      roughly like the following:
    </p>
<div class="screenshot"><div><img src="figures/socket.png"></div></div>
<p>
      If for some reason the <code class="classname">Socket</code> couldn't attach the
      <code class="classname">Plug</code>, the window would look something like this:
    </p>
<div class="screenshot"><div><img src="figures/socket-fail.png"></div></div>
</div>
<div class="navfooter">
<hr>
<table width="100%" summary="Navigation footer">
<tr>
<td width="40%" align="left">
<a accesskey="p" href="chapter-plugs-sockets.html"><img src="icons/prev.png" alt="Prev"></a> </td>
<td width="20%" align="center"><a accesskey="u" href="chapter-plugs-sockets.html"><img src="icons/up.png" alt="Up"></a></td>
<td width="40%" align="right"> <a accesskey="n" href="chapter-chapter-timeouts.html"><img src="icons/next.png" alt="Next"></a>
</td>
</tr>
<tr>
<td width="40%" align="left" valign="top">Chapter 20. Plugs and Sockets </td>
<td width="20%" align="center"><a accesskey="h" href="index.html"><img src="icons/home.png" alt="Home"></a></td>
<td width="40%" align="right" valign="top"> Chapter 21. Timeouts, I/O and Idle Functions </td>
</tr>
</table>
</div>
</body>
</html>