1 # Copyright (C) 2008-2009, Eduardo Silva <edsiper@gmail.com>
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software
15 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 from child
import Child
23 from listener
import Listener
24 from config
import PalmConfig
32 # Read Palm configuration
33 self
.conf
= PalmConfig()
34 self
.conf
.readconf("conf/palm.conf")
36 signal
.signal(signal
.SIGINT
, self
._on
_sigint
_cb
)
42 def _on_sigint_cb(self
, frame
, a
):
48 print "Monkey Palm Server", VERSION
49 print "Visit us: http://www.monkey-project.com"
51 def create_palms(self
):
52 print self
.conf
.get_handlers()
54 for h
in self
.conf
.get_handlers():
55 # Handler configuration
57 port
= int(self
.conf
.get(h
, 'Port'))
59 self
.conf_error(h
, 'Port')
62 childs
= int(self
.conf
.get(h
, 'Childs'))
64 self
.conf_error(h
, 'Childs')
67 bin
= self
.conf
.get(h
, 'Exec')
72 opts
= self
.conf
.get(h
, 'Arguments')
76 p
= Palm(h
, port
, bin
, opts
)
77 p
.create_n_childs(childs
)
84 def conf_error(self
, h
, var
):
85 print "\nPalm config error, no option '%s' in handler '%s'" % (var
, h
)
91 def __init__(self
, name
, port
, bin
, opts
):
97 self
.listen
= Listener(port
)
98 self
.s
= self
.listen
.s
100 def create_n_childs(self
, n
):
104 def create_child(self
):
105 child
= Child(self
.s
, self
)
106 self
._register
_child
(child
)
108 def _register_child(self
, child
):
109 self
._childs
.append(child
)
111 def get_child_list(self
):
114 def get_childs_len(self
):
115 return len(self
._childs
)
117 def ping_childs(self
):
118 for c
in self
._childs
:
119 c
.write_to_child('ping...')
121 for c
in self
._childs
:
122 os
.waitpid(c
.get_pid(), 0)
125 def _print_setup(self
):
126 print "** Handler '%s' running on port %s, %s childs" % \
127 (self
.name
, self
.port
, self
.get_childs_len())
129 def start_monitor(self
):
131 clen
= self
.get_childs_len()
134 efd = ep.epoll_create(clen)
136 # Add child read channels to epoll queue
137 for child in self._childs:
138 event = epoll_event()
139 event.events = EPOLLIN | EPOLLERR | EPOLLHUP
140 event.data.fd = child.ext_r
141 ep.epoll_ctl(efd, EPOLL_CTL_ADD, child.ext_r, event)
143 _ev = epoll_event * clen
145 # Start monitor loop, waiting for incomming
146 # data comming from childs
151 n_fds = ep.epoll_wait(efd, events, clen, -1)
153 for p in range(n_fds):
154 if events[p].events & EPOLLIN:
155 fd = events[p].data.fd
156 self._read_data(events[p].data.fd)
159 def _read_data(self
, fd
):
160 buf
= os
.read(fd
, 1024)
161 print "Parent read: ", buf
163 def kill_childs(self
):
164 for c
in self
._childs
: