33namespace Sock ;
44
55require_once "SocketServer.php " ;
6+ require_once "SocketClientBroadcast.php " ;
67
78class SocketServerBroadcast extends SocketServer {
89
910 const PIPENAME = '/tmp/broadcastserver.pid ' ;
1011
11- private static $ pid ;
12-
13- protected $ pipe ;
12+ protected $ pid ;
13+ public $ pipe ;
1414
1515 private $ connections = array ();
1616
1717 public function __construct ( $ port = 4444 , $ address = '127.0.0.1 ' ) {
1818 parent ::__construct ( $ port , $ address );
19- self :: $ pid = posix_getpid ();
19+ $ this -> pid = posix_getpid ();
2020 if (!file_exists (self ::PIPENAME )) {
2121 umask (0 );
2222 if ( ! posix_mkfifo (self ::PIPENAME , 0666 ) ) {
23- die ('Cant create a pipe: ' . self ::PIPENAME );
23+ die ('Cant create a pipe: " ' . self ::PIPENAME . ' " ' );
2424 }
2525 }
26-
2726 $ this ->pipe = fopen (self ::PIPENAME , 'r+ ' );
2827 }
2928
3029 public function handleProcess () {
31- $ len = $ this ->bytesToInt ( fread ($ this ->pipe , 4 ) );
30+ $ header = fread ($ this ->pipe , 4 );
31+ $ len = $ this ->bytesToInt ( $ header );
32+
3233 $ message = unserialize ( fread ( $ this ->pipe , $ len ) );
34+
3335 if ( $ message ['type ' ] == 'msg ' ) {
3436 $ client = $ this ->connections [ $ message ['pid ' ] ];
3537 $ msg = sprintf ('[%s] (%d):%s ' , $ client ->getAddress (), $ message ['pid ' ], $ message ['data ' ] );
@@ -59,19 +61,39 @@ protected function beforeServerLoop() {
5961 parent ::beforeServerLoop ();
6062 socket_set_nonblock ( $ this ->sockServer );
6163 pcntl_signal (SIGUSR1 , array ($ this , 'handleProcess ' ), true );
64+ //pcntl_signal(SIGINT, array($this, 'stop'), true);
65+ }
66+
67+ protected function stop () {
68+ echo "\nClosing active connections... \n" ;
69+ $ this ->_listenLoop = false ;
70+ foreach ( $ this ->connections as $ conn ) {
71+ $ conn ->close ();
72+ }
73+ echo "Shutting down server... \n" ;
74+ fclose ($ this ->pipe );
75+ $ this ->pipe = fopen (self ::PIPENAME , 'w ' );
76+ fclose ($ this ->pipe );
77+ echo unlink ( self ::PIPENAME ) ? 'removed ' : 'not removed ' ;
78+ pcntl_wait ($ status );
6279 }
6380
6481 protected function serverLoop () {
6582 while ( $ this ->_listenLoop ) {
6683 if ( ( $ client = @socket_accept ( $ this ->sockServer ) ) === false ) {
6784 $ info = array ();
68- if ( pcntl_sigtimedwait (array (SIGUSR1 ),$ info ,1 ) > 0 ) {
69- $ this ->handleProcess ();
85+ if ( pcntl_sigtimedwait (array (SIGUSR1 , SIGINT ),$ info ,1 ) > 0 ) {
86+ if ( $ info ['signo ' ] == SIGINT ) {
87+ $ this ->stop ();
88+ }
89+ else {
90+ $ this ->handleProcess ();
91+ }
7092 }
7193 continue ;
7294 }
7395
74- $ socketClient = new SocketClient ( $ client );
96+ $ socketClient = new SocketClientBroadcast ( $ client, $ this );
7597
7698 if ( is_array ( $ this ->connectionHandler ) ) {
7799 $ object = $ this ->connectionHandler [0 ];
@@ -83,26 +105,35 @@ protected function serverLoop() {
83105 $ childPid = $ function ( $ socketClient );
84106 }
85107
108+ if ( ! $ childPid ) {
109+ // force child process to exit from loop
110+ return ;
111+ }
112+
86113 $ this ->connections [ $ childPid ] = $ socketClient ;
87114 }
88- unlink ( self :: PIPENAME );
115+
89116 }
90117
91- static function broadcast ( Array $ msg ) {
118+ public function broadcast ( Array $ msg ) {
92119 $ msg ['pid ' ] = posix_getpid ();
93- $ message = serialize ( $ msg );
94- $ f = fopen (self ::PIPENAME , 'w ' );
95- fwrite ($ f , self ::strlenInBytes ($ message ) . $ message );
120+ $ message = serialize ( $ msg );
121+ $ f = fopen (self ::PIPENAME , 'w+ ' );
122+ if ( !$ f ) {
123+ echo "ERROR: Can't open PIPE for writting \n" ;
124+ return ;
125+ }
126+ fwrite ($ f , $ this ->strlenInBytes ($ message ) . $ message );
96127 fclose ($ f );
97- posix_kill (self :: $ pid , SIGUSR1 );
128+ posix_kill ($ this -> pid , SIGUSR1 );
98129 }
99130
100- static function strlenInBytes ($ str ) {
131+ protected function strlenInBytes ($ str ) {
101132 $ len = strlen ($ str );
102133 $ chars = chr ( $ len & 0xFF );
103134 $ chars .= chr ( ($ len >> 8 ) & 0xFF );
104135 $ chars .= chr ( ($ len >> 16 ) & 0xFF );
105136 $ chars .= chr ( ($ len >> 24 ) & 0xFF );
106137 return $ chars ;
107138 }
108- }
139+ }
0 commit comments