#!/usr/bin/php
<?php

$docRoot = getenv("DOCUMENT_ROOT");

require_once "DaemonWrapper.php";
$daemon = new DaemonWrapper("my_stellar");
$daemon->start($argv);

require_once $docRoot . "/mobi-config/mobi_lib_constants.php";
require_once LIBDIR . "db.php";
require_once LIBDIR . "StellarData.php";
require_once "apns_lib.php";

while($daemon->sleep(STELLAR_FEED_CACHE_TIMEOUT)) {
  db::ping();  //make sure the db keeps running

  $term = StellarData::get_term();
  $subject_ids = StellarData::check_subscriptions($term);

  foreach($subject_ids as $subject_id) {
    $announcements = StellarData::get_announcements($subject_id);

    if(count($announcements) > 0 ) {
      $notification_types = array(
	'apple' => new StellarAppleNotification($subject_id, $announcements)
      );

      foreach(StellarData::subscriptions_for_subject($subject_id, $term) as $subscription) {
        $notification_types[$subscription['device_type']]->queue_notification($subscription['device_id']);
      }
#!/usr/bin/php
<?
/**** 
 * this daemon regularly polls for stop predictions on each active route
 * doing this keeps caches fresh as a side effect
 * though this isn't the most logical way to keep caches fresh
 *
 * we will check for route-stop subscriptions and notify users
 * whose predictions are below SHUTTLE_NOTIFY_THRESHOLD
 */

define("SHUTTLE_NOTIFY_THRESHOLD", 320);

require_once("DaemonWrapper.php");

$daemon = new DaemonWrapper("shuttle");
$daemon->start($argv);


require_once dirname(__FILE__) . "/../../../mobi-config/mobi_lib_constants.php";
require_once LIB_ROOT . "ShuttleSchedule.php";
require_once LIB_ROOT . "NextBusReader.php";
require_once LIB_ROOT . "db.php";
require_once "apns_lib.php";

ShuttleSchedule::init();
NextBusReader::init();

$all_routes = ShuttleSchedule::get_route_list();

while ($daemon->sleep(10)) {
#!/usr/bin/php
<?php 
define("APNS_PUSH_REST_TIME", 15);
require_once dirname(__FILE__) . "/../../config/mobi_web_constants.php";
require_once "DaemonWrapper.php";
$daemon = new DaemonWrapper("apns_push");
$daemon->start($argv);
require_once 'apns_lib.php';
$apns_server = new ApplePushServersPool(APNS_CONNECTIONS_LIMIT);
// this is a daemon so loop forever
d_echo("push daemon activated");
while ($daemon->sleep(APNS_PUSH_REST_TIME)) {
    d_echo("waiting for messages to send...", False);
    db::ping();
    $messages = APNS_DB::get_unsent_notifications();
    while ($message = APNS_DB::fetch_notification($messages)) {
        $data = $message['payload'];
        $device_id = $message['device_id'];
        $device = APNS_DB::get_device($device_id);
        $module_name = get_module_name($message['tag']);
        // we only send to devices that have not been deactived
        // and we only send to enabled modules
        if ($device['active'] == 1 && APNS_DB::is_module_enabled($device_id, $module_name)) {
            // need to compute the number of unread messages
            // to be displayed on "badge" on the device
            $unreads = $device['unread_notifications'];
            // look for an old version of this message
            $old_position = array_search($message['tag'], $unreads);
            if ($old_position !== FALSE) {
                array_splice($unreads, $old_position, 1);
            }
#!/usr/bin/php
<?php 
define("APNS_FEEDBACK_REST_TIME", 5 * 60 * 60);
require_once "DaemonWrapper.php";
$daemon = new DaemonWrapper("apns_feedback");
$daemon->start($argv);
require_once 'apns_lib.php';
$apns_server = new ApplePushNotificationConnection();
while ($daemon->sleep(APNS_FEEDBACK_REST_TIME)) {
    // this is a daemon so loop forever
    $apns_server->open_feedback_connection();
    $messages = $apns_server->get_feedback_messages();
    db::ping();
    foreach ($messages as $message) {
        d_echo("received a deactivate message from apple for:{$message['device_token']}");
        APNS_DB::record_device_uninstalled_app($message['device_token'], $message['unixtime']);
    }
    $apns_server->close_feedback_connection();
}
$daemon->stop();