<s> import os <EOL> import sys <EOL> import struct <EOL> import getpass <EOL> from twisted . protocols . amp import AMP <EOL> from vertex import q2q , sigma <EOL> from twisted . python . usage import Options <EOL> from twisted . python import log <EOL> from twisted . python . failure import Failure <EOL> from twisted . internet import reactor <EOL> from twisted . internet import protocol <EOL> from twisted . internet . task import LoopingCall <EOL> from twisted . internet import error <EOL> from vertex . q2qadmin import AddUser <EOL> class Q2QAuthorize ( Options ) : <EOL> def parseArgs ( self , who , password = None ) : <EOL> self . who = who <EOL> self . password = password <EOL> def reportNoCertificate ( self , error ) : <EOL> print "<STR_LIT>" , error . getErrorMessage ( ) , "<STR_LIT>" <EOL> log . err ( error ) <EOL> return None <EOL> def postOptions ( self ) : <EOL> def go ( ) : <EOL> self . parent . getService ( ) . authorize ( <EOL> q2q . Q2QAddress . fromString ( self . who ) , <EOL> self . password ) . addErrback ( self . reportNoCertificate ) . addCallback ( lambda x : reactor . stop ( ) ) <EOL> if self . password is None : <EOL> self . password = getpass . getpass ( ) <EOL> reactor . callWhenRunning ( go ) <EOL> self . parent . start ( ) <EOL> class BandwidthEstimator : <EOL> bufsize = <NUM_LIT:20> <EOL> totalBytes = <NUM_LIT:0> <EOL> def __init__ ( self , message , length ) : <EOL> self . length = length <EOL> self . message = message <EOL> self . estim = [ ] <EOL> self . bytes = <NUM_LIT:0> <EOL> self . call = LoopingCall ( self . estimateBandwidth ) <EOL> self . call . start ( <NUM_LIT:1> ) <EOL> def estimateBandwidth ( self ) : <EOL> bytes = self . bytes <EOL> self . totalBytes += bytes <EOL> self . estim . append ( bytes ) <EOL> self . message ( "<STR_LIT>" <EOL> % ( ( sum ( self . estim ) / len ( self . estim ) ) / <NUM_LIT> , <EOL> ( float ( self . totalBytes ) / self . length ) * <NUM_LIT:100> ) ) <EOL> if len ( self . estim ) > self . bufsize : <EOL> self . estim . pop ( <NUM_LIT:0> ) <EOL> self . bytes = <NUM_LIT:0> <EOL> def stop ( self ) : <EOL> self . call . stop ( ) <EOL> self . estimateBandwidth ( ) <EOL> self . message ( "<STR_LIT>" % ( <EOL> self . totalBytes , ( float ( self . totalBytes ) / self . length ) * <NUM_LIT:100> ) ) <EOL> class FileReceiver ( protocol . Protocol ) : <EOL> gotLength = False <EOL> estimator = None <EOL> def connectionMade ( self ) : <EOL> self . f = open ( self . factory . program . filename , '<STR_LIT:wb>' ) <EOL> self . factory . program . parent . info ( "<STR_LIT>" ) <EOL> def dataReceived ( self , data ) : <EOL> if not self . gotLength : <EOL> self . length , = struct . unpack ( "<STR_LIT>" , data [ : <NUM_LIT:8> ] ) <EOL> data = data [ <NUM_LIT:8> : ] <EOL> self . estimator = BandwidthEstimator ( self . factory . program . parent . info , <EOL> self . length ) <EOL> self . gotLength = True <EOL> self . estimator . bytes += len ( data ) <EOL> self . f . write ( data ) <EOL> def connectionLost ( self , reason ) : <EOL> self . f . close ( ) <EOL> if self . estimator : <EOL> self . estimator . stop ( ) <EOL> reactor . stop ( ) <EOL> from twisted . protocols . basic import FileSender as fsdr <EOL> class FileSender ( protocol . Protocol ) : <EOL> def connectionMade ( self ) : <EOL> self . file = self . factory . openFile ( ) <EOL> self . file . seek ( <NUM_LIT:0> , <NUM_LIT:2> ) <EOL> self . length = self . file . tell ( ) <EOL> self . file . seek ( <NUM_LIT:0> ) <EOL> self . estimator = BandwidthEstimator ( self . factory . program . parent . info , <EOL> self . length ) <EOL> self . transport . write ( struct . pack ( "<STR_LIT>" , self . length ) ) <EOL> fsdr ( ) . beginFileTransfer ( <EOL> self . file , self ) . addCallback ( <EOL> lambda x : self . done ( ) ) <EOL> def done ( self ) : <EOL> self . factory . program . parent . info ( "<STR_LIT>" % ( <EOL> self . file . tell ( ) , ) ) <EOL> self . transport . loseConnection ( ) <EOL> def dataReceived ( self , data ) : <EOL> print "<STR_LIT>" , repr ( data ) <EOL> def registerProducer ( self , producer , streaming ) : <EOL> self . transport . registerProducer ( producer , streaming ) <EOL> def unregisterProducer ( self ) : <EOL> self . transport . unregisterProducer ( ) <EOL> def write ( self , data ) : <EOL> self . estimator . bytes += len ( data ) <EOL> self . transport . write ( data ) <EOL> def connectionLost ( self , reason ) : <EOL> reactor . stop ( ) <EOL> class FileSenderFactory ( protocol . ClientFactory ) : <EOL> protocol = FileSender <EOL> def __init__ ( self , sendprogram ) : <EOL> self . program = sendprogram <EOL> def openFile ( self ) : <EOL> return file ( self . program . filename , '<STR_LIT:r>' ) <EOL> def clientConnectionFailed ( self , connector , reason ) : <EOL> self . program . parent . info ( <EOL> "<STR_LIT>" % ( reason . getErrorMessage ( ) , ) ) <EOL> reactor . stop ( ) <EOL> def clientConnectionLost ( self , connector , reason ) : <EOL> reason . trap ( error . ConnectionDone ) <EOL> class FileReceiverFactory ( protocol . Factory ) : <EOL> def __init__ ( self , program ) : <EOL> self . program = program <EOL> protocol = FileReceiver <EOL> class ClientCertificateStore ( q2q . DirectoryCertificateStore ) : <EOL> def __init__ ( self , filepath ) : <EOL> q2q . DirectoryCertificateStore . __init__ ( self , os . path . expanduser ( filepath ) ) <EOL> class ClientQ2QService ( q2q . Q2QService ) : <EOL> def __init__ ( self , certspath , * a , ** kw ) : <EOL> q2q . Q2QService . __init__ ( self , <EOL> certificateStorage = ClientCertificateStore ( certspath ) , <EOL> q2qPortnum = <NUM_LIT:0> , <EOL> * a , ** kw ) <EOL> def getDefaultFrom ( self , default = None ) : <EOL> i = self . certificateStorage . localStore . iterkeys ( ) <EOL> try : <EOL> return i . next ( ) <EOL> except StopIteration : <EOL> return default <EOL> class TunnelProtocol ( protocol . Protocol ) : <EOL> def __init__ ( self , tunnel ) : <EOL> self . tunnel = tunnel <EOL> self . buffer = [ ] <EOL> def connectionMade ( self ) : <EOL> if self . tunnel is not None : <EOL> self . tunnel . setTunnel ( self ) <EOL> def dataReceived ( self , data ) : <EOL> if self . tunnel is not None : <EOL> self . tunnel . transport . write ( data ) <EOL> else : <EOL> self . buffer . append ( data ) <EOL> def setTunnel ( self , tunnel ) : <EOL> if self . tunnel is None : <EOL> self . tunnel = tunnel <EOL> self . dataReceived ( '<STR_LIT>' . join ( self . buffer ) ) <EOL> del self . buffer <EOL> self . tunnel . setTunnel ( self ) <EOL> class TunnelFactory ( protocol . ClientFactory ) : <EOL> def __init__ ( self , tunnel ) : <EOL> self . tunnel = tunnel <EOL> def buildProtocol ( self , addr ) : <EOL> return TunnelProtocol ( self . tunnel ) <EOL> def clientConnectionFailed ( self , connector , reason ) : <EOL> self . tunnel . transport . loseConnection ( ) <EOL> reactor . stop ( ) <EOL> clientConnectionLost = clientConnectionFailed <EOL> class Q2QTunnel ( Options ) : <EOL> optParameters = [ <EOL> [ '<STR_LIT:port>' , '<STR_LIT:p>' , '<STR_LIT>' , '<STR_LIT>' ] , <EOL> [ '<STR_LIT>' , '<STR_LIT:d>' , None , '<STR_LIT>' ] , <EOL> [ '<STR_LIT>' , '<STR_LIT:r>' , None , '<STR_LIT>' ] ] <EOL> def postOptions ( self ) : <EOL> self . toAddr = q2q . Q2QAddress . fromString ( self [ '<STR_LIT>' ] ) <EOL> reactor . listenTCP ( int ( self [ '<STR_LIT:port>' ] ) , self , interface = '<STR_LIT:127.0.0.1>' ) <EOL> self . parent . start ( ) <EOL> def doStart ( self ) : <EOL> pass <EOL> def doStop ( self ) : <EOL> pass <EOL> def buildProtocol ( self , addr ) : <EOL> p = TunnelProtocol ( None ) <EOL> svc = self . parent . getService ( ) <EOL> svc . connectQ2Q ( self . parent . getFrom ( ) , self . toAddr , <EOL> self [ '<STR_LIT>' ] , TunnelFactory ( p ) ) <EOL> return p <EOL> class Q2QReceive ( Options ) : <EOL> optParameters = [ [ "<STR_LIT:port>" , "<STR_LIT:p>" , "<STR_LIT>" , "<STR_LIT>" ] ] <EOL> def parseArgs ( self , filename ) : <EOL> self . filename = filename <EOL> def postOptions ( self ) : <EOL> serv = self . parent . getService ( ) <EOL> def pr ( x ) : <EOL> return x <EOL> def stopit ( err ) : <EOL> print "<STR_LIT>" , err . getErrorMessage ( ) <EOL> log . err ( err ) <EOL> reactor . stop ( ) <EOL> serv . listenQ2Q ( self . parent . getFrom ( ) , <EOL> { '<STR_LIT>' : FileReceiverFactory ( self ) } , <EOL> "<STR_LIT>" ) . addCallback ( pr ) . addErrback ( stopit ) <EOL> self . parent . start ( ) <EOL> class Q2QSend ( Options ) : <EOL> def parseArgs ( self , to , filename ) : <EOL> self . to = to <EOL> self . filename = filename <EOL> def postOptions ( self ) : <EOL> fs = q2q . Q2QAddress . fromString <EOL> toAddress = fs ( self . to ) <EOL> fromAddress = self . parent . getFrom ( ) <EOL> svc = self . parent . getService ( ) <EOL> svc . connectQ2Q ( fromAddress , toAddress , '<STR_LIT>' , <EOL> FileSenderFactory ( self ) ) <EOL> self . parent . start ( ) <EOL> class TextNexusUI ( sigma . BaseNexusUI ) : <EOL> def __init__ ( self ) : <EOL> sigma . BaseNexusUI . __init__ ( self ) <EOL> self . call = LoopingCall ( self . report ) <EOL> self . call . start ( <NUM_LIT:5> ) <EOL> def report ( self ) : <EOL> print '<STR_LIT>' , len ( self . transloads ) <EOL> for transloadui in self . transloads : <EOL> print '<STR_LIT>' , transloadui . name , '<STR_LIT>' <EOL> print transloadui . bits . percent ( ) <EOL> for peer , mask in transloadui . masks . items ( ) : <EOL> print peer , mask . percent ( ) <EOL> print '<STR_LIT>' <EOL> class Q2QSigma ( Options ) : <EOL> def __init__ ( self , * a , ** k ) : <EOL> Options . __init__ ( self , * a , ** k ) <EOL> self . pushers = [ ] <EOL> def opt_push ( self , filename ) : <EOL> self . pushers . append ( [ file ( filename ) , filename , [ ] ] ) <EOL> def opt_to ( self , q2qid ) : <EOL> fs = q2q . Q2QAddress . fromString <EOL> addr = fs ( q2qid ) <EOL> self . pushers [ - <NUM_LIT:1> ] [ - <NUM_LIT:1> ] . append ( addr ) <EOL> def postOptions ( self ) : <EOL> nex = sigma . Nexus ( self . parent . getService ( ) , <EOL> self . parent . getFrom ( ) , <EOL> TextNexusUI ( ) ) <EOL> for sharefile , sharename , sharepeers in self . pushers : <EOL> nex . push ( sharefile , sharename , sharepeers ) <EOL> self . parent . start ( ) <EOL> def enregister ( svc , newAddress , password ) : <EOL> """<STR_LIT>""" <EOL> return svc . connectQ2Q ( q2q . Q2QAddress ( "<STR_LIT>" , "<STR_LIT>" ) , <EOL> q2q . Q2QAddress ( newAddress . domain , "<STR_LIT>" ) , <EOL> '<STR_LIT>' , <EOL> protocol . ClientFactory . forProtocol ( AMP ) <EOL> ) . addCallback ( <EOL> AMP . callRemote , <EOL> AddUser , <EOL> name = newAddress . resource , <EOL> password = password <EOL> ) . addErrback ( <EOL> Failure . trap , <EOL> error . ConnectionDone <EOL> ) <EOL> class Q2QRegister ( Options ) : <EOL> synopsis = "<STR_LIT>" <EOL> def parseArgs ( self , newaddress , password ) : <EOL> self . newaddress = newaddress <EOL> self . password = password <EOL> def postOptions ( self ) : <EOL> fs = q2q . Q2QAddress . fromString <EOL> newAddress = fs ( self . newaddress ) <EOL> svc = self . parent . getService ( ) <EOL> def showit ( x ) : <EOL> print "<STR_LIT>" % ( x . value . __class__ , x . getErrorMessage ( ) ) <EOL> enregister ( svc , newAddress , self . password ) . addErrback ( <EOL> showit ) . addBoth ( lambda nothing : reactor . stop ( ) ) <EOL> self . parent . start ( ) <EOL> class Q2QClientProgram ( Options ) : <EOL> subCommands = [ <EOL> [ '<STR_LIT>' , '<STR_LIT:a>' , Q2QAuthorize , '<STR_LIT>' ] , <EOL> [ '<STR_LIT>' , '<STR_LIT:r>' , Q2QRegister , '<STR_LIT>' ] , <EOL> [ '<STR_LIT>' , '<STR_LIT:t>' , Q2QTunnel , '<STR_LIT>' ] , <EOL> [ '<STR_LIT>' , '<STR_LIT:l>' , Q2QReceive , '<STR_LIT>' ] , <EOL> [ '<STR_LIT>' , '<STR_LIT:s>' , Q2QSend , '<STR_LIT>' ] , <EOL> [ '<STR_LIT>' , '<STR_LIT:g>' , Q2QSigma , '<STR_LIT>' ] <EOL> ] <EOL> optParameters = [ <EOL> [ '<STR_LIT>' , '<STR_LIT:f>' , None , "<STR_LIT>" ] , <EOL> [ '<STR_LIT>' , '<STR_LIT:p>' , None , '<STR_LIT>' ] , <EOL> [ '<STR_LIT>' , '<STR_LIT:u>' , <NUM_LIT:0> , '<STR_LIT>' ] , <EOL> [ '<STR_LIT>' , '<STR_LIT:c>' , "<STR_LIT>" , <EOL> "<STR_LIT>" ] , <EOL> [ '<STR_LIT>' , '<STR_LIT:l>' , "<STR_LIT>" , <EOL> "<STR_LIT>" ] <EOL> ] <EOL> optFlags = [ ] <EOL> service = None <EOL> def postOptions ( self ) : <EOL> if not self . subCommand : <EOL> self . opt_help ( ) <EOL> def info ( self , message ) : <EOL> sys . stderr . write ( "<STR_LIT>" % ( message , ) ) <EOL> def getService ( self ) : <EOL> if self . service is None : <EOL> u = self [ '<STR_LIT>' ] <EOL> if u is not None : <EOL> u = int ( u ) <EOL> t = self [ '<STR_LIT>' ] <EOL> if t is not None : <EOL> t = int ( t ) <EOL> self . service = ClientQ2QService ( self [ '<STR_LIT>' ] , <EOL> inboundTCPPortnum = t ) <EOL> return self . service <EOL> def getDefaultPath ( self ) : <EOL> return os . path . expanduser ( os . path . join ( self [ '<STR_LIT>' ] , '<STR_LIT>' ) ) <EOL> def getFrom ( self ) : <EOL> fr = self [ '<STR_LIT>' ] <EOL> if not fr : <EOL> defpath = self . getDefaultPath ( ) <EOL> if os . path . exists ( defpath ) : <EOL> fr = file ( defpath ) . read ( ) <EOL> else : <EOL> fr = self . getService ( ) . getDefaultFrom ( ) <EOL> if fr is None : <EOL> self . info ( "<STR_LIT>" ) <EOL> self . info ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> sys . exit ( <NUM_LIT> ) <EOL> self . info ( "<STR_LIT>" + fr ) <EOL> f = file ( defpath , '<STR_LIT:wb>' ) <EOL> f . write ( fr ) <EOL> f . close ( ) <EOL> return q2q . Q2QAddress . fromString ( fr ) <EOL> def start ( self , portno = None ) : <EOL> import sys <EOL> lfname = self [ '<STR_LIT>' ] <EOL> if lfname == '<STR_LIT:->' : <EOL> lf = sys . stdout <EOL> else : <EOL> lf = file ( os . path . expanduser ( lfname ) , '<STR_LIT>' ) <EOL> log . startLogging ( lf , <EOL> setStdout = False ) <EOL> srv = self . getService ( ) <EOL> from twisted . application . app import startApplication <EOL> startApplication ( srv , False ) <EOL> reactor . run ( ) <EOL> verbosity = <NUM_LIT:0> <EOL> def verboseLogger ( self , messageDict ) : <EOL> self . info ( '<STR_LIT:U+0020>' . join ( [ str ( x ) for x in messageDict . get ( '<STR_LIT:message>' , [ ] ) ] ) ) <EOL> def opt_verbose ( self ) : <EOL> self . verbosity += <NUM_LIT:1> <EOL> log . addObserver ( log . FileLogObserver ( sys . stderr ) . emit ) <EOL> opt_v = opt_verbose </s>
