Skip to content

Commit 2d615a9

Browse files
committed
Removed .class file-extension. Backward compatibility was retained.
1 parent 2566ab6 commit 2d615a9

33 files changed

+7345
-3
lines changed

app-examples/ExampleWebSocket.php

100644100755
File mode changed.

bin/phpd

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,12 @@ spl_autoload_register(function($classname) {
3030
array_shift($parts);
3131
}
3232

33-
$path = 'lib/' . implode('/', $parts) . '.class.php';
33+
$pathes = array('lib/' . implode('/', $parts) . '.php', 'lib/' . implode('/', $parts) . '.class.php');
3434

35-
if (@include($path)) {
36-
return;
35+
foreach ($pathes as $path) {
36+
if (@include($path)) {
37+
return;
38+
}
3739
}
3840

3941
if (

lib/AppInstance.php

Lines changed: 284 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,284 @@
1+
<?php
2+
3+
/**
4+
* Application instance
5+
*
6+
* @package Core
7+
*
8+
* @author Zorin Vasily <[email protected]>
9+
*/
10+
class AppInstance {
11+
12+
public $status = 0; // runtime status
13+
public $passphrase; // optional passphrase
14+
public $reqCounter = 0; // counter of requests
15+
public $queue = array(); // queue of requests
16+
public $ready = FALSE; // ready to start?
17+
public $name; // name of instance
18+
public $config;
19+
20+
/**
21+
* Application constructor
22+
* @return void
23+
*/
24+
public function __construct($name = '') {
25+
$this->name = $name;
26+
27+
$appName = get_class($this);
28+
Daemon::$process->log($appName . ($name ? "-{$name}" : '') . ' instantiated.');
29+
$appNameLower = strtolower($appName);
30+
$fullname = Daemon::$appResolver->getAppFullName($appName, $this->name);
31+
if (!isset(Daemon::$appInstances[$appNameLower])) {
32+
Daemon::$appInstances[$appNameLower] = array();
33+
}
34+
Daemon::$appInstances[$appNameLower][$this->name] = $this;
35+
36+
if (!isset(Daemon::$config->{$fullname})) {
37+
38+
Daemon::$config->{$fullname} = new Daemon_ConfigSection;
39+
} else {
40+
if (
41+
!isset(Daemon::$config->{$fullname}->enable)
42+
&& !isset(Daemon::$config->{$fullname}->disable)
43+
) {
44+
Daemon::$config->{$fullname}->enable = new Daemon_ConfigEntry;
45+
Daemon::$config->{$fullname}->enable->setValue(TRUE);
46+
}
47+
}
48+
49+
$this->config = Daemon::$config->{$fullname};
50+
if(isset($this->config->enable->value) && $this->config->enable->value) {
51+
Daemon::$process->log($appName . ($name ? "-{$name}" : '') . ' up.');
52+
}
53+
54+
$defaults = $this->getConfigDefaults();
55+
if ($defaults) {
56+
$this->processDefaultConfig($defaults);
57+
}
58+
59+
$this->init();
60+
61+
if (Daemon::$process instanceof Daemon_WorkerThread) {
62+
if (!$this->ready) {
63+
$this->ready = true;
64+
$this->onReady();
65+
}
66+
}
67+
}
68+
69+
/**
70+
* Function to get default config options from application
71+
* Override to set your own
72+
* @return array|false
73+
*/
74+
protected function getConfigDefaults() {
75+
return false;
76+
}
77+
78+
/**
79+
* Function handles incoming Remote Procedure Calls
80+
* You can override it
81+
* @param string Method name.
82+
* @param array Arguments.
83+
* @return mixed Result
84+
*/
85+
public function RPCall($method, $args) {
86+
if ($this->enableRPC && is_callable(array($this, $method))) {
87+
return call_user_func_array(array($this, $method), $args);
88+
}
89+
}
90+
91+
92+
/**
93+
* Send broadcast call.
94+
* You can override it
95+
* @param string Method name.
96+
* @param array Arguments.
97+
* @param mixed Callback.
98+
* @return boolean Success.
99+
*/
100+
public function broadcastCall($method, $args = array(), $cb = NULL) {
101+
return Daemon::$process->IPCManager->broadcastCall(
102+
get_class($this) . ($this->name !== '' ? '-' . $this->name : ''),
103+
$method,
104+
$args,
105+
$cb
106+
);
107+
}
108+
109+
110+
/**
111+
* Process default config
112+
* @todo move it to Daemon_Config class
113+
* @param array {"setting": "value"}
114+
* @return void
115+
*/
116+
public function processDefaultConfig($settings = array()) {
117+
foreach ($settings as $k => $v) {
118+
$k = strtolower(str_replace('-', '', $k));
119+
120+
if (!isset($this->config->{$k})) {
121+
if (is_scalar($v)) {
122+
$this->config->{$k} = new Daemon_ConfigEntry($v);
123+
} else {
124+
$this->config->{$k} = $v;
125+
}
126+
} elseif ($v instanceof Daemon_ConfigSection) {
127+
// @todo
128+
} else {
129+
$current = $this->config->{$k};
130+
if (is_scalar($v)) {
131+
$this->config->{$k} = new Daemon_ConfigEntry($v);
132+
} else {
133+
$this->config->{$k} = $v;
134+
}
135+
136+
$this->config->{$k}->setHumanValue($current->value);
137+
$this->config->{$k}->source = $current->source;
138+
$this->config->{$k}->revision = $current->revision;
139+
}
140+
}
141+
}
142+
143+
/**
144+
* Called when the worker is ready to go
145+
* @todo -> protected?
146+
* @return void
147+
*/
148+
public function onReady() { }
149+
150+
/**
151+
* Called when creates instance of the application
152+
* @return void
153+
*/
154+
public function init() {}
155+
156+
/**
157+
* Called when worker is going to update configuration
158+
* @todo call it only when application section config is changed
159+
* @todo rename to onConfigChanged()
160+
* @return void
161+
*/
162+
public function update() {}
163+
164+
/**
165+
* Called when application instance is going to shutdown
166+
* @todo protected?
167+
* @return boolean Ready to shutdown?
168+
*/
169+
public function onShutdown() {
170+
return TRUE;
171+
}
172+
173+
/**
174+
* Create Request
175+
* @todo more description needed
176+
* @param object Request
177+
* @param object Upstream application instance
178+
* @return object Request
179+
*/
180+
public function beginRequest($req, $upstream) {
181+
return FALSE;
182+
}
183+
184+
/**
185+
* Handles the output from downstream requests
186+
* @todo more description
187+
* @param object Request
188+
* @param string The output
189+
* @return void
190+
*/
191+
public function requestOut($r, $s) { }
192+
193+
/**
194+
* Handles the output from downstream requests
195+
* @return void
196+
*/
197+
public function endRequest($req, $appStatus, $protoStatus) { }
198+
199+
/**
200+
* Log something
201+
* @param string - Message.
202+
* @return void
203+
*/
204+
public function log($message) {
205+
Daemon::log(get_class($this) . ': ' . $message);
206+
}
207+
208+
/**
209+
* Shutdown the application instance
210+
* @param boolean Graceful?
211+
* @return void
212+
*/
213+
public function shutdown($graceful = false) {
214+
if (Daemon::$config->logevents->value) {
215+
Daemon::log(__METHOD__ . ' invoked. Size of the queue: ' . sizeof($this->queue) . '.');
216+
}
217+
218+
foreach ($this->queue as &$r) {
219+
if ($r instanceof stdClass) {
220+
continue;
221+
}
222+
223+
$r->finish();
224+
}
225+
226+
return $this->onShutdown();
227+
}
228+
229+
/**
230+
* Handle the request
231+
* @param object Parent request
232+
* @param object Upstream application @todo is upstream really needed?
233+
* @return object Request
234+
*/
235+
public function handleRequest($parent, $upstream) {
236+
$req = $this->beginRequest($parent, $upstream);
237+
238+
if (!$req) {
239+
return $parent;
240+
}
241+
242+
if (Daemon::$config->logqueue->value) {
243+
Daemon::$process->log('request added to ' . get_class($this) . '->queue.');
244+
}
245+
246+
return $req;
247+
}
248+
249+
/**
250+
* Pushes request to the queue
251+
* @todo log warning message and then sometime remove it
252+
* @param object Request
253+
* @return object Request
254+
* @deprecated
255+
*/
256+
public function pushRequest($req) {
257+
return $req;
258+
}
259+
260+
/**
261+
* Handle the worker status
262+
* @param int Status code @todo use constants in method
263+
* @return boolean Result
264+
*/
265+
public function handleStatus($ret) {
266+
if ($ret === 2) {
267+
// script update
268+
$r = $this->update();
269+
}
270+
elseif ($ret === 3) {
271+
// graceful worker shutdown for restart
272+
$r = $this->shutdown(TRUE);
273+
}
274+
elseif ($ret === 5) {
275+
// shutdown worker
276+
$r = $this->shutdown();
277+
} else {
278+
$r = TRUE;
279+
}
280+
281+
return $r;
282+
}
283+
284+
}

0 commit comments

Comments
 (0)