You are here

class Nodejs in Node.js integration 7

Same name and namespace in other branches
  1. 6 nodejs.module \Nodejs

Hierarchy

Expanded class hierarchy of Nodejs

2 string references to 'Nodejs'
nodejs_init in ./nodejs.module
Implements hook_init().
nodejs_menu in ./nodejs.module
Implements hook_menu().

File

./nodejs.module, line 803

View source
class Nodejs {
  public static $messages = array();
  public static $config = NULL;
  public static $baseUrl = NULL;
  public static $nodeServerVersion = NULL;
  public static function initConfig() {
    if (!isset(self::$config)) {
      self::$config = nodejs_get_config();
      self::$baseUrl = nodejs_get_url(self::$config);
    }
  }
  public static function getMessages() {
    return self::$messages;
  }
  public static function enqueueMessage(StdClass $message) {
    self::$messages[] = $message;
  }
  public static function sendMessages() {
    foreach (self::$messages as $message) {
      self::sendMessage($message);
    }
  }
  public static function getServerVersion() {
    if (isset(self::$nodeServerVersion)) {
      return self::$nodeServerVersion;
    }
    $data = self::healthCheck();
    if (!isset($data->version)) {
      self::$nodeServerVersion = FALSE;
    }
    else {
      self::$nodeServerVersion = $data->version;
    }
    return self::$nodeServerVersion;
  }
  public static function checkServerVersion() {
    $server_version = self::getServerVersion();
    if (!$server_version) {

      // Version number is missing. Assume incompatibility.
      return FALSE;
    }
    $current_parts = explode('.', $server_version);
    $required_parts = explode('.', NODEJS_SERVER_VERSION);
    $current_major = reset($current_parts);
    $required_major = reset($required_parts);
    if ($current_major != $required_major || version_compare($server_version, NODEJS_SERVER_VERSION) < 0) {
      return FALSE;
    }
    return TRUE;
  }
  public static function sendMessage(StdClass $message) {
    self::initConfig();
    drupal_alter('nodejs_message', $message);
    $message->clientSocketId = nodejs_get_client_socket_id();
    $options = array(
      'method' => 'POST',
      'data' => drupal_json_encode($message),
      // This is server to server, so start with a low default timeout.
      'timeout' => !empty(self::$config['timeout']) ? self::$config['timeout'] : 5,
    );
    return self::httpRequest('nodejs/publish', $options);
  }
  public static function healthCheck() {
    $data = self::httpRequest('nodejs/health/check', array(), TRUE);
    return $data;
  }
  public static function setUserPresenceList($uid, array $uids) {
    return self::httpRequest("nodejs/user/presence-list/{$uid}/" . implode(',', $uids));
  }
  public static function logoutUser($token) {
    $options = array(
      'method' => 'POST',
    );
    return self::httpRequest("nodejs/user/logout/{$token}", $options);
  }
  public static function sendContentTokenMessage($message) {
    $message->clientSocketId = nodejs_get_client_socket_id();
    drupal_alter('nodejs_content_channel_message', $message);
    $options = array(
      'method' => 'POST',
      'data' => drupal_json_encode($message),
      'options' => array(
        'timeout' => 5.0,
      ),
    );
    return self::httpRequest('nodejs/content/token/message', $options);
  }
  public static function sendContentToken($message) {
    $options = array(
      'method' => 'POST',
      'data' => drupal_json_encode($message),
    );
    return self::httpRequest('nodejs/content/token', $options);
  }
  public static function getContentTokenUsers($message) {
    $options = array(
      'method' => 'POST',
      'data' => drupal_json_encode($message),
    );
    return self::httpRequest('nodejs/content/token/users', $options);
  }
  public static function kickUser($uid) {
    $options = array(
      'method' => 'POST',
    );
    return self::httpRequest("nodejs/user/kick/{$uid}", $options);
  }
  public static function addUserToChannel($uid, $channel) {
    $options = array(
      'method' => 'POST',
    );
    return self::httpRequest("nodejs/user/channel/add/{$channel}/{$uid}", $options);
  }
  public static function removeUserFromChannel($uid, $channel) {
    $options = array(
      'method' => 'POST',
    );
    return self::httpRequest("nodejs/user/channel/remove/{$channel}/{$uid}", $options);
  }
  public static function addChannel($channel) {
    $options = array(
      'method' => 'POST',
    );
    return self::httpRequest("nodejs/channel/add/{$channel}", $options);
  }
  public static function checkChannel($channel) {
    return self::httpRequest("nodejs/channel/check/{$channel}");
  }
  public static function removeChannel($channel) {
    $options = array(
      'method' => 'POST',
    );
    return self::httpRequest("nodejs/channel/remove/{$channel}", $options);
  }
  public static function httpRequest($url, $options = array(), $ignore_version = FALSE) {
    self::initConfig();
    if (!$ignore_version && !self::checkServerVersion()) {
      return FALSE;
    }
    $options += array(
      'method' => 'GET',
      'headers' => array(),
    );
    $options['headers'] += array(
      'NodejsServiceKey' => self::$config['serviceKey'],
      'Content-type' => 'application/json',
    );
    $response = drupal_http_request(self::$baseUrl . $url, $options);

    // If a http error occurred, and logging of http errors is enabled, log it.
    if (isset($response->error)) {
      if (self::$config['log_http_errors']) {
        $params = array(
          '%code' => $response->code,
          '%error' => $response->error,
          '%url' => $url,
        );
        $log_message = 'Error reaching the Node.js server at "%url": [%code] %error.';
        if (!empty($options['data'])) {
          $params['data'] = $options['data'];
          $log_message = 'Error reaching the Node.js server at "%url" with data "%data": [%code] %error.';
        }
        watchdog('nodejs', $log_message, $params);
      }
      return FALSE;
    }

    // No errors, so return Node.js server response.
    return json_decode($response->data);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Nodejs::$baseUrl public static property
Nodejs::$config public static property
Nodejs::$messages public static property
Nodejs::$nodeServerVersion public static property
Nodejs::addChannel public static function
Nodejs::addUserToChannel public static function
Nodejs::checkChannel public static function
Nodejs::checkServerVersion public static function
Nodejs::enqueueMessage public static function
Nodejs::getContentTokenUsers public static function
Nodejs::getMessages public static function
Nodejs::getServerVersion public static function
Nodejs::healthCheck public static function
Nodejs::httpRequest public static function
Nodejs::initConfig public static function
Nodejs::kickUser public static function
Nodejs::logoutUser public static function
Nodejs::removeChannel public static function
Nodejs::removeUserFromChannel public static function
Nodejs::sendContentToken public static function
Nodejs::sendContentTokenMessage public static function
Nodejs::sendMessage public static function
Nodejs::sendMessages public static function
Nodejs::setUserPresenceList public static function