Skip to content

Commit

Permalink
Configuration save support added for services and gateways
Browse files Browse the repository at this point in the history
  • Loading branch information
nasirbest committed Dec 17, 2017
1 parent 785f486 commit 0944c72
Show file tree
Hide file tree
Showing 10 changed files with 345 additions and 62 deletions.
8 changes: 8 additions & 0 deletions bin/cron.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* Mail : [email protected] *
* *************************************************************** */

use ICT\Core\Service;
use ICT\Core\Task;

// default include is /usr/ictcore/core
Expand All @@ -18,6 +19,13 @@ function cron_process()
// process all pending retries
Task::process_all();

// execute reload method for all available services
// it will restart gateway in case there are new configurations
$listService = Service::load_all();
foreach ($listService as $oService) {
$oService->config_update();
}

// execute email fetch script
// nothing special we just need to include it for execution
include_once('../bin/sendmail/gateway.php');
Expand Down
2 changes: 2 additions & 0 deletions core/Account.php
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,8 @@ public function save()
public function associate($user_id, $aUser = array())
{
Corelog::log("Changing account owner for: $this->account_id from: $this->user_id to: $user_id", Corelog::CRUD);
// we can change created_by field only via direct query
$this->user_id = $user_id; // also update the internal class variable
$query = "UPDATE " . self::$table . " SET created_by=%user_id% WHERE account_id=%account_id%";
$result = DB::query(self::$table, $query, array('user_id' => $user_id, 'account_id' => $this->account_id));
if ($result && !empty($aUser) && is_array($aUser)) {
Expand Down
36 changes: 26 additions & 10 deletions core/Account/Extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,13 @@

use ICT\Core\Account;
use ICT\Core\Service\Voice;
use ICT\Core\Token;

class Extension extends Account
{

/**
* @property-read string $type
* @var string
* @var string
*/
protected $type = 'extension';

Expand All @@ -30,22 +29,39 @@ public static function search($aFilter = array())

public function save()
{
parent::save();

$oToken = new Token();
$oToken->add('account', $this);
$result = parent::save();

// configuration update is required for accounts
$oVoice = new Voice();
$template = $oVoice->template_path($this->type);
$extension = $oToken->render_template($template);
$oVoice->config_save('extension', $this->username, $extension);
$oVoice->config_update_account($this);

return $result;
}

public function delete()
{
// configuration update is required for accounts
$this->active = 0; // disable to delete, no save needed
$oVoice = new Voice();
$oVoice->config_delete('extension', $this->username);
$oVoice->config_update_account($this);

// now it is safe to delete
parent::delete();
}

public function associate($user_id, $aUser = array())
{
parent::associate($user_id, $aUser);
// configuration update is required for accounts
$oVoice = new Voice();
$oVoice->config_update_account($this);
}

public function dissociate()
{
parent::dissociate();
// configuration update is required for accounts
$oVoice = new Voice();
$oVoice->config_update_account($this);
}
}
18 changes: 7 additions & 11 deletions core/Provider/Sip.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

use ICT\Core\Provider;
use ICT\Core\Service\Voice;
use ICT\Core\Token;

class Sip extends Provider
{
Expand All @@ -32,25 +31,22 @@ public function save()
{
$result = parent::save();

$oToken = new Token();
$oToken->add('provider', $this);

// configuration update is required for providers
$oVoice = new Voice();
$template = $oVoice->template_path('sip');
$aSetting = $oToken->render_template($template);
$oVoice->config_delete('sip', $this->name);
$oVoice->config_save('sip', $this->name, $aSetting);
$oVoice->config_reload();
$oVoice->config_update_provider($this);

return $result;
}

public function delete()
{
// configuration update is required for providers
$this->active = 0; // disable to delete, no save needed
$oVoice = new Voice();
$oVoice->config_delete('sip', $this->username);
$oVoice->config_update_provider($this);

// now it is safe to delete
parent::delete();
$oVoice->config_reload();
}

}
22 changes: 9 additions & 13 deletions core/Provider/Smpp.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

use ICT\Core\Provider;
use ICT\Core\Service\Sms;
use ICT\Core\Token;

class Smpp extends Provider
{
Expand All @@ -32,25 +31,22 @@ public function save()
{
$result = parent::save();

$oToken = new Token();
$oToken->add('provider', $this);

$oVoice = new Sms();
$template = $oVoice->template_path('smpp');
$aSetting = $oToken->render_template($template);
$oVoice->config_delete('smpp', $this->name);
$oVoice->config_save('smpp', $this->name, $aSetting);
$oVoice->config_reload();
// configuration update is required for providers
$oSms = new Sms();
$oSms->config_update_provider($this);

return $result;
}

public function delete()
{
$oVoice = new Sms();
$oVoice->config_delete('smpp', $this->username);
// configuration update is required for providers
$this->active = 0; // disable to delete, no save needed
$oSms = new Sms();
$oSms->config_update_provider($this);

// now it is safe to delete
parent::delete();
$oVoice->config_reload();
}

}
127 changes: 108 additions & 19 deletions core/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ class Service
const MESSAGE_CLASS = 'Message';
const GATEWAY_CLASS = 'Gateway';

const STATUS_READY = 0;
const STATUS_NEED_RELOAD = 1;

public function __construct()
{
// nothing to do
Expand Down Expand Up @@ -53,7 +56,12 @@ public function is_supported($feature, $type = 'application')
}
}

public static function load($service_flag) {
/**
* ********************************************* Services related function **
*/

public static function _load()
{
static $serviceMap = null;

if (empty($serviceMap)) {
Expand All @@ -66,15 +74,45 @@ public static function load($service_flag) {
}
}

if (!empty($service_flag) && isset($serviceMap[$service_flag])) {
return $serviceMap;
}

/**
* Get all available services
*
* @return Service[]
*/
public static function load_all()
{
$serviceMap = self::_load();
$serviceList = array();
foreach ($serviceMap as $className) {
$serviceList[] = new $className;
}
return $serviceList;
}

/**
* Get Service object by service_flag
* @param int $service_flag
*
* @return null|Service
*/
public static function load($service_flag)
{
$serviceMap = self::_load();
if (isset($serviceMap[$service_flag])) {
$className = $serviceMap[$service_flag];
$oService = new $className;
return $oService;
} else {
return false;
}
return null;
}

/**
* ******************************************* Default Gateway for service **
*/

public static function get_gateway() {
static $oGateway = NULL;
if (empty($oGateway)) {
Expand All @@ -83,14 +121,6 @@ public static function get_gateway() {
return $oGateway;
}

public static function get_message() {
static $oMessage = NULL;
if (empty($oMessage)) {
$oMessage = new Message();
}
return $oMessage;
}

public static function get_route() {
$aFilter = array(
'active' => 1,
Expand All @@ -105,6 +135,22 @@ public static function get_route() {
throw new CoreException('404', 'No provider available');
}

/**
* ******************************************* Default message for service **
*/

public static function get_message() {
static $oMessage = NULL;
if (empty($oMessage)) {
$oMessage = new Message();
}
return $oMessage;
}

/**
* ***************************************** Application related functions **
*/

public static function template_path($template_name)
{
Corelog::log("Service->template_path demo. name: $template_name", Corelog::WARNING);
Expand All @@ -125,22 +171,65 @@ public function application_execute(Application $oApplication, $command = '', $c
}
}

public function config_save($config_type, $config_name = 'default', $aSetting = array())
/**
* *************************************** Configuration related functions **
*/

public static function config_status($new_status = null)
{
$oGateway = $this->get_gateway();
$oGateway->config_save($config_type, $config_name, $aSetting);
$status_variable = 'service_' . static::SERVICE_TYPE . '_status';
$current_status = Conf::get($status_variable, static::STATUS_READY);
if ($new_status === null) {
return $current_status;
}

Conf::set($status_variable, $new_status);
return $current_status;
}


public function config_delete($config_type, $config_name = 'default')
public function config_update()
{
$oGateway = $this->get_gateway();
$oGateway->config_delete($config_type, $config_name);
$status = $this->config_status();
if (($status | static::STATUS_NEED_RELOAD) == static::STATUS_NEED_RELOAD) {
if ($this->config_reload()) {
$this->config_status(static::STATUS_READY);
}
}
}

public function config_reload()
public function config_update_account(Account $oAccount)
{
Corelog::log("Service->config_update_account demo. account_id: " . $oAccount->account_id, Corelog::WARNING);
}

public function config_update_user(User $oUser)
{
Corelog::log("Service->config_update_user demo. username: " . $oUser->username, Corelog::WARNING);
}

public function config_update_provider(Provider $oProvider)
{
Corelog::log("Service->config_update_provider demo. name: " . $oProvider->name, Corelog::WARNING);
}

public function config_update_reload()
{
$oGateway = $this->get_gateway();
$oGateway->config_reload();
}

protected function config_save($config_type, Token $oToken, $config_name = 'default')
{
$template = $this->template_path($config_type);
$aSetting = $oToken->render_template($template);
$oGateway = $this->get_gateway();
$oGateway->config_save($config_type, $config_name, $aSetting);
}

protected function config_delete($config_type, $config_name = 'default')
{
$oGateway = $this->get_gateway();
$oGateway->config_delete($config_type, $config_name);
}
}
Loading

0 comments on commit 0944c72

Please sign in to comment.