main.py :  » Web-Services » Shtoom » shtoom-0.2 » shtoom » ui » textui » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Web Services » Shtoom 
Shtoom » shtoom 0.2 » shtoom » ui » textui » main.py

from twisted.python import log
from shtoom.ui.base import ShtoomBaseUI
from twisted.internet import stdio
from twisted.protocols import basic

class ShtoomMain(basic.LineReceiver, ShtoomBaseUI):
    _cookie = None
    _pending = None
    _debug = True
    from os import linesep

    def debugMessage(self, msg):
        if self._debug:
            print msg

    def statusMessage(self, msg):
        if self._debug:
            print "status", msg

    def errorMessage(self, message, exc=None):
        log.msg("error %s"%(message))

    def shutdown(self):
        # XXX Hang up any calls
        from twisted.internet import reactor
        reactor.stop()

    def incomingCall(self, description, call, defresp, defsetup):
        self._pending = ( call, defresp, defsetup )
        self.transport.write("INCOMING CALL: %s\n"%description)
        self.transport.write("Type 'accept' to accept, 'reject' to reject\n")

    def connectionMade(self):
        self.transport.write("Welcome to shtoom\n>> ")

    def lineReceived(self, line):
        args = line.strip().split()
        if args:
            cmd = args[0].lower()
            if hasattr(self, "cmd_%s"%cmd):
                getattr(self, "cmd_%s"%(cmd))(line)
            elif cmd == "?":
                self.cmd_help(line)
            else:
                self.transport.write("Unknown command '%s'\n"%(cmd))
        self.transport.write(">> ")

    def cmd_help(self, line):
        methods = [ x[4:] for x in dir(self) if x[:4] == "cmd_" ]
        self.transport.write("Commands: %s\n"%(' '.join(methods)))

    def cmd_call(self, line):
        if self._cookie is not None:
            self.transport.write("error: only one call at a time for now\n")
            return
        args = line.split()
        if len(args) != 2:
            self.transport.write("error: call <sipurl>\n")
            return
        if not args[1].startswith('sip:'):
            self.transport.write("error: call <sipurl>\n")
            return
        self.sipURL = args[1]
        deferred = self.app.placeCall(self.sipURL)
        deferred.addCallbacks(self.callConnected, self.callDisconnected).addErrback(log.err)

    def callStarted(self, cookie):
        self._cookie = cookie
        log.msg("Call to %s STARTED"%(self.sipURL))

    def callConnected(self, cookie):
        log.msg("Call to %s CONNECTED"%(self.sipURL))

    def callFailed(self, e, message=None):
        log.msg("Call to %s FAILED: %r"%(self.sipURL, e))

    def callDisconnected(self, cookie, message):
        log.msg("Call to %s DISCONNECTED"%(self.sipURL))
        self._cookie = None

    def cmd_hangup(self, line):
        if self._cookie is not None:
            self.app.dropCall(self._cookie)
        else:
            self.transport.write("error: no active call\n")

    def cmd_quit(self, line):
        self.shutdown()

    def cmd_accept(self, line):
        if not self._pending:
            self.transport.write("no pending calls")
            return
        self._cookie, resp, setup = self._pending
        self._pending = None
        setup.addCallbacks(self.callConnected, self.callFailed).addErrback(log.err)
        resp.callback('yes')

    def cmd_reject(self, line):
        if not self._pending:
            self.transport.write("no pending calls")
            return
        self._cookie, resp, setup = self._pending
        self._pending = None
        resp.errback('no')
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.