<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array"/>
  <modified type="array">
    <modified>
      <diff>@@ -146,45 +146,49 @@ else {
 ### Roles
 
 sub subscriber {
-  my $sb = get_agent_connection();
-  
-  foreach my $topic (@topics) {
-    $sb-&gt;subscribe({
-      topic      =&gt; $topic,
-      on_success =&gt; sub {
-        diag(&quot;%% Subscribed topic '$topic'.&quot;);
-      },
-      on_error =&gt; sub {
-        diag(&quot;ERROR subscribing to topic '$topic':&quot;);
-        foreach my $e (@_) {
-          next if ref($e);
-          diag(&quot;    $e&quot;);
+  my $on_connected = sub {
+    my ($sb) = @_;
+    
+    foreach my $topic (@topics) {
+      $sb-&gt;subscribe({
+        topic      =&gt; $topic,
+        on_success =&gt; sub {
+          diag(&quot;%% Subscribed topic '$topic'.&quot;);
+        },
+        on_error =&gt; sub {
+          diag(&quot;!! ERROR subscribing to topic '$topic':&quot;);
+          foreach my $e (@_) {
+            next if ref($e);
+            diag(&quot;    $e&quot;);
+          }
+        },
+        on_message =&gt; sub {
+          my (undef, $notif) = @_;
+          my $payload = $notif-&gt;payload;
+          my $topic   = $notif-&gt;topic;
+          my ($now, $count, $rate, $delta) = _calc_rate($topic);
+
+          my $rt = sprintf('%0.3f mesg/sec (%d messages)', $rate, $count);
+          my $ts = localtime($now-&gt;[0]);
+          substr($ts, -5, 0, '.'.substr($now-&gt;[1], 0, 4));
+          $ts .= sprintf(' (%0.3f elapsed since last message)', $delta) if $delta;
+
+          my $len = length($payload);
+
+          diag(&quot;** Got message on topic '$topic':&quot;);
+          diag(&quot;** at $ts&quot;);
+          diag(&quot;** rate for topic is $rt&quot;);
+          diag();
+          diag(&quot;-- Message starts (length $len)&quot;);
+          print $payload,&quot;\n&quot;;
+          diag(&quot;-- Message ends&quot;);
+          diag();
         }
-      },
-      on_message =&gt; sub {
-        my (undef, $notif) = @_;
-        my $payload = $notif-&gt;payload;
-        my $topic   = $notif-&gt;topic;
-        my ($now, $count, $rate, $delta) = _calc_rate($topic);
+      })
+    }
+  };
 
-        my $rt = sprintf('%0.3f mesg/sec (%d messages)', $rate, $count);
-        my $ts = localtime($now-&gt;[0]);
-        substr($ts, -5, 0, '.'.substr($now-&gt;[1], 0, 4));
-        $ts .= sprintf(' (%0.3f elapsed since last message)', $delta) if $delta;
-        
-        my $len = length($payload);
-
-        diag(&quot;** Got message on topic '$topic':&quot;);
-        diag(&quot;** at $ts&quot;);
-        diag(&quot;** rate for topic is $rt&quot;);
-        diag();
-        diag(&quot;-- Message starts (length $len)&quot;);
-        print $payload,&quot;\n&quot;;
-        diag(&quot;-- Message ends&quot;);
-        diag();
-      }
-    })
-  }
+  my $sb = get_agent_connection($on_connected);
   
   # Temp hack, deliver_messages() should wait forever
   while ($sb-&gt;state eq 'connected') { $sb-&gt;deliver_messages(5) };
@@ -444,6 +448,8 @@ sub max_rate {
 ### Agent connection
 
 sub get_agent_connection {
+  my ($on_connected_cb) = @_;
+  
   my $sb = Net::SAPO::Broker-&gt;new({
     host =&gt; $host,
     port =&gt; $port,
@@ -453,16 +459,17 @@ sub get_agent_connection {
       my $agent = $sb-&gt;host;
       diag(&quot;## Connecting to agent at '$agent'&quot;);
     },
-    on_connected =&gt; sub {
+    on_state_connected =&gt; sub {
       diag(&quot;## Connected!&quot;);
+      $on_connected_cb-&gt;(@_) if $on_connected_cb;
     },
-    on_eof =&gt; sub {
+    on_state_eof =&gt; sub {
       diag(&quot;## Lost connection to agent&quot;);
     },
     on_state_disconnecting =&gt; sub {
       diag(&quot;## Disconnecting&quot;);
     },
-    on_reconnect =&gt; sub {
+    on_state_reconnecting =&gt; sub {
       diag(&quot;## Reconnecting&quot;);
     },
     on_read_error =&gt; sub {</diff>
      <filename>App-SAPO-Broker-Utils/script/sapo-broker-client</filename>
    </modified>
  </modified>
  <removed type="array"/>
  <parents type="array">
    <parent>
      <id>43792ec72abaffc1ec82d81813c9a6f45edf8fb9</id>
    </parent>
  </parents>
  <author>
    <name>Pedro Melo</name>
    <email>melo@simplicidade.org</email>
  </author>
  <url>http://github.com/melo/perl-sapo-broker/commit/6bcdac0e15360a7867b785eff8100715e5fc93ba</url>
  <id>6bcdac0e15360a7867b785eff8100715e5fc93ba</id>
  <committed-date>2008-09-01T10:49:21-07:00</committed-date>
  <authored-date>2008-09-01T10:49:21-07:00</authored-date>
  <message>Adjust subscriber() role to recover the subscriptions after reconnect

Also updated the callbacks to the new hooks of {Net|Protocol}::SAPO::Broker.

Signed-off-by: Pedro Melo &lt;melo@simplicidade.org&gt;</message>
  <tree>bfd10f901a824a700c5b2d9e90543705d21a36e4</tree>
  <committer>
    <name>Pedro Melo</name>
    <email>melo@simplicidade.org</email>
  </committer>
</commit>
