Net_HL7
[ class tree: Net_HL7 ] [ index: Net_HL7 ] [ all elements ]

Source for file Connection.php

Documentation is available at Connection.php

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3. // +----------------------------------------------------------------------+
  4. // | PHP version 4                                                        |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 2004 D.A.Dokter                                        |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 3.0 of the PHP license,       |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available through the world-wide-web at the following url:           |
  11. // | http://www.php.net/license/3_0.txt.                                  |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: D.A.Dokter <dokter@w20e.com>                                |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id: Connection.php,v 1.7 2004/08/06 07:38:54 wyldebeast Exp $
  20.  
  21. require_once 'Net/HL7/Message.php';
  22.  
  23.  
  24. /**
  25.  * Usage:
  26.  * <code>
  27.  * $conn =& new Net_HL7_Connection('localhost', 8089);
  28.  *
  29.  * $req =& new Net_HL7_Message();
  30.  * 
  31.  * ... set some request attributes
  32.  * 
  33.  * $res = $conn->send($req);
  34.  * 
  35.  * $conn->close();
  36.  * </code>
  37.  *
  38.  * The Net_HL7_Connection object represents the tcp connection to the
  39.  * HL7 message broker. The Connection has only two useful methods
  40.  * (apart from the constructor), send and close. The 'send' method
  41.  * takes a Net_HL7_Message object as argument, and also returns a
  42.  * Net_HL7_Message object. The send method can be used more than once,
  43.  * before the connection is closed.
  44.  *
  45.  * The Connection object holds the following fields:
  46.  *
  47.  * _MESSAGE_PREFIX
  48.  *
  49.  * The prefix to be sent to the HL7 server to initiate the
  50.  * message. Defaults to \013.
  51.  *
  52.  * _MESSAGE_SUFFIX
  53.  * End of message signal for HL7 server. Defaults to \034\015.
  54.  * 
  55.  *
  56.  * @version    0.10
  57.  * @author     D.A.Dokter <dokter@w20e.com>
  58.  * @access     public
  59.  * @category   Networking
  60.  * @package    Net_HL7
  61.  * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
  62.  */
  63.  
  64.     var $_HANDLE;
  65.     var $_MESSAGE_PREFIX;
  66.     var $_MESSAGE_SUFFIX;
  67.     var $_MAX_READ;
  68.  
  69.  
  70.     /**
  71.      * Creates a connection to a HL7 server, or returns undef when a
  72.      * connection could not be established.are:
  73.      *
  74.      * @param mixed Host to connect to
  75.      * @param int Port to connect to
  76.      * @return boolean 
  77.      */
  78.     function Net_HL7_Connection($host$port
  79.     {  
  80.         $this->_HANDLE $this->_connect($host$port);
  81.         $this->_MESSAGE_PREFIX "\013";
  82.         $this->_MESSAGE_SUFFIX "\034\015";
  83.         $this->_MAX_READ       = 8192;
  84.  
  85.         return true;
  86.     }
  87.  
  88.  
  89.     /**
  90.      * Connect to specified host and port
  91.      *
  92.      * @param mixed Host to connect to
  93.      * @param int Port to connect to
  94.      * @return socket 
  95.      * @access private
  96.      */
  97.     function _connect($host$port
  98.     {  
  99.         $socket socket_create(AF_INETSOCK_STREAMSOL_TCP);
  100.         if ($socket < 0{
  101.             trigger_error("create failed: " socket_strerror($socket)E_USER_ERROR);
  102.         }
  103.  
  104.         $result socket_connect($socket$host$port);
  105.  
  106.         if ($result < 0{
  107.             trigger_error("connect failed: " socket_strerror($result)E_USER_ERROR);
  108.         }
  109.  
  110.         return $socket;
  111.     }
  112.  
  113.  
  114.     /**
  115.      * Sends a Net_HL7_Message object over this connection.
  116.      * 
  117.      * @param object Instance of Net_HL7_Message
  118.      * @return object Instance of Net_HL7_Message
  119.      * @access public
  120.      * @see Net_HL7_Message
  121.      */
  122.     function send($req
  123.     {
  124.         $handle $this->_HANDLE;
  125.         $hl7Msg $req->toString();
  126.     
  127.         socket_write($handle$this->_MESSAGE_PREFIX $hl7Msg $this->_MESSAGE_SUFFIX);
  128.  
  129.         $data "";
  130.  
  131.         while(($buf socket_read($handle256PHP_BINARY_READ)) !== false{
  132.             $data .= $buf;
  133.  
  134.             if(preg_match("/" $this->_MESSAGE_SUFFIX "$/"$buf))
  135.                 break;
  136.         }
  137.  
  138.         // Remove message prefix and suffix
  139.         $data preg_replace("/^" $this->_MESSAGE_PREFIX "/"""$data);
  140.         $data preg_replace("/" $this->_MESSAGE_SUFFIX "$/"""$data);
  141.  
  142.         $resp = new Net_HL7_Message($data);
  143.  
  144.         return $resp;
  145.     }
  146.  
  147.  
  148.     /**
  149.      * Close the connection.
  150.      * 
  151.      * @access public
  152.      * @return boolean 
  153.      */
  154.     function close(
  155.     {
  156.         socket_close($this->_HANDLE);
  157.         return true;
  158.     }
  159. }
  160.  
  161. ?>

Documentation generated on Mon, 11 Mar 2019 15:32:34 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.