lproto.py :  » Development » Leo » Leo-4.7.1-final » leo » external » 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 » Development » Leo 
Leo » Leo 4.7.1 final » leo » external » lproto.py
#@+leo-ver=4-thin
#@+node:ville.20091010232339.6117:@thin ../external/lproto.py
#@@language python

#@<< docstring >>
#@+node:ville.20091010205847.1364:<< docstring >>
""" lproto - simple local socket protocol dispatcher (reactor) for PyQt 

Author: Ville M. Vainio <vivainio@gmail.com>

"""
#@nonl
#@-node:ville.20091010205847.1364:<< docstring >>
#@nl

#@<< imports >>
#@+node:ville.20091009234538.1373:<< imports >>
# todo move out qt dep
from PyQt4 import QtCore,QtNetwork
import socket
import struct
#@nonl
#@-node:ville.20091009234538.1373:<< imports >>
#@nl

#@+others
#@+node:ville.20091010205847.1363:sending
def mk_send_bytes(msg):
    lendesc = struct.pack('I', len(msg))
    return lendesc + msg

#@-node:ville.20091010205847.1363:sending
#@+node:ville.20091010205847.1362:class LProtoBuf
class LProtoBuf:
    def __init__(self):

        self.plen = -1
        self.buf = ""

    def set_recv_cb(self, cb):
        """ set func to call with received messages """
        self.recv_cb = cb
    def get_rlen(self):
        # read pkg length
        if self.plen == -1:
            return 4
        return self.plen - len(self.buf)

    def push_bytes(self, allbytes):
        while allbytes:
            rlen = self.get_rlen()
            byts = allbytes[0:rlen]
            self.push_bytes_one(byts)
            allbytes = allbytes[rlen:]

    def push_bytes_one(self, byts):
        if self.plen == -1:
            lendesc = byts[0:4]
            intlen = struct.unpack('I', lendesc)[0]
            print("have", intlen, "bytes")
            self.plen = intlen
            self.buf = byts[4:]
        else:
            self.buf = self.buf + byts

        if len(self.buf) == self.plen:
            print("dispatch msg", self.buf)
            self.recv_cb(self.buf)
            self.buf = ""
            self.plen = -1
            return

        print("in buf",self.buf)
#@-node:ville.20091010205847.1362:class LProtoBuf
#@+node:ville.20091009234538.1374:class LProtoServer
class LProtoServer:
    #@    @+others
    #@+node:ville.20091009234538.1380:methods
    def __init__(self):
        self.srv = QtNetwork.QLocalServer()
        self.srv.connect(self.srv, QtCore.SIGNAL("newConnection()"),
            self.connected)
        self.receiver = None

        self.ses = {}  

    def listen(self, name):
        self.srv.listen(name)
        print("listen on",self.srv.fullServerName())

    def msg_received(self, msg, ses):
        if self.receiver:
            self.receiver(msg, ses)

    def set_receiver(self, receiver):
        self.receiver = receiver

    def connected(self):
        print("hnd con")
        lsock = self.srv.nextPendingConnection()
        print("conn", lsock)
        buf =  LProtoBuf()

        self.ses[lsock] = ses_ent = {'_sock' : lsock, '_buf' : buf }

        def msg_recv_cb(msg):
            self.msg_received(msg, ses_ent)

        buf.set_recv_cb( msg_recv_cb )


        def readyread_cb():
            print("read ready")
            allbytes = lsock.readAll()
            buf = ses_ent['_buf']
            buf.push_bytes(allbytes)

        lsock.connect(lsock, QtCore.SIGNAL('readyRead()'), readyread_cb)
        #self.connect(self.qsock, SIGNAL('connectionClosed()'), self.handleClosed)


    def readyread(self):
        pass

    #@-node:ville.20091009234538.1380:methods
    #@-others
#@-node:ville.20091009234538.1374:class LProtoServer
#@+node:ville.20091010205847.1360:(ignore) class LProtoObsoleteClient
class LProtoObsoleteClient:
    #@    @+others
    #@+node:ville.20091010205847.1361:initialization
    def __init__(self):
        self.cl = QtNetwork.QLocalSocket()

    def connect(self, name):
        self.cl.connectToServer(name)
        print("client connected")
    #@-node:ville.20091010205847.1361:initialization
    #@-others
#@-node:ville.20091010205847.1360:(ignore) class LProtoObsoleteClient
#@+node:ville.20091010233144.10051:class LProtoClient
class LProtoClient:

    def __init__(self, fname):

        self.socket = socket.socket( socket.AF_UNIX, socket.SOCK_STREAM)
        self.socket.connect (fname)
        self.recvbuf = LProtoBuf()

    def send(self, msg):
        byts = mk_send_bytes(msg)
        self.socket.sendall(byts)


#@-node:ville.20091010233144.10051:class LProtoClient
#@-others
#@nonl
#@-node:ville.20091010232339.6117:@thin ../external/lproto.py
#@-leo
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.