pydonkey-general
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Pydonkey-general] Re: Todo for pysocket


From: BuziFuzee
Subject: Re: [Pydonkey-general] Re: Todo for pysocket
Date: Sat, 30 Aug 2003 17:15:22 +0200
User-agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.4) Gecko/20030624 Netscape/7.1 (ax)



Joël Vennin wrote:
7.) Integrating user-defined data must be made easier, when it is needed
in the protocol
       Solution: Option to add a data-container to the protocol
    
Explain more ...
  

As example how I added my Main-Application-Class to the protocol and factory in my webgui:

from pysocket import *
import struct

class WebGuiProtocol (protocol.Protocol):

    def connectionMade (self):
        self.inbuffer=''
        self.packetsender.Login('diddi','duddu')

    def dataReceived (self):
        self.inbuffer = self.inbuffer + self.transport.read ()
        #print "Received: %s " % buffer
        #self.transport.write (buffer)
        try:
            bufferlen=len(self.inbuffer)
            packetlen=struct.unpack('<I', self.inbuffer[1:5])
            packetlen=packetlen[0]+5   
            while (bufferlen > 5) and (packetlen<=bufferlen):
                self.packetreceiver.dispatch(self.inbuffer[:packetlen])
                self.inbuffer=self.inbuffer[packetlen:]
                #self.packetreceiver.logger.debug("next: "+`self.inbuffer`)
                bufferlen=len(self.inbuffer)
                if (bufferlen > 5):
                    packetlen=struct.unpack('<I', self.inbuffer[1:5])
                    packetlen=packetlen[0]+5
                    #self.packetreceiver.logger.debug(`bufferlen`+`packetlen`)
        except:
            self.packetreceiver.logger.debug("Unexpected error:", sys.exc_info()[0])


      

class MyClientF(client.ClientFactory):

    def setmainclass(self, mainclass):
        self.mainclass=mainclass

    def buildProtocol (self, addr):
        """
        This method create an instance of self.protocol
        @param addr: the tuple address and port
        @type addr: tuple
        """
        #        self.logentry( "Factory::buildProtocol"
        a=self.protocol()
        a.packetsender=self.mainclass.packetsender
        a.packetreceiver=self.mainclass.packetreceiver
        self.mainclass.packetsender.setprotocol(a)
        return a
   
class Connection:
       
    def __init__(self, IP, Port, WebGUIMain):
        self.logger=WebGUIMain.logger

        self.b = bandwidth.BandwidthControl ( 10000000, 10000000)
   

        self.f = MyClientF (self.b)
        self.f.setmainclass(WebGUIMain)
        self.f.setlogger(self.logger)
        self.f.protocol = WebGuiProtocol

        self.m = manager.PySocket ()
        self.m.addBandwidthControl (self.b)
        self.m.setlogger(self.logger)
       
        self.m.TCPConnection(IP,Port,self.f)
        self.m.start ()       


Everything marked green should be added as a function in protocol, which could be called:
    def ispacketcomplete()  # is called everytime when data was received, to check whether we have a full packet)
    def packetcomplete() # is called when a full packet is complete


Everything marked red, did I have to add, to get my Applicationdata down to the protocol. That should be made easier.

For example in Factory:
    def setdatacontainer(datacontainer) # sets automatically the datacontainer to the protocol, as well to the Factory

Cheers,
Spikeee
   
P.S.: Oh boy, the mailinglist took about 2 hours to get the last message to the list. Because of that I sent it twice.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]