<html>
  <head>
    <title>Test Persistent Amqphp</title>

    <style type="text/css">

      #container {
        float: none;
      }

      #controls {
        float: left;
        padding: 0;
        border: 0;
        margin: 0;
      }

      #data {
        float: left;
        background: #A8C0D8;
      }

      #connections {
        /* Connections floating menu */
        background: #BEE2E7;
      }

      label {
        display: block;
      }

      #connections input {
        vertical-align: bottom;
        position: relative;
      }

      #new-conn {
        /* New connection form */
        background: #4F57AA;
      }

      #channels {
        /* Channels container */
        background: #72818B;
      }

      .channel {
        /* Channel container */
        background: #72818B;
        border: 1px solid black;
      }

      .consumer {
        /* Consumer container */
        background: #8F002E;
      }

      #new-channel {
        /* New channel form */
        background: #454857;
      }

      #methods {
        background: #788F7D;
      }

      #send {
        background: #77775E;
      }

      #receive {
        background: #DCDABB;
      }

      span.in-message {
        display: block;
      }

      span.out-message {
        display: block;
      }


      div#messages span {
        display: block;
        color: red;
      }


    </style>



  </head>
  <body>

    <h1>Amqphp in web servers: <?php echo getmypid() ?></h1>


    <p>
      This demo  was used to  test the Persistent  connection library,
      you can add and  remove connections, channels and consumers, you
      can send  and receive messages.  To start,  enter the connection
      details  of  your  test Amqp  server  in  to  the form,  once  a
      connection is established you can add channels etc. in your next
      request.  On each reload, the interface shows all currently open
      connections, channels and consumers.
    </p>
    <p>
      Because of the  nature of web servers, the  best (in fact, maybe
      the only sane) way to use  this tool is to tweak your web server
      so that  it only  has a single  worker process.  This  way, each
      time you send a command  to the demo (running inside the server)
      you're sending a command  to the same <i>connection</i>.  Recall
      that PConnections are  <i>&quot;Per worker process&quot;</i>, so
      if your  web server has more  than one worker  the commands that
      you send through  the interface won't necessarily be  run by the
      same  process as  the one  which generated  the  interface.  For
      example, consider a server with 2 worker processes, the sequence
      of command as received by the server might look like this:
      <ol>
        <li>Start web server, 2 worker processes are 5000 and 5001</li>
        <li><strong>User     request:</strong>     Open     connection
        &quot;conn-1&quot; (5000 - OK)</li>
        <li><strong>User      request:</strong>      Open      channel
        &quot;chan-1.1&quot; on  connection &quot;conn-1&quot; (5000 -
        OK)</li>
        <li><strong>User      request:</strong>      Send      message
        &quot;Hi!&quot; to  channel &quot;chan-1.1&quot; on connection
        &quot;conn-1&quot; (5001 - Error: no such channel!)</li>
      </ol>

      The  last message  failed because  this request  happened  to be
      handled by  a different worker process to  the previous request,
      so the  connection &quot;conn-1&quot; doesn't exist  yet in this
      process.   If this request  had been  handled by  worker process
      5000, the request would have worked OK.
    </p>

    <p>
      This  demo   has  2  implementation   classes,  PConnHelper  and
      PConnHelperAlt,  to  demonstrate  the  2  different  persistence
      methods  in  Amqphp.   PConnHelper  uses  a  custom  persistence
      mechanism whereby the code  in PConnHelper calls serialize() and
      unserialize()   on   each    of   the   PConnections   manually.
      PConnHelperAlt  uses a simpler,  internal mechanism  whereby the
      PConnHelperAlt  class calls PConnection->sleep()  at the  end of
      every  request  to  invoke  the persistence  routine.   These  2
      persistence  mechanisms use the  same basic  implementation; the
      reason for  having 2 mechanisms  is to allow users  to integrate
      PConnections in to existing persistence routines.
    </p>

    <?php foreach ($this->messages as $m) { ?>
    <div id="messages">
      <span><?php echo $m; ?></span>
    </div>
    <?php } ?>


    <div id="container">
      <div id="controls">

        <div id="connections">
          <h2>Connections</h2>
          <!-- TODO: Turn this is to a floater! -->
          <ul>
            <?php if ($this->conns->getConnections()) { foreach ($this->conns->getConnections() as $k => $conn) { ?>
            <li>
              <form method="POST">
                ID: <?php echo $k ?>
                <input type="hidden" name="action" value="remove-connection"/>
                <input type="hidden" name="name" value="<?php echo $k ?>"/>
                <input type="submit" value="Remove"/>
              </form>
            </li>
            <?php } } else { ?>
            <li>
              <strong>There are no open connections at this time.</strong>
            </li>
            <?php } ?>
          </ul>
          <div id="new-conn">
            <h2>Create a new connection</h2>
            <form method="POST">
              <input type="hidden" name="action" value="new-connection"/>
              <input type="hidden" name="persistent" value="1"/>

              <label>
                Name
                <input type="text" name="name" value="test1"/>
              </label>
              <label>
                Url
                <input type="text" name="socketParams[url]" value="tcp://rabbit1:5672"/>
              </label>
              <label>
                VHost
                <input type="text" name="vhost" value="/"/>
              </label>
              <label>
                Username
                <input type="text" name="username" value="testing"/>
              </label>
              <label>
                Password
                <input type="text" name="userpass" value="letmein"/>
              </label>

              <input type="submit" value="Go!"/>
            </form>
          </div>
        </div>



        <div id="channels">
          <h2>Channels</h2>

          <?php if (! $this->conns->hasChannels()) { ?>
            <strong>There are no channels open at this time.</strong>
          <?php } else { ?>
          <?php foreach ($this->conns->getConnections() as $k => $conn) { if ($chans = $conn->getChannels()) { foreach ($chans as $k2 => $chan) { ?>
          <div class="channel">
            <!-- Repeats -->
            <ul>
              <li>Channel: <?php echo $k ?>.<?php echo $k2 ?></li>
            </ul>
            <form method="POST">
              <input type="hidden" name="action" value="remove-channel"/>
              <input type="hidden" name="channel" value="<?php echo $k2 ?>"/>
              <input type="hidden" name="connection" value="<?php echo $k ?>"/>
              <input type="submit" value="Remove Channel" id="channel-remove"/>
            </form>

            <?php if ($cTags = $chan->getConsumerTags()) { foreach ($cTags as $cTag) { ?>
            <h3>Consumers</h3>
            <div class="consumer">
              <!-- Repeats -->
              <form method="POST">
                <span class="consumer-tag">Consumer: <?php echo $cTag ?></span>
                <span class="consumer-class">Class: DemoConsumer</span>
                <input type="hidden" name="action" value="remove-consumer"/>
                <input type="hidden" name="connection" value="<?php echo $k ?>"/>
                <input type="hidden" name="channel" value="<?php echo $k2 ?>"/>
                <input type="hidden" name="consumer-tag" value="<?php echo $cTag ?>"/>
                <input type="submit" value="Remove Consumer"/>
              </form>
            </div>
            <?php } } else { ?>
              <strong>There are no consumers attached to this channel</strong>
            <?php } ?>

            <h3>Add Consumer</h3>
            <div class="new-consumer">
              <form method="POST">
                <input type="hidden" name="action" value="new-consumer"/>
                <input type="hidden" name="connection" value="<?php echo $k ?>"/>
                <input type="hidden" name="channel" value="<?php echo $k2 ?>"/>
                <label>
                  Consumer Class
                  <input type="hidden" name="impl" value="DemoPConsumer"/>
                </label>
                <input type="submit" value="Add Consumer"/>
              </form>
            </div>
          </div>
          <?php } } } ?> <!-- End foreach channel / if hasChannels / foreach channels -->
          <?php } ?><!-- End of "if has channels" -->


          <?php if ($conns = $this->conns->getConnections()) { ?>
          <div id="new-channel">
            <h2>Add new Channel</h2>
            <form method="POST">
              <input type="hidden" name="action" value="new-channel"/>
              <label>
                Connection
                <select name="connection">
                  <?php foreach ($conns as $k => $conn) { ?>
                    <option value="<?php echo $k  ?>"><?php echo $k ?></option>
                  <?php } ?>
                </select>
              </label>
              <input type="hidden" name="persistent" value="yes"/>
              <input type="submit" value="Add Channel"/>
            </form>
          </div>
          <?php } ?>
        </div><!-- End Channels section -->




        <?php if ($this->conns->hasChannels()) { ?>
        <div id="send">
          <h2>Send</h2>
          <form method="POST">
            <input type="hidden" name="action" value="send"/>
            <label>
              Message
              <textarea name="message" cols="45" rows="8"></textarea>
            </label>

            <fieldset>
              <legend>Send to channels</legend>
              <?php foreach ($this->conns->getConnections() as $k => $conn) { foreach ($conn->getChannels() as $k2 => $chan) { ?>
              <label>
                Channel <?php echo $k ?>.<?php echo $k2 ?>
                <input type="checkbox" name="target[<?php echo $k ?>][]" value="<?php echo $k2 ?>"/>
              </label>
              <?php } } ?>
            </fieldset>
            <input type="submit" value="Send"/>
          </form>
        </div>
        <?php }?>

        <?php if ($this->conns->hasChannels()) { ?>
        <div id="receive">
          <h2>Receive</h2>
          <form method="POST">
            <input type="hidden" name="action" value="receive"/>
            <fieldset>
              <legend>Receive on Connections</legend>
              <?php foreach ($this->conns->getConnections() as $k => $conn) { $f = false; foreach ($conn->getChannels() as $chan) { if ($chan->getConsumerTags()) { $f = true; break; } } ?>
              <label>
                Connection <?php echo $k ?>
                <input type="checkbox" name="connection[]" value="<?php echo $k ?>"/>
              </label>
              <?php } ?>
            </fieldset>
            <input type="submit" value="Receive"/>
          </form>
        </div>
        <?php } ?>
      </div><!-- End of controls container -->


      <div id="data">
        <h2>Data - messages sent / received</h2>

        <?php if (isset($this->received) && $this->received) { ?>
        <div id="received">
          <h3>Received</h3>
          <?php foreach ($this->received as $m) { ?>
          <span class="in-message"><?php echo $m ?></span>
          <?php } ?>
        </div>
        <?php } ?>




        <?php if (isset($this->sent) && $this->sent) { ?>
        <div id="received">
          <h3>Sent</h3>
          <?php foreach ($this->sent as $m) { ?>
          <span class="out-message"><?php printf('Sent to %s.%s: %s', $m[0], $m[1], $m[2]) ?></span>
          <?php } ?>
        </div>
        <?php } ?>


      </div>

    </div><!-- End of data container -->


  </body>
</html>
