HCE Project PHP language client API bindings OOP style  1.1.1
Hierarchical Cluster Engine PHP Client Interface API OOP style
 All Classes Namespaces Files Functions Variables Pages
Connection.inc.php
Go to the documentation of this file.
1 <?php
2 
14 namespace HCE\transport {
15 
16  require_once 'zmsg.php';
17  require_once 'Constants.inc.php';
18  require_once 'IdGenerator.inc.php';
19 
23  class Connection {
27  protected $connectionArray = array ();
28 
35  public function setConnectionArray($connectionArray = NULL) {
36  $this->connectionArray = $connectionArray;
37  }
38 
44  public function getConnectionArray() {
45  return $this->connectionArray;
46  }
47 
55  public function __get($name) {
56  return $this->connectionArray [$name];
57  }
58 
67  public function __construct($connectionArray = NULL) {
68  // Init connection array
69  $ret = array (
70  'error' => PROTOCOL_ERROR_OK,
71  'context' => NULL,
72  'socket' => NULL,
73  'type' => NULL,
74  'host' => NULL,
75  'port' => NULL,
76  'identity' => NULL
77  );
78 
79  // Check connection parameters
80  if ($connectionArray === NULL) {
81  $con_ar = array (
82  'host' => PROTOCOL_HOST_DEFAULT,
83  'port' => PROTOCOL_PORT_ADMIN_DEFAULT,
84  'type' => PROTOCOL_CONNECTION_TYPE_ADMIN,
85  'identity' => IdGenerator::getClientId ()
86  );
87  } else {
88  if (! isset ( $connectionArray ['host'] ) || ! isset ( $connectionArray ['port'] ) || ! isset ( $connectionArray ['type'] ) || ! isset ( $connectionArray ['identity'] )) {
89  $ret ['error'] = PROTOCOL_ERROR_CONNECTION_PARAMS;
90  } else {
91  $con_ar = $connectionArray;
92  }
93  }
94 
95  // Create socket
96  if ($ret ['error'] == PROTOCOL_ERROR_OK) {
97  // Create zmq context
98  $ret ['context'] = new \ZMQContext ();
99  if ($ret ['context'] == NULL) {
100  $ret ['error'] = PROTOCOL_ERROR_CONTEXT_CREATE;
101  } else {
102  // Create zmq socket
103  $ret ['socket'] = new \ZMQSocket ( $ret ['context'], \ZMQ::SOCKET_DEALER );
104  if ($ret ['socket'] === NULL) {
105  $ret ['error'] = PROTOCOL_ERROR_SOCKET_CREATE;
106  } else {
107  // Set request identity
108  $ret ['socket']->setSockOpt ( \ZMQ::SOCKOPT_IDENTITY, $con_ar ['identity'] );
109  if ($con_ar ['type'] == PROTOCOL_CONNECTION_TYPE_ADMIN) {
110  // Set linger 0, do not wait on close
111  $ret ['socket']->setSockOpt ( \ZMQ::SOCKOPT_LINGER, 0 );
112  }
113 
114  // Set connection array properties
115  $ret ['host'] = $con_ar ['host'];
116  $ret ['port'] = $con_ar ['port'];
117  $ret ['type'] = $con_ar ['type'];
118  $ret ['identity'] = $con_ar ['identity'];
119  }
120  }
121  }
122 
123  // Set connection array
124  $this->setConnectionArray ( $ret );
125  }
126 
132  public function open() {
133  //TODO try and catch zmq exeptions
134  return $this->connectionArray ['socket']->connect ( $this->getDSN () );
135  }
136 
142  public function close() {
143  return $this->connectionArray ['socket']->disconnect ( $this->getDSN () );
144  }
145 
151  public function reset() {
152  $this->connectionArray ['socket']->disconnect ( $this->getDSN () );
153  return $this->connectionArray ['socket']->connect ();
154  }
155 
161  public function getDSN($protocol = PROTOCOL_ADMIN_DEFAULT) {
162  return $protocol . '://' . $this->connectionArray ['host'] . ':' . $this->connectionArray ['port'];
163  }
164 
169  public function __destruct() {
170  $this->connectionArray ['context'] = NULL;
171  $this->connectionArray ['socket'] = NULL;
172  $this->connectionArray ['host'] = NULL;
173  $this->connectionArray ['port'] = NULL;
174  $this->connectionArray ['type'] = NULL;
175  $this->connectionArray ['identity'] = NULL;
176  }
177  }
178 }
179 
180 ?>