<s> from cStringIO import StringIO <EOL> from binascii import b2a_hex <EOL> from urllib import quote <EOL> import Connecter <EOL> try : <EOL> True <EOL> except : <EOL> True = <NUM_LIT:1> <EOL> False = <NUM_LIT:0> <EOL> DEBUG = False <EOL> protocol_name = '<STR_LIT>' <EOL> option_pattern = chr ( <NUM_LIT:0> ) * <NUM_LIT:8> <EOL> def toint ( s ) : <EOL> return long ( b2a_hex ( s ) , <NUM_LIT:16> ) <EOL> def tohex ( s ) : <EOL> return b2a_hex ( s ) . upper ( ) <EOL> def make_readable ( s ) : <EOL> if not s : <EOL> return '<STR_LIT>' <EOL> if quote ( s ) . find ( '<STR_LIT:%>' ) >= <NUM_LIT:0> : <EOL> return tohex ( s ) <EOL> return '<STR_LIT:">' + s + '<STR_LIT:">' <EOL> streamno = <NUM_LIT:0> <EOL> class StreamCheck : <EOL> def __init__ ( self ) : <EOL> global streamno <EOL> self . no = streamno <EOL> streamno += <NUM_LIT:1> <EOL> self . buffer = StringIO ( ) <EOL> self . next_len , self . next_func = <NUM_LIT:1> , self . read_header_len <EOL> def read_header_len ( self , s ) : <EOL> if ord ( s ) != len ( protocol_name ) : <EOL> print self . no , '<STR_LIT>' <EOL> return len ( protocol_name ) , self . read_header <EOL> def read_header ( self , s ) : <EOL> if s != protocol_name : <EOL> print self . no , '<STR_LIT>' <EOL> return <NUM_LIT:8> , self . read_reserved <EOL> def read_reserved ( self , s ) : <EOL> return <NUM_LIT:20> , self . read_download_id <EOL> def read_download_id ( self , s ) : <EOL> if DEBUG : <EOL> print self . no , '<STR_LIT>' + tohex ( s ) <EOL> return <NUM_LIT:20> , self . read_peer_id <EOL> def read_peer_id ( self , s ) : <EOL> if DEBUG : <EOL> print self . no , '<STR_LIT>' + make_readable ( s ) <EOL> return <NUM_LIT:4> , self . read_len <EOL> def read_len ( self , s ) : <EOL> l = toint ( s ) <EOL> if l > <NUM_LIT:2> ** <NUM_LIT> : <EOL> print self . no , '<STR_LIT>' + str ( l ) + '<STR_LIT>' + s + '<STR_LIT:)>' <EOL> return l , self . read_message <EOL> def read_message ( self , s ) : <EOL> if not s : <EOL> return <NUM_LIT:4> , self . read_len <EOL> m = s [ <NUM_LIT:0> ] <EOL> if ord ( m ) > <NUM_LIT:8> : <EOL> print self . no , '<STR_LIT>' + str ( ord ( m ) ) <EOL> if m == Connecter . REQUEST : <EOL> if len ( s ) != <NUM_LIT> : <EOL> print self . no , '<STR_LIT>' + str ( len ( s ) ) <EOL> return <NUM_LIT:4> , self . read_len <EOL> index = toint ( s [ <NUM_LIT:1> : <NUM_LIT:5> ] ) <EOL> begin = toint ( s [ <NUM_LIT:5> : <NUM_LIT:9> ] ) <EOL> length = toint ( s [ <NUM_LIT:9> : ] ) <EOL> print self . no , '<STR_LIT>' + str ( index ) + '<STR_LIT>' + str ( begin ) + '<STR_LIT:->' + str ( begin ) + '<STR_LIT:+>' + str ( length ) <EOL> elif m == Connecter . CANCEL : <EOL> if len ( s ) != <NUM_LIT> : <EOL> print self . no , '<STR_LIT>' + str ( len ( s ) ) <EOL> return <NUM_LIT:4> , self . read_len <EOL> index = toint ( s [ <NUM_LIT:1> : <NUM_LIT:5> ] ) <EOL> begin = toint ( s [ <NUM_LIT:5> : <NUM_LIT:9> ] ) <EOL> length = toint ( s [ <NUM_LIT:9> : ] ) <EOL> print self . no , '<STR_LIT>' + str ( index ) + '<STR_LIT>' + str ( begin ) + '<STR_LIT:->' + str ( begin ) + '<STR_LIT:+>' + str ( length ) <EOL> elif m == Connecter . PIECE : <EOL> index = toint ( s [ <NUM_LIT:1> : <NUM_LIT:5> ] ) <EOL> begin = toint ( s [ <NUM_LIT:5> : <NUM_LIT:9> ] ) <EOL> length = len ( s ) - <NUM_LIT:9> <EOL> print self . no , '<STR_LIT>' + str ( index ) + '<STR_LIT>' + str ( begin ) + '<STR_LIT:->' + str ( begin ) + '<STR_LIT:+>' + str ( length ) <EOL> else : <EOL> print self . no , '<STR_LIT>' + str ( ord ( m ) ) + '<STR_LIT>' + str ( len ( s ) ) + '<STR_LIT:)>' <EOL> return <NUM_LIT:4> , self . read_len <EOL> def write ( self , s ) : <EOL> while <NUM_LIT:1> : <EOL> i = self . next_len - self . buffer . tell ( ) <EOL> if i > len ( s ) : <EOL> self . buffer . write ( s ) <EOL> return <EOL> self . buffer . write ( s [ : i ] ) <EOL> s = s [ i : ] <EOL> m = self . buffer . getvalue ( ) <EOL> self . buffer . reset ( ) <EOL> self . buffer . truncate ( ) <EOL> x = self . next_func ( m ) <EOL> self . next_len , self . next_func = x </s>
