Created
May 8, 2013 16:23
-
-
Save wcang/5541629 to your computer and use it in GitHub Desktop.
Quick tryout of state design pattern for MLD router state transition
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#implementation of state design pattern for MLD router state transition | |
#(RFC2710) | |
import time | |
class NonQuerier: | |
def __init__(self, ctx): | |
print "In non-querier" | |
print "Start other querier timeout" | |
self.ctx = ctx | |
self.timeout = time.time() | |
def recv_query(self, lower): | |
if lower: | |
print "Resetting timeout" | |
def expire(self): | |
'''other querier present timer expired''' | |
self.transit() | |
def transit(self): | |
print "Transit to querier" | |
self.ctx.state = Querier(self.ctx) | |
class Querier: | |
def __init__(self, ctx): | |
print "In querier" | |
print "Send general query" | |
self.ctx = ctx | |
def recv_query(self, lower): | |
if lower: | |
self.transit() | |
def expire(self): | |
'''general query timer expire''' | |
print "Resend general query" | |
def transit(self): | |
print "Stop send general query thread" | |
print "Transit to non-querier" | |
self.ctx.state = NonQuerier(self.ctx) | |
class MLDCtx: | |
def __init__(self): | |
self.state = Querier(self) | |
if __name__ == '__main__': | |
ctx = MLDCtx() | |
ctx.state.recv_query(True) | |
ctx.state.recv_query(True) | |
ctx.state.expire() | |
ctx.state.recv_query(True) | |
ctx.state.expire() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment