-
Notifications
You must be signed in to change notification settings - Fork 0
/
master.py
executable file
·277 lines (215 loc) · 8.82 KB
/
master.py
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#!/usr/bin/env python
#
# Karaka Skype-XMPP Gateway: Master controller
# <http://www.vipadia.com/products/karaka.html>
#
# Copyright (C) 2008-2009 Vipadia Limited
# Copyright (C) 2010 Voxeo Corporation
# Richard Mortier <[email protected]>
# Neil Stratford <[email protected]>
#
## This program is free software; you can redistribute it and/or
## modify it under the terms of the GNU General Public License version
## 2 as published by the Free Software Foundation.
## This program is distributed in the hope that it will be useful, but
## WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
## General Public License version 2 for more details.
## You should have received a copy of the GNU General Public License
## version 2 along with this program; if not, write to the Free
## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
## MA 02110-1301, USA.
import sys, os, logging, locale, codecs, time, signal, threading, pprint
from pyxmpp.all import Iq
## pyrepl appears to conflict with screen - get EINTR in os.read() on reattch
## from pyrepl.unix_console import UnixConsole
## from pyrepl.historical_reader import HistoricalReader
from karaka import *
from karaka.common import *
from karaka.common import _dbg
Debug = 5
def dbg(s, l=0):
if Debug > l: _dbg(s)
WELCOME_MSG = COPYRIGHT_MESSAGE + """\
Karaka Master Controller
"""
GOING_MSG = "Karaka Master Controller exiting - cleaning up..."
GONE_MSG = """\
Karaka Master exited - goodbye!
"""
HELP_MSG = """\
Commands are:
copying | show license information pertaining to copying
debug [n] | show debug level, or set it to n
help | show this help message
quit | quit the slave
state | show bundle state
status | show connection status
warranty | show license information pertaining to warranty
"""
LOGFILE = "/tmp/karaka_master.log"
PIDFILE = "/tmp/karaka_master.pid"
CHAT_PKLFILE = "/tmp/karaka_master_chat.pkl"
GROUPCHAT_PKLFILE = "/tmp/karaka_master_groupchat.pkl"
PROMPT = 'karaka master> '
Running = True
Master = None
Master_thread = None
Muc = None
Muc_thread = None
def sigkill_handler(signal, frame):
err("signal: %s" % signal)
global Running
Running = False
################################################################################
def master_keepalive():
config = masterconfig.MasterConfig()
global Master
Master = chat.Master(config)
keepalive = Iq(to_jid=config.domain, from_jid=config.component, stanza_type="set")
keptalive = 0
while Master.running:
try:
if Master.connection == CONNECTION.idle:
dbg("master connecting")
Master.connect()
Master.connection = CONNECTION.connecting
elif Master.connection == CONNECTION.connecting:
dbg("master connecting...")
if Master.stream: Master.stream.loop_iter(1)
elif Master.connection == CONNECTION.connected:
dbg("looping master")
if Master.stream: Master.stream.loop_iter(1)
Master.idle()
now = time.time()
if now - keptalive > 60:
dbg("master keepalive! %s" % (fmt_evt(keepalive),), 5)
Master.stream.send(keepalive)
keptalive = now
elif Master.connection == CONNECTION.error:
Master.connection = CONNECTION.idle
except Exception, exc:
log_stacktrace(exc)
try:
Master.disconnect()
except Exception, exc:
err("second chance! failing")
log_stacktrace(exc)
Master.connection = CONNECTION.error
time.sleep(1)
log("master exit: jid:%s" % (Master.jid.as_utf8(),))
def muc_keepalive():
config = masterconfig.MasterConfig()
global Muc
Muc = groupchat.Muc(config)
keepalive = Iq(to_jid=config.domain, from_jid=config.muc, stanza_type="set")
keptalive = 0
while Muc.running:
try:
if Muc.connection == CONNECTION.idle:
dbg("muc connecting")
Muc.connect()
Muc.connection = CONNECTION.connecting
elif Muc.connection == CONNECTION.connecting:
dbg("muc connecting...")
if Muc.stream: Muc.stream.loop_iter(1)
Muc.idle()
elif Muc.connection == CONNECTION.connected:
dbg("looping muc")
if Muc.stream: Muc.stream.loop_iter(1)
Muc.idle()
now = time.time()
if now - keptalive > 60:
dbg("muc keepalive! %s" % (fmt_evt(keepalive),), 5)
Muc.stream.send(keepalive)
keptalive = now
elif Muc.connection == CONNECTION.error:
Muc.connection = CONNECTION.idle
except Exception, exc:
log_stacktrace(exc)
try:
Muc.disconnect()
except Exception, exc:
err("second chance! failing")
log_stacktrace(exc)
Muc.connection = CONNECTION.error
time.sleep(1)
log("muc exit: jid:%s" % (Muc.jid.as_utf8(),))
################################################################################
if __name__ == '__main__':
try:
try:
print WELCOME_MSG
signal.signal(signal.SIGINT, sigkill_handler)
signal.signal(signal.SIGTERM, sigkill_handler)
openlog()
if check_running(PIDFILE):
die("Karaka already runnning - check %s" % PIDFILE)
## unicode magic
locale.setlocale(locale.LC_CTYPE, "")
encoding = locale.getlocale()[1]
if not encoding: encoding = "us-ascii"
sys.stdout = codecs.getwriter(encoding)(sys.stdout, errors="replace")
sys.stderr = codecs.getwriter(encoding)(sys.stderr, errors="replace")
## pyxmpp logger setup
logger = logging.getLogger()
logger.addHandler(logging.FileHandler(LOGFILE))
logger.setLevel(logging.DEBUG) # change to DEBUG for higher verbosity
## master
Master_thread = threading.Thread(name="Master", target=master_keepalive)
Master_thread.setDaemon(True)
Master_thread.start()
time.sleep(1)
## groupchat
Muc_thread = threading.Thread(name="MUC", target=muc_keepalive)
Muc_thread.setDaemon(True)
Muc_thread.start()
## loop, processing user input
## reader = HistoricalReader(UnixConsole())
## reader.ps1 = PROMPT
try:
while Running:
## line = reader.readline()
line = raw_input(PROMPT)
if not line or len(line) == 0: continue
cmd = line.split()
if cmd[0] == 'quit':
Running = False
elif cmd[0] in ('help', '?'):
print HELP_MSG
elif cmd[0] in 'copying':
print COPYING_MESSAGE
elif cmd[0] in 'warranty':
print WARRANTY_MESSAGE
elif cmd[0] == 'debug':
if len(cmd) == 2: Debug = int(cmd[1])
print "Debug = %s" % Debug
elif cmd[0] in 'state':
chat.StLock.acquire()
pprint.pprint(chat.St)
chat.StLock.release()
groupchat.StLock.acquire()
pprint.pprint(groupchat.St)
groupchat.StLock.release()
elif cmd[0] in 'status':
print "Status = master:%s, muc:%s" % (
Master.connection, Muc.connection)
except IOError, ioe:
if ioe.errno == 4: ## interrupted system call
err("exception due to signal handling; ignore")
log_stacktrace(ioe)
except EOFError, eof:
Running = False
if os.path.exists(PIDFILE): os.remove(PIDFILE)
except KeyboardInterrupt:
print "^C"
if os.path.exists(PIDFILE): os.remove(PIDFILE)
except Exception, exc:
print "### exception caught! see error log for details"
log_stacktrace(exc)
print GOING_MSG
finally:
if Master_thread: Master_thread.running = False
if Muc_thread: Muc_thread.running = False
print GONE_MSG
os._exit(os.EX_OK)