Minor fixes
[MonkeyD.git] / palm / lib / proc.py
blobe7e6138bb75d9072e6d119acaebb588bb3676b42
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
17 import os
18 import sys
19 import signal
20 import time
21 from epoll import *
22 from child import Child
23 from listener import Listener
24 from config import PalmConfig
26 VERSION = 0.1
28 class BigPalm:
29 def __init__(self):
30 self.print_info()
32 # Read Palm configuration
33 self.conf = PalmConfig()
34 self.conf.readconf("conf/palm.conf")
36 signal.signal(signal.SIGINT, self._on_sigint_cb)
37 self._palms = []
39 # Load Palms
40 self.create_palms()
42 def _on_sigint_cb(self, frame, a):
43 for p in self._palms:
44 p.kill_childs()
46 exit(0)
47 def print_info(self):
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
56 try:
57 port = int(self.conf.get(h, 'Port'))
58 except:
59 self.conf_error(h, 'Port')
61 try:
62 childs = int(self.conf.get(h, 'Childs'))
63 except:
64 self.conf_error(h, 'Childs')
66 try:
67 bin = self.conf.get(h, 'Exec')
68 except:
69 bin = None
71 try:
72 opts = self.conf.get(h, 'Arguments')
73 except:
74 opts = None
76 p = Palm(h, port, bin, opts)
77 p.create_n_childs(childs)
78 p.start_monitor()
79 self._palms.append(p)
81 while 1:
82 time.sleep(1)
84 def conf_error(self, h, var):
85 print "\nPalm config error, no option '%s' in handler '%s'" % (var, h)
86 exit(1)
88 class Palm:
89 _childs = []
91 def __init__(self, name, port, bin, opts):
92 self.name = name
93 self.port = port
94 self.bin = bin
95 self.opts = opts
97 self.listen = Listener(port)
98 self.s = self.listen.s
100 def create_n_childs(self, n):
101 for i in range(n):
102 self.create_child()
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):
112 return self._childs
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):
130 self._print_setup()
131 clen = self.get_childs_len()
133 ep = EPoll()
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
147 # childs
148 while(1):
149 events = _ev()
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:
165 c.kill()