Hoa central
Growl.php
Go to the documentation of this file.
1 <?php
2 
37 namespace {
38 
39 from('Hoa')
40 
41 
44 -> import('Notification.Exception')
45 
49 -> import('Notification.~')
50 
54 -> import('Socket.Client');
55 
56 }
57 
58 namespace Hoa\Notification {
59 
70 class Growl implements Notification {
71 
77  protected $_client = null;
78 
84  protected $_applicationName = null;
85 
91  protected $_password = null;
92 
98  protected $_channel = '_default';
99 
106  protected $_channels = array('_default' => false);
107 
108 
109 
121  public function __construct ( $applicationName = 'Hoa',
122  \Hoa\Socket\Client $client = null,
123  $password = '' ) {
124 
125  if(null === $client)
126  $client = new \Hoa\Socket\Client('udp://localhost:9887');
127 
128  $this->_client = $client;
129  $this->_client->connect();
130  $this->setApplicationName($applicationName);
131  $this->setPassword($password);
132 
133  return;
134  }
135 
143  public function __get ( $channel ) {
144 
145  if(false === $this->channelExists($channel))
146  $this->_channels[$channel] = false;
147 
148  $this->_channel = $channel;
149 
150  if(false === $this->_channels[$channel])
151  $this->handshake();
152 
153  return $this;
154  }
155 
162  protected function handshake ( ) {
163 
164  $channel = $this->getChannel();
165  $data = pack(
166  'c2nc2',
167  1,
168  0,
169  strlen($this->getApplicationName()),
170  1,
171  1
172  ) .
173  $this->getApplicationName() .
174  pack('n', strlen($channel)) . $channel .
175  pack('c', 0);
176 
177  $this->_client->writeAll(
178  $data .
179  pack('H32', md5($data . $this->getPassword()))
180  );
181 
182  $this->_channels[$channel] = true;
183 
184  return true;
185  }
186 
195  public function notify ( $title, $message ) {
196 
197  $channel = $this->getChannel();
198 
199  if(false === $this->_channels[$channel]) // _default
200  $this->handshake();
201 
202  $flags = 0;
203  $data = pack(
204  'c2n5',
205  1,
206  1,
207  $flags,
208  strlen($channel),
209  strlen($title),
210  strlen($message),
211  strlen($this->getApplicationName())
212  ) .
213  $channel .
214  $title .
215  $message .
216  $this->getApplicationName();
217 
218  $this->_client->writeAll(
219  $data .
220  pack('H32', md5($data . $this->getPassword()))
221  );
222 
223  return $this;
224  }
225 
233  protected function setApplicationName ( $applicationName ) {
234 
236  $this->_applicationName = $applicationName;
237 
238  return $old;
239  }
240 
247  public function getApplicationName ( ) {
248 
250  }
251 
259  protected function setPassword ( $password ) {
260 
261  $old = $this->_password;
262  $this->_password = $password;
263 
264  return $old;
265  }
266 
273  public function getPassword ( ) {
274 
275  return $this->_password;
276  }
277 
284  public function getChannel ( ) {
285 
286  return $this->_channel;
287  }
288 
295  public function getChannels ( ) {
296 
297  return $this->_channels;
298  }
299 
306  public function channelExists ( $channel ) {
307 
308  return array_key_exists($channel, $this->getChannels());
309  }
310 }
311 
312 }
notify($title, $message)
Definition: Growl.php:195
setApplicationName($applicationName)
Definition: Growl.php:233
setPassword($password)
Definition: Growl.php:259
__construct($applicationName= 'Hoa',\Hoa\Socket\Client $client=null, $password= '')
Definition: Growl.php:121
channelExists($channel)
Definition: Growl.php:306