Changeset 42

Show
Ignore:
Timestamp:
02/07/08 15:46:20 (4 years ago)
Author:
mike
Message:

updated stuff

Files:
4 modified

Legend:

Unmodified
Added
Removed
  • cometd.py

    r41 r42  
    301301                                print "I'm delivering foo &&&& %s" % simplejson.dumps 
    302302 
     303                                print "$$$$$ delivering %s" % self.backlog 
    303304                                self.stream.write( 
    304305                                        self.ctypeProps["envelope"] % ( 
     
    322323                        self.stream = stream.ProducerStream() 
    323324                        self.deliver() 
     325 
     326class ClientSubscription(object): 
     327         
     328 
     329        def __init__( self, channel ): 
     330                self.channel = channel 
     331                self.sendMessageCallback = None #Make sure we set this!!! 
     332 
     333 
     334        def messageReceived(self, message): 
     335                self.sendMessage(message) 
     336         
     337        #Make sure you call this first if you override this message!!! 
     338        def sendMessage(self, message): 
     339                self.sendMessageCallback(message, self.channel) 
     340         
     341        #Make sure you call this first if you override this message!!! 
     342        def subscribe(self, channel): 
     343                return True 
     344         
     345        #when we get disconnected or unsubscribed 
     346        def unsubscribe(self): 
     347                pass 
     348 
     349class SubscriptionException(Exception): 
     350        pass 
    324351 
    325352class Client: 
     
    332359                self.authToken = authToken 
    333360                self.lastError = lastError 
     361                self.cSubscriptions = {} 
     362 
     363        #override this if you want different subscriptions to have different classes 
     364        def generateSubscription(self, channel): 
     365                return  ClientSubscription(channel) 
     366 
     367        def subscribe(self, channel): 
     368                if channel in self.cSubscriptions: 
     369                        raise SubscriptionException("Already subscribed") 
     370                else: 
     371                        cs = self.generateSubscription(channel) 
     372                        cs.sendMessageCallback = self.sendMessage 
     373                        print "TRYING TO SUBSCRIBE TO " + channel 
     374                        if cs.subscribe(channel): 
     375                                self.cSubscriptions[channel] = cs 
     376                                return True 
     377                        else: 
     378                                del(cs) 
     379                                return False 
     380         
     381        #when we get disconnected or unsubscribed 
     382        def unSubscribe(self, channel): 
     383                if channel in self.cSubscriptions: 
     384                        cs = self.clientSubscription(channel, self.sendMessage) 
     385                        cs.unsubscribe(channel) 
     386                        del(self.cSubscriptions[channel]) 
     387                else: 
     388                        raise SubscriptionException("Not subscribed yet and trying to unsubscribe") 
    334389 
    335390        def setConnection(self, conn): 
     391                print "Setting Connection &&&&&&&&&&&&&& %s" % conn 
    336392                self.connection = conn 
    337393 
    338394        def messageReceived(self, message, channel): 
    339                 self.sendMessage(message,channel) 
    340                 pass 
     395                if channel in self.cSubscriptions: 
     396                        self.cSubscriptions[channel].messageReceived(message) 
     397                else: 
     398                        raise SubscriptionException("Not subscribed yet and trying to send message") 
    341399 
    342400        def sendMessage(self, message, channel): 
     
    432490 
    433491                ctr = 0 
    434                 if verbose: log.msg("messages:", type(messages), ":",  simplejson.dumps(messages)) 
     492                if verbose: log.msg("messages:", type(messages), ":",  simplejson.dumps(messages), " :", messages) 
    435493                aStream = None 
    436494                print( messages) 
     
    442500                        client = None 
    443501                        localResp = [] 
    444                         if "clientId" in m and m["clientId"] in self.clients: 
     502                        if "clientId" in m and m["clientId"] in self.clients\ 
     503                                        and self.clients[m["clientId"]].connection is not None: 
    445504                                client = self.clients[m["clientId"]] 
    446505                        else: 
     
    465524 
    466525                        chan = m["channel"] 
     526                        print "CHAN: !!!!!!!!!!!!!!!!! %s " % chan 
    467527                        if chan == "/meta/handshake" and ctr == 0: 
    468528                                # looks like we'll need to create a Connection 
     
    620680                # get the client and the channel here 
    621681                # self._subscribe() 
     682                print "self.subscribe ^^^^^^^^^^^^^^^^" 
    622683                if not self._checkClient(request, message): 
    623684                        # FIXME: we should probably send advice here instead of just raw failure 
     
    646707                # FIXME: should we be calling client.deliver and having *that* dispatch 
    647708                # down to the correct connection object? 
     709                print "Sending Message: with %s %s"  % (self.clients, client) 
    648710                client.connection.deliver(resp) 
    649711                # return { "successful": True } 
     
    679741        def _globbing_unsubscribe(self, client, chan): 
    680742                "remove a subscription" 
     743                client.unsubscribe(chan) 
    681744 
    682745                cparts = chan.split("/")[1:] 
     
    700763                if verbose: log.msg(cparts) 
    701764                root = self.subscriptions 
    702                 for part in cparts: # create parts of the topic tree that don't yet exist 
    703                         if not part in root: 
    704                                 if verbose: 
    705                                         log.msg("creating part: ", part) 
    706                                 root[part] = { "__cometd_subscribers": None } 
    707                                 # root[part] = weakref.WeakValueDictionary() 
    708                                 root[part]["__cometd_subscribers"] = weakref.WeakValueDictionary() 
    709  
    710                         root = root[part]  
    711  
    712                 root["__cometd_subscribers"][client.id] = client 
     765                # make sure the subscription was successful 
     766                if client.subscribe( chan ): 
     767                        for part in cparts: # create parts of the topic tree that don't yet exist 
     768                                if not part in root: 
     769                                        if verbose: 
     770                                                log.msg("creating part: ", part) 
     771                                        root[part] = { "__cometd_subscribers": None } 
     772                                        # root[part] = weakref.WeakValueDictionary() 
     773                                        root[part]["__cometd_subscribers"] = weakref.WeakValueDictionary() 
     774 
     775                                root = root[part]  
     776 
     777                        root["__cometd_subscribers"][client.id] = client 
    713778 
    714779        def _globbing_route(self, request, message): 
     
    758823                                subs[client].connection.deliver(message) 
    759824                return { "successful": True } 
    760  
    761825# vim:ts=4:noet: 
  • tabled.tac

    r40 r42  
    8585clientChannels = [ r"\/poker" ] 
    8686 
    87 class WebClient(cometd.Client,BasePokerTableHandler): 
     87class PokerSubscription(cometd.ClientSubscription, BasePokerTableHandler): 
     88    def __init__(self, channel): 
     89        BasePokerTableHandler.__init__(self) 
     90        cometd.ClientSubscription.__init__(self, channel) 
     91 
     92    def messageReceived(self,message): 
     93        self.dataReceived(message) 
     94 
     95    def sendMessageViaTransport(self,data): 
     96        self.sendMessage(data) 
     97 
     98class WebClient(cometd.Client): 
    8899     
    89     def __init__(self, id=None, authSuccessful=False, authToken=None, lastError=""): 
    90         cometd.Client.__init__(self,id,authSuccessful,authToken,lastError) 
    91         print "INITIALIZING WEB CLIENT ((((" 
    92         BasePokerTableHandler.__init__(self) 
    93  
    94     def messageReceived(self,message,channel): 
    95         self.dataReceived(message) 
    96  
    97     def sendMessageViaTransport(self,data): 
    98         cometd.Client.sendMessage(self,data,"/poker") 
     100    def generateSubscription(self, channel): 
     101        return PokerSubscription( channel ) 
     102 
     103    def subscribe(self, channel): 
     104        #we only want poker channels 
     105        print "SUB ---------------- CHAN " + channel 
     106        if channel == "/poker": 
     107            return cometd.Client.subscribe(self,channel) 
     108        else: return False 
    99109 
    100110 
     
    217227print "Listening on localhost:%d(tcp) %d(http)" % (port, port2) 
    218228 
    219  
     229# vim:filetype=python 
  • web/js/jquery.comet.js

    r37 r42  
    1818                this.connectionType = (this._bXD) ? 'callback-polling' : 'long-polling'; 
    1919 
    20                 this.startup = function(oReturn) 
     20                this.startup = function(oReturn, fCallback) 
    2121                { 
    2222                        if(this._comet._bConnected) return; 
    23                         this.tunnelInit(); 
    24                 }; 
    25  
    26                 this.tunnelInit = function() 
     23                        this.tunnelInit(fCallback); 
     24                }; 
     25 
     26                this.tunnelInit = function(fCallback) 
    2727                { 
    2828                        var msgConnect =  
     
    3434                        }; 
    3535 
    36                         this.openTunnel(msgConnect); 
    37                 }; 
    38  
    39                 this.openTunnel = function(oMsg) 
     36                        this.openTunnel(msgConnect,fCallback); 
     37                }; 
     38 
     39                this.openTunnel = function(oMsg, fCallback) 
    4040                { 
    4141                        $.comet._bPolling = true; 
    42  
    43                         this._send($.comet._sUrl, oMsg, function(sReturn) 
    44                         { 
     42                        var fc = fCallback; 
     43                        this._send($.comet._sUrl, [oMsg], function(sReturn) 
     44                        { 
     45                                if( fc ) { fc(); } 
    4546                                var oReturn = (typeof sReturn != "object") ? (eval('(' + sReturn + ')')) : sReturn; 
    4647                                $.comet._bPolling = false; 
     
    134135                                        type: 'post', 
    135136                                        beforeSend: function(oXhr) { oXhr.setRequestHeader('Connection', 'Keep-Alive'); }, 
    136                                         data: { message: JSON.stringify([oMsg]) }, 
     137                                        data: { message: JSON.stringify(oMsg) }, 
    137138                                        success: fCallback 
    138139                                }); 
     
    146147                                        data:  
    147148                                        {  
    148                                                 message: JSON.stringify([$.extend(oMsg,{connectionType: 'callback-polling' })])  
     149                                                message: JSON.stringify($.extend(oMsg,{connectionType: 'callback-polling' }))  
    149150                                        }, 
    150151                                        success: fCallback 
     
    178179                this._bTrigger = true; // this sends $.event.trigger(channel, data) 
    179180 
    180                 this.init = function(sUrl) 
     181                this.init = function(sUrl,fCallback) 
    181182                { 
    182183                        this._sUrl = (sUrl) ? sUrl : '/cometd'; 
     
    190191                        var oMsg = $.extend(msgHandshake, {id: this._nNextId++}); 
    191192 
    192                         this._oTransport._send(this._sUrl, oMsg, $.comet._finishInit); 
    193                 }; 
    194  
    195                 this._finishInit = function(sReturn) 
     193                        this._oTransport._send(this._sUrl, [oMsg], function(sReturn){ 
     194                                        $.comet._finishInit(sReturn, fCallback)}); 
     195                }; 
     196 
     197                this._finishInit = function(sReturn, fCallback) 
    196198                { 
    197199                        var oReturn = (typeof sReturn != "object") ? (eval('(' + sReturn + ')')[0]) : sReturn[0]; 
     
    211213                                $.comet._oTransport.version = $.comet.version; 
    212214                                $.comet.clientId = oReturn.clientId; 
    213                                 $.comet._oTransport.startup(oReturn); 
     215                                $.comet._oTransport.startup(oReturn,fCallback); 
    214216                                $.comet.endBatch(); 
    215217                        } 
    216                 }; 
    217  
    218                 this._sendMessage = function(oMsg) 
     218 
     219                }; 
     220                //batch mode defaults to null 
     221                this._sendMessage = function(oMsg, batchMode) 
    219222                { 
    220223                        if($.comet._nBatch <= 0) 
     
    231234                                        oMsg.id = $.comet._nNextId++; 
    232235                                } 
    233  
    234                                 $.comet._oTransport._send($.comet._sUrl, oMsg); 
     236                                if( batchMode ) 
     237                                        $.comet._oTransport._send($.comet._sUrl, oMsg); 
     238                                else 
     239                                        $.comet._oTransport._send($.comet._sUrl, [oMsg]); 
    235240                        } 
    236241                        else 
     
    244249                this.endBatch = function() { 
    245250                        if(--this._nBatch == 0) 
    246                         { 
    247                                 this._sendMessage(this._aMessageQueue); 
     251                        {        
     252                                if( this._aMessageQueue.length > 0 ) 
     253                                        this._sendMessage(this._aMessageQueue, true); 
    248254 
    249255                                this._aMessageQueue = []; 
  • web/js/poker-client.js

    r37 r42  
    6161        } 
    6262        //Do status generating code here 
    63         var player_names = [] 
    64  
    65  
    66                 if( stat.table_info ) { 
    67                         ti = stat.table_info; 
    68                         player_names = ti.player_names; 
    69                         for( i in player_names ) { 
    70                                 pn = player_names[i] 
    71                                         if( players[pn] == null ) { //if it's null let's create a new player object 
    72                                                 newPlayer = baseplayer.clone(true); 
    73  
    74                                                 players[pn] = new Player(newPlayer); 
    75                                                 players[pn].id.text(pn); 
    76                                                 if( pn == stat.you.id ) { 
    77                                                         you = players[stat.you.id] 
    78                                                                 $("#you").append(you.pl)  
    79                                                 } else { 
    80                                                         player_status.append( newPlayer ); 
    81                                                 } 
     63        var player_names = []; 
     64 
     65 
     66        if( stat.table_info ) { 
     67                ti = stat.table_info; 
     68                player_names = ti.player_names; 
     69                for( i in player_names ) { 
     70                        pn = player_names[i] 
     71                                if( players[pn] == null ) { //if it's null let's create a new player object 
     72                                        newPlayer = baseplayer.clone(true); 
     73 
     74                                        players[pn] = new Player(newPlayer); 
     75                                        players[pn].id.text(pn); 
     76                                        if( pn == stat.you.id ) { 
     77                                                you = players[stat.you.id]; 
     78                                                $("#you").append(you.pl); 
     79                                        } else { 
     80                                                player_status.append( newPlayer ); 
    8281                                        } 
    83                                 players[pn].updatePublic( ti.players[pn] ); 
    84                         } 
    85                 } 
     82                                } 
     83                        players[pn].updatePublic( ti.players[pn] ); 
     84                } 
     85        } 
    8686        if( stat.game_state ) { 
    8787                gs = stat.game_state; 
     
    174174        //$("form#connect_form").hide(); 
    175175                // 
    176                 $.comet.init("/cometd"); 
    177                 $.comet.subscribe("/poker", processStatus); 
     176                $.comet.init("/cometd", function() { 
     177                        $.comet.subscribe("/poker", processStatus);}) 
    178178 
    179179                sel = $('selector'); 
     
    200200        }); 
    201201 
     202