summaryrefslogtreecommitdiffstats
path: root/client/ivamPipeConnector.py
diff options
context:
space:
mode:
Diffstat (limited to 'client/ivamPipeConnector.py')
-rw-r--r--client/ivamPipeConnector.py221
1 files changed, 221 insertions, 0 deletions
diff --git a/client/ivamPipeConnector.py b/client/ivamPipeConnector.py
new file mode 100644
index 0000000..ff0be5f
--- /dev/null
+++ b/client/ivamPipeConnector.py
@@ -0,0 +1,221 @@
+
+import os, sys, time, select, signal
+
+import ivamCore, ivamApi, ivamUtil
+
+pc = None
+
+def sigterm(*args):
+ pc.quit = 1
+
+class PipeConnector(ivamApi.Connector):
+
+ BUFSIZE = 128
+
+ sdtin = sys.stdin
+ stdout = sys.stdout
+
+ playing = False
+ recording = False
+
+ timeout = 0
+ quit = False
+
+ def __init__(self, processor):
+
+ if ivamCore.DEBUG:
+ ivamCore.log("PipeConnector()")
+
+ global pc
+ pc = self
+ signal.signal(signal.SIGTERM, sigterm)
+
+ self.callerNumber = os.getenv("CALLERMSN")
+ self.ringNumber = os.getenv("RINGMSN")
+ self.processor = processor
+
+ def openDtmf(self):
+
+ try:
+ fn = os.environ["DTMFFIFO"]
+ except KeyError:
+ self.dtmfFifo = None
+ return
+
+ self.dtmfFifo = file.open(fn, "rb")
+
+ def playClip(self, fname):
+
+ if ivamCore.DEBUG:
+ ivamCore.log("playClip('%s')" % fname)
+
+ self.stopPlayback()
+
+ self.playFile = ivamUtil.magicFile(fname, "rb")
+ self.playName = fname
+ self.playing = True
+ self.playBuffer = None
+
+ def stopPlayback(self):
+
+ if ivamCore.DEBUG:
+ ivamCore.log("stopPlayback()")
+
+ if self.playing:
+ self.playFile.close()
+ self.playing = False
+
+ processor.onClipFinish(self, self.playName)
+
+ def recordClip(self, fname, gzip = False):
+
+ if ivamCore.DEBUG:
+ ivamCore.log("recordClip('%s')" % fname)
+
+ self.stopRecording()
+
+ if gzip:
+ self.recordFile = gzip.open(fname, "w+b")
+ else:
+ self.recordFile = open(fname, "w+b")
+
+ self.recordName = fname
+ self.recording = True
+
+ def stopRecording(self):
+
+ if ivamCore.DEBUG:
+ ivamCore.log("stopRecording()")
+
+ if self.recording:
+ self.recordFile.close()
+ self.recording = False
+
+ processor.onRecordFinish(self, self.recordName)
+
+ def timeout(self, t):
+
+ if ivamCore.DEBUG:
+ ivamCore.log("timeout(%u)" % t)
+
+ if t:
+ self.timeout = time.time()+t
+ else:
+ self.timeout = 0
+
+
+ def run(self):
+
+ if ivamCore.DEBUG:
+ ivamCore.log("run()")
+
+ self.openDtmf()
+ self.processor.onConnect(self, self.callerNumber, self.ringNumber)
+ self.loop()
+ self.stopPlayback()
+ self.stopRecording()
+ self.processor.onHangup(self)
+ self.dtmfFifo.close()
+
+ if ivamCore.DEBUG:
+ ivamCore.log("run() finished")
+
+ def hangup(self):
+
+ if ivamCore.DEBUG:
+ ivamCore.log("hangup()")
+
+ self.quit = True
+
+ def loop(self):
+
+ while not self.quit:
+
+ if ivamCore.DEBUG:
+ ivamCore.log("iteration")
+
+ t = None
+ now = time.time()
+
+ if self.timeout:
+ t = self.timeout - now
+
+ if t < 0:
+ t = 0
+
+ o = []
+ i = [self.stdin]
+
+ if not self.dtmfFifo is None:
+ i += [self.dtmfFifo]
+
+ if self.playing:
+ o += [self.stdout]
+
+ if t is None:
+ i, o = select.select(i, o, [])
+ else:
+ i, o = select.select(i, o, [], t)
+
+ now = time.time()
+
+ if self.timeout < now:
+ processor.onTimeout()
+ if self.quit: break
+
+ # Check DTMF fifo
+ if not self.dtmfFifo is None and self.dtmfFifo in i:
+ self.handleDtmf()
+ if self.quit: break
+
+ # Check STDOUT
+ if self.stdout in o:
+ self.writeStdout()
+ if self.quit: break
+
+ # Check STDIN
+ if self.stdin in o:
+ self.readStdin()
+ if self.quit: break
+
+
+ def readStdin(self):
+ buf = os.read(self.stdin.fileno(), self.BUFSIZE)
+
+ if buf == "":
+ self.quit = 1
+ elif self.recording:
+ self.recordFile.write(buf)
+
+
+ def writeStdout(self):
+
+ if not self.playing:
+ return
+
+ if self.playBuffer is None:
+
+ self.playBuffer = self.playFile.read(self.BUFSIZE)
+
+ # EOF?
+ if len(self.playBuffer) == 0:
+ self.stopPlayback()
+ return
+
+ c = os.write(self.stdout.fileno(), self.playBuffer)
+ self.playBuffer = self.playBuffer[c:]
+
+ if len(self.playBuffer) == 0:
+ self.playBuffer = None
+
+ def dtmfCheck(self):
+
+ if not self.dtmfFifo is None:
+
+ d = self.dtmfFifoFd.read(1)
+
+ if d == "":
+ self.dtmfFifo.close()
+ self.dtmfFifo = None
+ else:
+ self.onDtmfEvent(self, d)