/
Server.php
82 lines (64 loc) · 2.08 KB
/
Server.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<?php
/************************************************************
* icarus v1.0-beta -- an IRC framework for PHP *
* Author: rintaun - Matthew J. Lanigan <rintaun@gmail.com> *
* *
* Copyright 2011 Matthew J. Lanigan. *
* See LICENSE file for licensing restrictions *
************************************************************/
if (!defined('_ICARUS_')) die('This script may not be invoked directly.' . "\n");
abstract class Server extends EventHandler {
private $sid = "";
private $config;
public $modules = array();
final public function __construct($name, $config)
{
$this->config = $config;
if ((!isset($config['listen'])) || (!isset($config['port'])))
_die("Server %s: Didn't get a listening address or port value!", $name);
$SH = SocketHandler::getInstance();
// $this->sid = $SH->createListener(etc. etc. etc.)
$this->loadModules();
$this->_create($name, $config);
}
final public function __destruct()
{
$this->_destroy();
}
public function loadModules()
{
if ((isset($this->config['module'])) && (is_array($this->config['module'])))
foreach ($this->config['module'] AS $key => $entry)
{
$keyinfo = explode(":", $key);
$type = 'Module_' . $keyinfo[0];
$name = $keyinfo[1];
if (file_exists($GLOBALS['modulesdir'] . $type . '.php'))
{
require_once($GLOBALS['modulesdir'] . $type . '.php');
$this->modules[] = new $type($this, $name, $this->config['modules'][$key]);
}
else
_log(L_WARNING, '%s: could not find %s', get_called_class(), $type);
}
}
public function connect($sid)
{
// todo: implement this.
}
public function read($sid, $data)
{
$this->parse($data);
}
public function write($format)
{
if (strlen($format) == 0) return;
$SH = SocketHandler::getInstance();
$args = func_get_args();
array_unshift($args, $this->sid);
call_user_func_array(array($SH, 'send'), $args);
}
abstract function parse($data);
abstract function _create($name, $config);
abstract function _destroy();
}