apns-php
 All Data Structures Files Functions Variables Groups Pages
ApnsPHP_Abstract Class Reference

Abstract class: this is the superclass for all Apple Push Notification Service classes. More...

Inheritance diagram for ApnsPHP_Abstract:
Inheritance graph
Collaboration diagram for ApnsPHP_Abstract:
Collaboration graph

Public Member Functions

void __construct (integer $nEnvironment, string $sProviderCertificateFile)
 Constructor. More...
 
void connect ()
 Connects to Apple Push Notification service server. More...
 
boolean disconnect ()
 Disconnects from Apple Push Notifications service server. More...
 
string getCertificateAuthority ()
 Get the Root Certification Authority file path. More...
 
integer getConnectRetryInterval ()
 Get the connect retry interval. More...
 
integer getConnectRetryTimes ()
 Get the connect retry time value. More...
 
integer getConnectTimeout ()
 Get the connection timeout. More...
 
ApnsPHP_Log_Interface getLogger ()
 Get the Logger instance. More...
 
integer getSocketSelectTimeout ()
 Get the TCP socket select timeout. More...
 
integer getWriteInterval ()
 Get the write interval. More...
 
void setConnectRetryInterval (integer $nRetryInterval)
 Set the connect retry interval. More...
 
void setConnectRetryTimes (integer $nRetryTimes)
 Set the connect retry times value. More...
 
void setConnectTimeout (integer $nTimeout)
 Set the connection timeout. More...
 
void setLogger (ApnsPHP_Log_Interface $logger)
 Set the Logger instance to use for logging purpose. More...
 
void setProviderCertificatePassphrase (string $sProviderCertificatePassphrase)
 Set the Provider Certificate passphrase. More...
 
void setRootCertificationAuthority (string $sRootCertificationAuthorityFile)
 Set the Root Certification Authority file. More...
 
void setSocketSelectTimeout (integer $nSelectTimeout)
 Set the TCP socket select timeout. More...
 
void setWriteInterval (integer $nWriteInterval)
 Set the write interval. More...
 

Data Fields

const CONNECT_RETRY_INTERVAL = 1000000
 integer Default connect retry interval in micro seconds. More...
 
const DEVICE_BINARY_SIZE = 32
 integer Device token length. More...
 
const ENVIRONMENT_PRODUCTION = 0
 integer Production environment. More...
 
const ENVIRONMENT_SANDBOX = 1
 integer Sandbox environment. More...
 
const SOCKET_SELECT_TIMEOUT = 1000000
 integer Default socket select timeout in micro seconds. More...
 
const WRITE_INTERVAL = 10000
 integer Default write interval in micro seconds. More...
 

Protected Member Functions

boolean _connect ()
 Connects to Apple Push Notification service server. More...
 
void _log (string $sMessage)
 Logs a message through the Logger. More...
 

Protected Attributes

array $_aServiceURLs = array()
 Container for service URLs environments. More...
 
resource $_hSocket
 SSL Socket. More...
 
ApnsPHP_Log_Interface $_logger
 Logger. More...
 
integer $_nConnectRetryInterval
 Connect retry interval in micro seconds. More...
 
integer $_nConnectRetryTimes = 3
 Connect retry times. More...
 
integer $_nConnectTimeout
 Connect timeout in seconds. More...
 
integer $_nEnvironment
 Active environment. More...
 
integer $_nSocketSelectTimeout
 Socket select timeout in micro seconds. More...
 
integer $_nWriteInterval
 Write interval in micro seconds. More...
 
string $_sProviderCertificateFile
 Provider certificate file with key (Bundled PEM). More...
 
string $_sProviderCertificatePassphrase
 Provider certificate passphrase. More...
 
string $_sRootCertificationAuthorityFile
 Root certification authority file. More...
 

Detailed Description

Abstract class: this is the superclass for all Apple Push Notification Service classes.

This class is responsible for the connection to the Apple Push Notification Service and Feedback.

See Also
http://tinyurl.com/ApplePushNotificationService

Definition at line 40 of file Abstract.php.

Member Function Documentation

void __construct ( integer  $nEnvironment,
string  $sProviderCertificateFile 
)

Constructor.

Parameters
$nEnvironmentEnvironment.
$sProviderCertificateFileProvider certificate file with key (Bundled PEM).
Exceptions
ApnsPHP_Exceptionif the environment is not sandbox or production or the provider certificate file is not readable.

Definition at line 79 of file Abstract.php.

80  {
81  if ($nEnvironment != self::ENVIRONMENT_PRODUCTION && $nEnvironment != self::ENVIRONMENT_SANDBOX) {
82  throw new ApnsPHP_Exception(
83  "Invalid environment '{$nEnvironment}'"
84  );
85  }
86  $this->_nEnvironment = $nEnvironment;
87 
88  if (!is_readable($sProviderCertificateFile)) {
89  throw new ApnsPHP_Exception(
90  "Unable to read certificate file '{$sProviderCertificateFile}'"
91  );
92  }
93  $this->_sProviderCertificateFile = $sProviderCertificateFile;
94 
95  $this->_nConnectTimeout = ini_get("default_socket_timeout");
96  $this->_nWriteInterval = self::WRITE_INTERVAL;
97  $this->_nConnectRetryInterval = self::CONNECT_RETRY_INTERVAL;
98  $this->_nSocketSelectTimeout = self::SOCKET_SELECT_TIMEOUT;
99  }
Exception class.
Definition: Exception.php:25
boolean _connect ( )
protected

Connects to Apple Push Notification service server.

Exceptions
ApnsPHP_Exceptionif is unable to connect.
Returns
True if successful connected.
See Also
http://php.net/manual/en/context.ssl.php

Definition at line 371 of file Abstract.php.

References $_nEnvironment, and _log().

Referenced by connect().

372  {
373  $sURL = $this->_aServiceURLs[$this->_nEnvironment];
374  unset($aURLs);
375 
376  $this->_log("INFO: Trying {$sURL}...");
377 
378  /**
379  * @see http://php.net/manual/en/context.ssl.php
380  */
381  $streamContext = stream_context_create(array('ssl' => array(
382  'verify_peer' => isset($this->_sRootCertificationAuthorityFile),
383  'cafile' => $this->_sRootCertificationAuthorityFile,
384  'local_cert' => $this->_sProviderCertificateFile
385  )));
386 
387  if (!empty($this->_sProviderCertificatePassphrase)) {
388  stream_context_set_option($streamContext, 'ssl',
389  'passphrase', $this->_sProviderCertificatePassphrase);
390  }
391 
392  $this->_hSocket = @stream_socket_client($sURL, $nError, $sError,
393  $this->_nConnectTimeout, STREAM_CLIENT_CONNECT, $streamContext);
394 
395  if (!$this->_hSocket) {
396  throw new ApnsPHP_Exception(
397  "Unable to connect to '{$sURL}': {$sError} ({$nError})"
398  );
399  }
400 
401  stream_set_blocking($this->_hSocket, 0);
402  stream_set_write_buffer($this->_hSocket, 0);
403 
404  $this->_log("INFO: Connected to {$sURL}.");
405 
406  return true;
407  }
Exception class.
Definition: Exception.php:25
void _log(string $sMessage)
Logs a message through the Logger.
Definition: Abstract.php:414
integer $_nEnvironment
Active environment.
Definition: Abstract.php:53

Here is the call graph for this function:

Here is the caller graph for this function:

void _log ( string  $sMessage)
protected

Logs a message through the Logger.

Parameters
$sMessageThe message.

Definition at line 414 of file Abstract.php.

Referenced by _connect(), ApnsPHP_Push_Server::_mainLoop(), ApnsPHP_Push::_updateQueue(), connect(), disconnect(), ApnsPHP_Push_Server::onShutdown(), ApnsPHP_Push_Server::onSignal(), ApnsPHP_Feedback::receive(), ApnsPHP_Push::send(), and ApnsPHP_Push_Server::start().

415  {
416  if (!isset($this->_logger)) {
417  $this->_logger = new ApnsPHP_Log_Embedded();
418  }
419  $this->_logger->log($sMessage);
420  }
A simple logger.
Definition: Embedded.php:31

Here is the caller graph for this function:

void connect ( )

Connects to Apple Push Notification service server.

Retries ConnectRetryTimes if unable to connect and waits setConnectRetryInterval between each attempts.

See Also
setConnectRetryTimes
setConnectRetryInterval
Exceptions
ApnsPHP_Exceptionif is unable to connect after ConnectRetryTimes.

Definition at line 328 of file Abstract.php.

References _connect(), and _log().

Referenced by ApnsPHP_Push::_updateQueue().

329  {
330  $bConnected = false;
331  $nRetry = 0;
332  while (!$bConnected) {
333  try {
334  $bConnected = $this->_connect();
335  } catch (ApnsPHP_Exception $e) {
336  $this->_log('ERROR: ' . $e->getMessage());
337  if ($nRetry >= $this->_nConnectRetryTimes) {
338  throw $e;
339  } else {
340  $this->_log(
341  "INFO: Retry to connect (" . ($nRetry+1) .
342  "/{$this->_nConnectRetryTimes})..."
343  );
344  usleep($this->_nConnectRetryInterval);
345  }
346  }
347  $nRetry++;
348  }
349  }
boolean _connect()
Connects to Apple Push Notification service server.
Definition: Abstract.php:371
Exception class.
Definition: Exception.php:25
void _log(string $sMessage)
Logs a message through the Logger.
Definition: Abstract.php:414

Here is the call graph for this function:

Here is the caller graph for this function:

boolean disconnect ( )

Disconnects from Apple Push Notifications service server.

Returns
True if successful disconnected.

Definition at line 356 of file Abstract.php.

References _log().

Referenced by ApnsPHP_Push::_updateQueue().

357  {
358  if (is_resource($this->_hSocket)) {
359  $this->_log('INFO: Disconnected.');
360  return fclose($this->_hSocket);
361  }
362  return false;
363  }
void _log(string $sMessage)
Logs a message through the Logger.
Definition: Abstract.php:414

Here is the call graph for this function:

Here is the caller graph for this function:

string getCertificateAuthority ( )

Get the Root Certification Authority file path.

Returns
Current Root Certification Authority file path.

Definition at line 185 of file Abstract.php.

References $_sRootCertificationAuthorityFile.

186  {
188  }
string $_sRootCertificationAuthorityFile
Root certification authority file.
Definition: Abstract.php:60
integer getConnectRetryInterval ( )

Get the connect retry interval.

Returns
Connect retry interval in micro seconds.

Definition at line 280 of file Abstract.php.

References $_nConnectRetryInterval.

281  {
283  }
integer $_nConnectRetryInterval
Connect retry interval in micro seconds.
Definition: Abstract.php:63
integer getConnectRetryTimes ( )

Get the connect retry time value.

Returns
Connect retry times.

Definition at line 255 of file Abstract.php.

References $_nConnectRetryTimes.

256  {
258  }
integer $_nConnectRetryTimes
Connect retry times.
Definition: Abstract.php:56
integer getConnectTimeout ( )

Get the connection timeout.

Returns
Connection timeout in seconds.

Definition at line 232 of file Abstract.php.

References $_nConnectTimeout.

233  {
235  }
integer $_nConnectTimeout
Connect timeout in seconds.
Definition: Abstract.php:55
ApnsPHP_Log_Interface getLogger ( )

Get the Logger instance.

Returns
Current Logger instance.

Definition at line 139 of file Abstract.php.

References $_logger.

140  {
141  return $this->_logger;
142  }
ApnsPHP_Log_Interface $_logger
Logger.
Definition: Abstract.php:66
integer getSocketSelectTimeout ( )

Get the TCP socket select timeout.

Returns
Socket select timeout in micro seconds.

Definition at line 312 of file Abstract.php.

References $_nSocketSelectTimeout.

313  {
315  }
integer $_nSocketSelectTimeout
Socket select timeout in micro seconds.
Definition: Abstract.php:64
integer getWriteInterval ( )

Get the write interval.

Returns
Write interval in micro seconds.

Definition at line 209 of file Abstract.php.

References $_nWriteInterval.

210  {
211  return $this->_nWriteInterval;
212  }
integer $_nWriteInterval
Write interval in micro seconds.
Definition: Abstract.php:62
void setConnectRetryInterval ( integer  $nRetryInterval)

Set the connect retry interval.

If the client is unable to connect to the server retries at least for ConnectRetryTimes and waits for this value between each attempts.

See Also
setConnectRetryTimes
Parameters
$nRetryIntervalConnect retry interval in micro seconds.

Definition at line 270 of file Abstract.php.

271  {
272  $this->_nConnectRetryInterval = (int)$nRetryInterval;
273  }
void setConnectRetryTimes ( integer  $nRetryTimes)

Set the connect retry times value.

If the client is unable to connect to the server retries at least for this value. The default connect retry times is 3.

Parameters
$nRetryTimesConnect retry times.

Definition at line 245 of file Abstract.php.

246  {
247  $this->_nConnectRetryTimes = (int)$nRetryTimes;
248  }
void setConnectTimeout ( integer  $nTimeout)

Set the connection timeout.

The default connection timeout is the PHP internal value "default_socket_timeout".

See Also
http://php.net/manual/en/filesystem.configuration.php
Parameters
$nTimeoutConnection timeout in seconds.

Definition at line 222 of file Abstract.php.

223  {
224  $this->_nConnectTimeout = (int)$nTimeout;
225  }
void setLogger ( ApnsPHP_Log_Interface  $logger)

Set the Logger instance to use for logging purpose.

The default logger is ApnsPHP_Log_Embedded, an instance of ApnsPHP_Log_Interface that simply print to standard output log messages.

To set a custom logger you have to implement ApnsPHP_Log_Interface and use setLogger, otherwise standard logger will be used.

See Also
ApnsPHP_Log_Interface
ApnsPHP_Log_Embedded
Parameters
$loggerLogger instance.
Exceptions
ApnsPHP_Exceptionif Logger is not an instance of ApnsPHP_Log_Interface.

Definition at line 118 of file Abstract.php.

119  {
120  if (!is_object($logger)) {
121  throw new ApnsPHP_Exception(
122  "The logger should be an instance of 'ApnsPHP_Log_Interface'"
123  );
124  }
125  if (!($logger instanceof ApnsPHP_Log_Interface)) {
126  throw new ApnsPHP_Exception(
127  "Unable to use an instance of '" . get_class($logger) . "' as logger: " .
128  "a logger must implements ApnsPHP_Log_Interface."
129  );
130  }
131  $this->_logger = $logger;
132  }
Exception class.
Definition: Exception.php:25
The Log Interface.
Definition: Interface.php:33
void setProviderCertificatePassphrase ( string  $sProviderCertificatePassphrase)

Set the Provider Certificate passphrase.

Parameters
$sProviderCertificatePassphraseProvider Certificate passphrase.

Definition at line 150 of file Abstract.php.

151  {
152  $this->_sProviderCertificatePassphrase = $sProviderCertificatePassphrase;
153  }
void setRootCertificationAuthority ( string  $sRootCertificationAuthorityFile)

Set the Root Certification Authority file.

Setting the Root Certification Authority file automatically set peer verification on connect.

See Also
http://tinyurl.com/GeneralProviderRequirements
http://www.entrust.net/
https://www.entrust.net/downloads/root_index.cfm
Parameters
$sRootCertificationAuthorityFileRoot Certification Authority file.
Exceptions
ApnsPHP_Exceptionif Root Certification Authority file is not readable.

Definition at line 170 of file Abstract.php.

171  {
172  if (!is_readable($sRootCertificationAuthorityFile)) {
173  throw new ApnsPHP_Exception(
174  "Unable to read Certificate Authority file '{$sRootCertificationAuthorityFile}'"
175  );
176  }
177  $this->_sRootCertificationAuthorityFile = $sRootCertificationAuthorityFile;
178  }
Exception class.
Definition: Exception.php:25
void setSocketSelectTimeout ( integer  $nSelectTimeout)

Set the TCP socket select timeout.

After writing to socket waits for at least this value for read stream to change status.

In Apple Push Notification protocol there isn't a real-time feedback about the correctness of notifications pushed to the server; so after each write to server waits at least SocketSelectTimeout. If, during this time, the read stream change its status and socket received an end-of-file from the server the notification pushed to server was broken, the server has closed the connection and the client needs to reconnect.

See Also
http://php.net/stream_select
Parameters
$nSelectTimeoutSocket select timeout in micro seconds.

Definition at line 302 of file Abstract.php.

303  {
304  $this->_nSocketSelectTimeout = (int)$nSelectTimeout;
305  }
void setWriteInterval ( integer  $nWriteInterval)

Set the write interval.

After each socket write operation we are sleeping for this time interval. To speed up the sending operations, use Zero as parameter but some messages may be lost.

Parameters
$nWriteIntervalWrite interval in micro seconds.

Definition at line 199 of file Abstract.php.

200  {
201  $this->_nWriteInterval = (int)$nWriteInterval;
202  }

Field Documentation

array $_aServiceURLs = array()
protected

Container for service URLs environments.

Definition at line 51 of file Abstract.php.

resource $_hSocket
protected

SSL Socket.

Definition at line 68 of file Abstract.php.

ApnsPHP_Log_Interface $_logger
protected

Logger.

Definition at line 66 of file Abstract.php.

Referenced by getLogger().

integer $_nConnectRetryInterval
protected

Connect retry interval in micro seconds.

Definition at line 63 of file Abstract.php.

Referenced by getConnectRetryInterval().

integer $_nConnectRetryTimes = 3
protected

Connect retry times.

Definition at line 56 of file Abstract.php.

Referenced by getConnectRetryTimes().

integer $_nConnectTimeout
protected

Connect timeout in seconds.

Definition at line 55 of file Abstract.php.

Referenced by getConnectTimeout().

integer $_nEnvironment
protected

Active environment.

Definition at line 53 of file Abstract.php.

Referenced by _connect().

integer $_nSocketSelectTimeout
protected

Socket select timeout in micro seconds.

Definition at line 64 of file Abstract.php.

Referenced by getSocketSelectTimeout().

integer $_nWriteInterval
protected

Write interval in micro seconds.

Definition at line 62 of file Abstract.php.

Referenced by getWriteInterval().

string $_sProviderCertificateFile
protected

Provider certificate file with key (Bundled PEM).

Definition at line 58 of file Abstract.php.

string $_sProviderCertificatePassphrase
protected

Provider certificate passphrase.

Definition at line 59 of file Abstract.php.

string $_sRootCertificationAuthorityFile
protected

Root certification authority file.

Definition at line 60 of file Abstract.php.

Referenced by getCertificateAuthority().

const CONNECT_RETRY_INTERVAL = 1000000

integer Default connect retry interval in micro seconds.

Definition at line 48 of file Abstract.php.

const DEVICE_BINARY_SIZE = 32

integer Device token length.

Definition at line 45 of file Abstract.php.

const ENVIRONMENT_PRODUCTION = 0

integer Production environment.

Definition at line 42 of file Abstract.php.

const ENVIRONMENT_SANDBOX = 1

integer Sandbox environment.

Definition at line 43 of file Abstract.php.

const SOCKET_SELECT_TIMEOUT = 1000000

integer Default socket select timeout in micro seconds.

Definition at line 49 of file Abstract.php.

const WRITE_INTERVAL = 10000

integer Default write interval in micro seconds.

Definition at line 47 of file Abstract.php.


The documentation for this class was generated from the following file: