<s> '''<STR_LIT>''' <EOL> import sys , os , time , string , math , threading <EOL> import heapq <EOL> from core . api import coreapi <EOL> from core . conf import ConfigurableManager , Configurable <EOL> from core . coreobj import PyCoreNode <EOL> from core . misc . utils import check_call <EOL> from core . misc . ipaddr import IPAddr <EOL> class MobilityManager ( ConfigurableManager ) : <EOL> '''<STR_LIT>''' <EOL> _name = "<STR_LIT>" <EOL> _type = coreapi . CORE_TLV_REG_WIRELESS <EOL> def __init__ ( self , session ) : <EOL> ConfigurableManager . __init__ ( self , session ) <EOL> self . verbose = self . session . getcfgitembool ( '<STR_LIT>' , False ) <EOL> self . _modelclsmap = { } <EOL> self . phys = { } <EOL> self . physnets = { } <EOL> self . session . broker . handlers += ( self . physnodehandlelink , ) <EOL> self . register ( ) <EOL> def startup ( self , nodenums = None ) : <EOL> '''<STR_LIT>''' <EOL> if nodenums is None : <EOL> nodenums = self . configs . keys ( ) <EOL> for nodenum in nodenums : <EOL> try : <EOL> n = self . session . obj ( nodenum ) <EOL> except KeyError : <EOL> self . session . warn ( "<STR_LIT>" <EOL> "<STR_LIT>" % nodenum ) <EOL> continue <EOL> if nodenum not in self . configs : <EOL> self . session . warn ( "<STR_LIT>" <EOL> "<STR_LIT>" % nodenum ) <EOL> continue <EOL> v = self . configs [ nodenum ] <EOL> for model in v : <EOL> try : <EOL> cls = self . _modelclsmap [ model [ <NUM_LIT:0> ] ] <EOL> except KeyError : <EOL> self . session . warn ( "<STR_LIT>" <EOL> "<STR_LIT>" % model [ <NUM_LIT:0> ] ) <EOL> continue <EOL> n . setmodel ( cls , model [ <NUM_LIT:1> ] ) <EOL> if self . session . master : <EOL> self . installphysnodes ( n ) <EOL> if n . mobility : <EOL> self . session . evq . add_event ( <NUM_LIT:0.0> , n . mobility . startup ) <EOL> return ( ) <EOL> def reset ( self ) : <EOL> '''<STR_LIT>''' <EOL> self . clearconfig ( nodenum = None ) <EOL> def setconfig ( self , nodenum , conftype , values ) : <EOL> '''<STR_LIT>''' <EOL> super ( MobilityManager , self ) . setconfig ( nodenum , conftype , values ) <EOL> if self . session is None : <EOL> return <EOL> if self . session . getstate ( ) == coreapi . CORE_EVENT_RUNTIME_STATE : <EOL> try : <EOL> n = self . session . obj ( nodenum ) <EOL> except KeyError : <EOL> self . session . warn ( "<STR_LIT>" <EOL> "<STR_LIT>" % nodenum ) <EOL> n . updatemodel ( conftype , values ) <EOL> def register ( self ) : <EOL> '''<STR_LIT>''' <EOL> models = [ BasicRangeModel , Ns2ScriptedMobility ] <EOL> for m in models : <EOL> self . session . addconfobj ( m . _name , m . _type , m . configure_mob ) <EOL> self . _modelclsmap [ m . _name ] = m <EOL> def handleevent ( self , msg ) : <EOL> '''<STR_LIT>''' <EOL> eventtype = msg . gettlv ( coreapi . CORE_TLV_EVENT_TYPE ) <EOL> nodenum = msg . gettlv ( coreapi . CORE_TLV_EVENT_NODE ) <EOL> name = msg . gettlv ( coreapi . CORE_TLV_EVENT_NAME ) <EOL> try : <EOL> node = self . session . obj ( nodenum ) <EOL> except KeyError : <EOL> self . session . warn ( "<STR_LIT>" "<STR_LIT>" % ( name , nodenum ) ) <EOL> return <EOL> models = name [ <NUM_LIT:9> : ] . split ( '<STR_LIT:U+002C>' ) <EOL> for m in models : <EOL> try : <EOL> cls = self . _modelclsmap [ m ] <EOL> except KeyError : <EOL> self . session . warn ( "<STR_LIT>" % m ) <EOL> continue <EOL> _name = "<STR_LIT>" <EOL> if cls . _type == coreapi . CORE_TLV_REG_WIRELESS : <EOL> model = node . mobility <EOL> elif cls . _type == coreapi . CORE_TLV_REG_MOBILITY : <EOL> model = node . mobility <EOL> else : <EOL> continue <EOL> if model is None : <EOL> self . session . warn ( "<STR_LIT>" % node . name ) <EOL> continue <EOL> if cls . _name != model . _name : <EOL> self . session . warn ( "<STR_LIT>" % ( node . name , cls . _name , model . _name ) ) <EOL> continue <EOL> if eventtype == coreapi . CORE_EVENT_STOP or eventtype == coreapi . CORE_EVENT_RESTART : <EOL> model . stop ( move_initial = True ) <EOL> if eventtype == coreapi . CORE_EVENT_START or eventtype == coreapi . CORE_EVENT_RESTART : <EOL> model . start ( ) <EOL> if eventtype == coreapi . CORE_EVENT_PAUSE : <EOL> model . pause ( ) <EOL> def sendevent ( self , model ) : <EOL> '''<STR_LIT>''' <EOL> if model . state == model . STATE_STOPPED : <EOL> eventtype = coreapi . CORE_EVENT_STOP <EOL> elif model . state == model . STATE_RUNNING : <EOL> eventtype = coreapi . CORE_EVENT_START <EOL> elif model . state == model . STATE_PAUSED : <EOL> eventtype = coreapi . CORE_EVENT_PAUSE <EOL> data = "<STR_LIT>" % int ( model . lasttime - model . timezero ) <EOL> data += "<STR_LIT>" % int ( model . endtime ) <EOL> tlvdata = "<STR_LIT>" <EOL> tlvdata += coreapi . CoreEventTlv . pack ( coreapi . CORE_TLV_EVENT_NODE , <EOL> model . objid ) <EOL> tlvdata += coreapi . CoreEventTlv . pack ( coreapi . CORE_TLV_EVENT_TYPE , <EOL> eventtype ) <EOL> tlvdata += coreapi . CoreEventTlv . pack ( coreapi . CORE_TLV_EVENT_NAME , <EOL> "<STR_LIT>" % model . _name ) <EOL> tlvdata += coreapi . CoreEventTlv . pack ( coreapi . CORE_TLV_EVENT_DATA , <EOL> data ) <EOL> tlvdata += coreapi . CoreEventTlv . pack ( coreapi . CORE_TLV_EVENT_TIME , <EOL> "<STR_LIT:%s>" % time . time ( ) ) <EOL> msg = coreapi . CoreEventMessage . pack ( <NUM_LIT:0> , tlvdata ) <EOL> try : <EOL> self . session . broadcastraw ( None , msg ) <EOL> except Exception , e : <EOL> self . warn ( "<STR_LIT>" % e ) <EOL> def updatewlans ( self , moved , moved_netifs ) : <EOL> '''<STR_LIT>''' <EOL> for nodenum in self . configs : <EOL> try : <EOL> n = self . session . obj ( nodenum ) <EOL> except KeyError : <EOL> continue <EOL> if n . model : <EOL> n . model . update ( moved , moved_netifs ) <EOL> def addphys ( self , netnum , node ) : <EOL> '''<STR_LIT>''' <EOL> nodenum = node . objid <EOL> self . phys [ nodenum ] = node <EOL> if netnum not in self . physnets : <EOL> self . physnets [ netnum ] = [ nodenum , ] <EOL> else : <EOL> self . physnets [ netnum ] . append ( nodenum ) <EOL> def physnodehandlelink ( self , msg ) : <EOL> '''<STR_LIT>''' <EOL> if msg . msgtype == coreapi . CORE_API_LINK_MSG and msg . flags & coreapi . CORE_API_ADD_FLAG : <EOL> nn = msg . nodenumbers ( ) <EOL> if nn [ <NUM_LIT:0> ] not in self . session . broker . nets : <EOL> return <EOL> if nn [ <NUM_LIT:1> ] in self . session . broker . phys : <EOL> dummy = PyCoreNode ( session = self . session , objid = nn [ <NUM_LIT:1> ] , <EOL> name = "<STR_LIT>" % nn [ <NUM_LIT:1> ] , start = False ) <EOL> self . addphys ( nn [ <NUM_LIT:0> ] , dummy ) <EOL> def physnodeupdateposition ( self , msg ) : <EOL> '''<STR_LIT>''' <EOL> nodenum = msg . nodenumbers ( ) [ <NUM_LIT:0> ] <EOL> try : <EOL> dummy = self . phys [ nodenum ] <EOL> nodexpos = msg . gettlv ( coreapi . CORE_TLV_NODE_XPOS ) <EOL> nodeypos = msg . gettlv ( coreapi . CORE_TLV_NODE_YPOS ) <EOL> dummy . setposition ( nodexpos , nodeypos , None ) <EOL> except KeyError : <EOL> pass <EOL> def installphysnodes ( self , net ) : <EOL> '''<STR_LIT>''' <EOL> try : <EOL> nodenums = self . physnets [ net . objid ] <EOL> except KeyError : <EOL> return <EOL> for nodenum in nodenums : <EOL> node = self . phys [ nodenum ] <EOL> servers = self . session . broker . getserversbynode ( nodenum ) <EOL> ( host , port , sock ) = self . session . broker . getserver ( servers [ <NUM_LIT:0> ] ) <EOL> netif = self . session . broker . gettunnel ( net . objid , IPAddr . toint ( host ) ) <EOL> node . addnetif ( netif , <NUM_LIT:0> ) <EOL> netif . node = node <EOL> ( x , y , z ) = netif . node . position . get ( ) <EOL> netif . poshook ( netif , x , y , z ) <EOL> class WirelessModel ( Configurable ) : <EOL> '''<STR_LIT>''' <EOL> _type = coreapi . CORE_TLV_REG_WIRELESS <EOL> _bitmap = None <EOL> _positioncallback = None <EOL> def __init__ ( self , session , objid , verbose = False , values = None ) : <EOL> Configurable . __init__ ( self , session , objid ) <EOL> self . verbose = verbose <EOL> def tolinkmsgs ( self , flags ) : <EOL> '''<STR_LIT>''' <EOL> return [ ] <EOL> def update ( self , moved , moved_netifs ) : <EOL> raise NotImplementedError <EOL> def updateconfig ( self , values ) : <EOL> '''<STR_LIT>''' <EOL> return False <EOL> class BasicRangeModel ( WirelessModel ) : <EOL> '''<STR_LIT>''' <EOL> _name = "<STR_LIT>" <EOL> _confmatrix = [ <EOL> ( "<STR_LIT>" , coreapi . CONF_DATA_TYPE_UINT32 , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( "<STR_LIT>" , coreapi . CONF_DATA_TYPE_UINT32 , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( "<STR_LIT>" , coreapi . CONF_DATA_TYPE_FLOAT , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( "<STR_LIT>" , coreapi . CONF_DATA_TYPE_FLOAT , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( "<STR_LIT:error>" , coreapi . CONF_DATA_TYPE_FLOAT , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ] <EOL> _confgroups = "<STR_LIT>" % len ( _confmatrix ) <EOL> def __init__ ( self , session , objid , verbose = False , values = None ) : <EOL> '''<STR_LIT>''' <EOL> super ( BasicRangeModel , self ) . __init__ ( session = session , objid = objid , <EOL> verbose = verbose ) <EOL> self . wlan = session . obj ( objid ) <EOL> self . _netifs = { } <EOL> self . _netifslock = threading . Lock ( ) <EOL> if values is None : <EOL> values = session . mobility . getconfig ( objid , self . _name , <EOL> self . getdefaultvalues ( ) ) [ <NUM_LIT:1> ] <EOL> self . range = float ( self . valueof ( "<STR_LIT>" , values ) ) <EOL> if self . verbose : <EOL> self . session . info ( "<STR_LIT>" "<STR_LIT>" % ( objid , self . range ) ) <EOL> self . valuestolinkparams ( values ) <EOL> def valuestolinkparams ( self , values ) : <EOL> self . bw = int ( self . valueof ( "<STR_LIT>" , values ) ) <EOL> if self . bw == <NUM_LIT:0.0> : <EOL> self . bw = None <EOL> self . delay = float ( self . valueof ( "<STR_LIT>" , values ) ) <EOL> if self . delay == <NUM_LIT:0.0> : <EOL> self . delay = None <EOL> self . loss = float ( self . valueof ( "<STR_LIT:error>" , values ) ) <EOL> if self . loss == <NUM_LIT:0.0> : <EOL> self . loss = None <EOL> self . jitter = float ( self . valueof ( "<STR_LIT>" , values ) ) <EOL> if self . jitter == <NUM_LIT:0.0> : <EOL> self . jitter = None <EOL> @ classmethod <EOL> def configure_mob ( cls , session , msg ) : <EOL> '''<STR_LIT>''' <EOL> return cls . configure ( session . mobility , msg ) <EOL> def setlinkparams ( self ) : <EOL> '''<STR_LIT>''' <EOL> with self . _netifslock : <EOL> for netif in self . _netifs : <EOL> self . wlan . linkconfig ( netif , bw = self . bw , delay = self . delay , <EOL> loss = self . loss , duplicate = None , <EOL> jitter = self . jitter ) <EOL> def get_position ( self , netif ) : <EOL> with self . _netifslock : <EOL> return self . _netifs [ netif ] <EOL> def set_position ( self , netif , x = None , y = None , z = None ) : <EOL> '''<STR_LIT>''' <EOL> self . _netifslock . acquire ( ) <EOL> self . _netifs [ netif ] = ( x , y , z ) <EOL> if x is None or y is None : <EOL> self . _netifslock . release ( ) <EOL> return <EOL> for netif2 in self . _netifs : <EOL> self . calclink ( netif , netif2 ) <EOL> self . _netifslock . release ( ) <EOL> _positioncallback = set_position <EOL> def update ( self , moved , moved_netifs ) : <EOL> '''<STR_LIT>''' <EOL> with self . _netifslock : <EOL> while len ( moved_netifs ) : <EOL> netif = moved_netifs . pop ( ) <EOL> ( nx , ny , nz ) = netif . node . getposition ( ) <EOL> if netif in self . _netifs : <EOL> self . _netifs [ netif ] = ( nx , ny , nz ) <EOL> for netif2 in self . _netifs : <EOL> if netif2 in moved_netifs : <EOL> continue <EOL> self . calclink ( netif , netif2 ) <EOL> def calclink ( self , netif , netif2 ) : <EOL> '''<STR_LIT>''' <EOL> if netif == netif2 : <EOL> return <EOL> try : <EOL> ( x , y , z ) = self . _netifs [ netif ] <EOL> ( x2 , y2 , z2 ) = self . _netifs [ netif2 ] <EOL> except KeyError : <EOL> return <EOL> if x2 is None or y2 is None : <EOL> return <EOL> d = self . calcdistance ( ( x , y , z ) , ( x2 , y2 , z2 ) ) <EOL> a = min ( netif , netif2 ) <EOL> b = max ( netif , netif2 ) <EOL> try : <EOL> self . wlan . _linked_lock . acquire ( ) <EOL> linked = self . wlan . linked ( a , b ) <EOL> except KeyError : <EOL> return <EOL> finally : <EOL> self . wlan . _linked_lock . release ( ) <EOL> if d > self . range : <EOL> if linked : <EOL> self . wlan . unlink ( a , b ) <EOL> self . sendlinkmsg ( a , b , unlink = True ) <EOL> else : <EOL> if not linked : <EOL> self . wlan . link ( a , b ) <EOL> self . sendlinkmsg ( a , b ) <EOL> @ staticmethod <EOL> def calcdistance ( p1 , p2 ) : <EOL> '''<STR_LIT>''' <EOL> a = p1 [ <NUM_LIT:0> ] - p2 [ <NUM_LIT:0> ] <EOL> b = p1 [ <NUM_LIT:1> ] - p2 [ <NUM_LIT:1> ] <EOL> c = <NUM_LIT:0> <EOL> if p1 [ <NUM_LIT:2> ] is not None and p2 [ <NUM_LIT:2> ] is not None : <EOL> c = p1 [ <NUM_LIT:2> ] - p2 [ <NUM_LIT:2> ] <EOL> return math . hypot ( math . hypot ( a , b ) , c ) <EOL> def updateconfig ( self , values ) : <EOL> '''<STR_LIT>''' <EOL> self . valuestolinkparams ( values ) <EOL> self . range = float ( self . valueof ( "<STR_LIT>" , values ) ) <EOL> return True <EOL> def linkmsg ( self , netif , netif2 , flags ) : <EOL> '''<STR_LIT>''' <EOL> n1 = netif . localname . split ( '<STR_LIT:.>' ) [ <NUM_LIT:0> ] <EOL> n2 = netif2 . localname . split ( '<STR_LIT:.>' ) [ <NUM_LIT:0> ] <EOL> tlvdata = coreapi . CoreLinkTlv . pack ( coreapi . CORE_TLV_LINK_N1NUMBER , <EOL> netif . node . objid ) <EOL> tlvdata += coreapi . CoreLinkTlv . pack ( coreapi . CORE_TLV_LINK_N2NUMBER , <EOL> netif2 . node . objid ) <EOL> tlvdata += coreapi . CoreLinkTlv . pack ( coreapi . CORE_TLV_LINK_NETID , <EOL> self . wlan . objid ) <EOL> tlvdata += coreapi . CoreLinkTlv . pack ( coreapi . CORE_TLV_LINK_TYPE , <EOL> coreapi . CORE_LINK_WIRELESS ) <EOL> return coreapi . CoreLinkMessage . pack ( flags , tlvdata ) <EOL> def sendlinkmsg ( self , netif , netif2 , unlink = False ) : <EOL> '''<STR_LIT>''' <EOL> if unlink : <EOL> flags = coreapi . CORE_API_DEL_FLAG <EOL> else : <EOL> flags = coreapi . CORE_API_ADD_FLAG <EOL> msg = self . linkmsg ( netif , netif2 , flags ) <EOL> self . session . broadcastraw ( src = None , data = msg ) <EOL> self . session . sdt . updatelink ( netif . node . objid , netif2 . node . objid , flags , <EOL> wireless = True ) <EOL> def tolinkmsgs ( self , flags ) : <EOL> '''<STR_LIT>''' <EOL> r = [ ] <EOL> with self . wlan . _linked_lock : <EOL> for a in self . wlan . _linked : <EOL> for b in self . wlan . _linked [ a ] : <EOL> if self . wlan . _linked [ a ] [ b ] : <EOL> r . append ( self . linkmsg ( a , b , flags ) ) <EOL> return r <EOL> class WayPointMobility ( WirelessModel ) : <EOL> '''<STR_LIT>''' <EOL> _name = "<STR_LIT>" <EOL> _type = coreapi . CORE_TLV_REG_MOBILITY <EOL> STATE_STOPPED = <NUM_LIT:0> <EOL> STATE_RUNNING = <NUM_LIT:1> <EOL> STATE_PAUSED = <NUM_LIT:2> <EOL> class WayPoint ( object ) : <EOL> def __init__ ( self , time , nodenum , coords , speed ) : <EOL> self . time = time <EOL> self . nodenum = nodenum <EOL> self . coords = coords <EOL> self . speed = speed <EOL> def __cmp__ ( self , other ) : <EOL> tmp = cmp ( self . time , other . time ) <EOL> if tmp == <NUM_LIT:0> : <EOL> tmp = cmp ( self . nodenum , other . nodenum ) <EOL> return tmp <EOL> def __init__ ( self , session , objid , verbose = False , values = None ) : <EOL> super ( WayPointMobility , self ) . __init__ ( session = session , objid = objid , <EOL> verbose = verbose , values = values ) <EOL> self . state = self . STATE_STOPPED <EOL> self . queue = [ ] <EOL> self . queue_copy = [ ] <EOL> self . points = { } <EOL> self . initial = { } <EOL> self . lasttime = None <EOL> self . endtime = None <EOL> self . wlan = session . obj ( objid ) <EOL> self . loop = False <EOL> self . refresh_ms = <NUM_LIT:50> <EOL> self . empty_queue_stop = True <EOL> def runround ( self ) : <EOL> '''<STR_LIT>''' <EOL> if self . state != self . STATE_RUNNING : <EOL> return <EOL> t = self . lasttime <EOL> self . lasttime = time . time ( ) <EOL> now = self . lasttime - self . timezero <EOL> dt = self . lasttime - t <EOL> self . updatepoints ( now ) <EOL> if not len ( self . points ) : <EOL> if len ( self . queue ) : <EOL> nexttime = self . queue [ <NUM_LIT:0> ] . time - now <EOL> if nexttime > ( <NUM_LIT> * self . refresh_ms ) : <EOL> nexttime -= ( <NUM_LIT> * self . refresh_ms ) <EOL> self . session . evq . add_event ( nexttime , self . runround ) <EOL> return <EOL> else : <EOL> if not self . empty_queue_stop : <EOL> self . session . evq . add_event ( <NUM_LIT> * self . refresh_ms , self . runround ) <EOL> return <EOL> if not self . loopwaypoints ( ) : <EOL> return self . stop ( move_initial = False ) <EOL> if not len ( self . queue ) : <EOL> return <EOL> return self . run ( ) <EOL> moved = [ ] <EOL> moved_netifs = [ ] <EOL> for netif in self . wlan . netifs ( ) : <EOL> node = netif . node <EOL> if self . movenode ( node , dt ) : <EOL> moved . append ( node ) <EOL> moved_netifs . append ( netif ) <EOL> self . session . mobility . updatewlans ( moved , moved_netifs ) <EOL> self . session . evq . add_event ( <NUM_LIT> * self . refresh_ms , self . runround ) <EOL> def run ( self ) : <EOL> self . timezero = time . time ( ) <EOL> self . lasttime = self . timezero - ( <NUM_LIT> * self . refresh_ms ) <EOL> self . movenodesinitial ( ) <EOL> self . runround ( ) <EOL> self . session . mobility . sendevent ( self ) <EOL> def movenode ( self , node , dt ) : <EOL> '''<STR_LIT>''' <EOL> if node . objid not in self . points : <EOL> return False <EOL> x1 , y1 , z1 = node . getposition ( ) <EOL> x2 , y2 , z2 = self . points [ node . objid ] . coords <EOL> speed = self . points [ node . objid ] . speed <EOL> if speed == <NUM_LIT:0> : <EOL> self . setnodeposition ( node , x2 , y2 , z2 ) <EOL> del self . points [ node . objid ] <EOL> return True <EOL> if isinstance ( speed , ( float , int ) ) : <EOL> alpha = math . atan2 ( y2 - y1 , x2 - x1 ) <EOL> sx = speed * math . cos ( alpha ) <EOL> sy = speed * math . sin ( alpha ) <EOL> else : <EOL> sx = speed [ <NUM_LIT:0> ] <EOL> sy = speed [ <NUM_LIT:1> ] <EOL> dx = sx * dt <EOL> dy = sy * dt <EOL> if abs ( dx ) > abs ( x2 - x1 ) : <EOL> dx = x2 - x1 <EOL> if abs ( dy ) > abs ( y2 - y1 ) : <EOL> dy = y2 - y1 <EOL> if dx == <NUM_LIT:0.0> and dy == <NUM_LIT:0.0> : <EOL> if self . endtime < ( self . lasttime - self . timezero ) : <EOL> self . endtime = self . lasttime - self . timezero <EOL> del self . points [ node . objid ] <EOL> return False <EOL> if ( x1 + dx ) < <NUM_LIT:0.0> : <EOL> dx = <NUM_LIT:0.0> - x1 <EOL> if ( y1 + dy ) < <NUM_LIT:0.0> : <EOL> dy = <NUM_LIT:0.0> - y1 <EOL> self . setnodeposition ( node , x1 + dx , y1 + dy , z1 ) <EOL> return True <EOL> def movenodesinitial ( self ) : <EOL> '''<STR_LIT>''' <EOL> moved = [ ] <EOL> moved_netifs = [ ] <EOL> for netif in self . wlan . netifs ( ) : <EOL> node = netif . node <EOL> if node . objid not in self . initial : <EOL> continue <EOL> ( x , y , z ) = self . initial [ node . objid ] . coords <EOL> self . setnodeposition ( node , x , y , z ) <EOL> moved . append ( node ) <EOL> moved_netifs . append ( netif ) <EOL> self . session . mobility . updatewlans ( moved , moved_netifs ) <EOL> def addwaypoint ( self , time , nodenum , x , y , z , speed ) : <EOL> '''<STR_LIT>''' <EOL> wp = self . WayPoint ( time , nodenum , coords = ( x , y , z ) , speed = speed ) <EOL> heapq . heappush ( self . queue , wp ) <EOL> def addinitial ( self , nodenum , x , y , z ) : <EOL> '''<STR_LIT>''' <EOL> wp = self . WayPoint ( <NUM_LIT:0> , nodenum , coords = ( x , y , z ) , speed = <NUM_LIT:0> ) <EOL> self . initial [ nodenum ] = wp <EOL> def updatepoints ( self , now ) : <EOL> '''<STR_LIT>''' <EOL> while len ( self . queue ) : <EOL> if self . queue [ <NUM_LIT:0> ] . time > now : <EOL> break <EOL> wp = heapq . heappop ( self . queue ) <EOL> self . points [ wp . nodenum ] = wp <EOL> def copywaypoints ( self ) : <EOL> '''<STR_LIT>''' <EOL> self . queue_copy = list ( self . queue ) <EOL> def loopwaypoints ( self ) : <EOL> '''<STR_LIT>''' <EOL> self . queue = list ( self . queue_copy ) <EOL> return self . loop <EOL> def setnodeposition ( self , node , x , y , z ) : <EOL> '''<STR_LIT>''' <EOL> node . position . set ( x , y , z ) <EOL> msg = node . tonodemsg ( flags = <NUM_LIT:0> ) <EOL> self . session . broadcastraw ( None , msg ) <EOL> self . session . sdt . updatenode ( node . objid , flags = <NUM_LIT:0> , x = x , y = y , z = z ) <EOL> def setendtime ( self ) : <EOL> '''<STR_LIT>''' <EOL> try : <EOL> self . endtime = self . queue [ - <NUM_LIT:1> ] . time <EOL> except IndexError : <EOL> self . endtime = <NUM_LIT:0> <EOL> def start ( self ) : <EOL> '''<STR_LIT>''' <EOL> laststate = self . state <EOL> self . state = self . STATE_RUNNING <EOL> if laststate == self . STATE_STOPPED or laststate == self . STATE_RUNNING : <EOL> self . loopwaypoints ( ) <EOL> self . timezero = <NUM_LIT:0> <EOL> self . lasttime = <NUM_LIT:0> <EOL> self . run ( ) <EOL> elif laststate == self . STATE_PAUSED : <EOL> now = time . time ( ) <EOL> self . timezero += now - self . lasttime <EOL> self . lasttime = now - ( <NUM_LIT> * self . refresh_ms ) <EOL> self . runround ( ) <EOL> def stop ( self , move_initial = True ) : <EOL> '''<STR_LIT>''' <EOL> self . state = self . STATE_STOPPED <EOL> self . loopwaypoints ( ) <EOL> self . timezero = <NUM_LIT:0> <EOL> self . lasttime = <NUM_LIT:0> <EOL> if move_initial : <EOL> self . movenodesinitial ( ) <EOL> self . session . mobility . sendevent ( self ) <EOL> def pause ( self ) : <EOL> '''<STR_LIT>''' <EOL> self . state = self . STATE_PAUSED <EOL> self . lasttime = time . time ( ) <EOL> class Ns2ScriptedMobility ( WayPointMobility ) : <EOL> '''<STR_LIT>''' <EOL> _name = "<STR_LIT>" <EOL> _confmatrix = [ <EOL> ( "<STR_LIT:file>" , coreapi . CONF_DATA_TYPE_STRING , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( "<STR_LIT>" , coreapi . CONF_DATA_TYPE_UINT32 , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( "<STR_LIT>" , coreapi . CONF_DATA_TYPE_BOOL , '<STR_LIT:1>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( "<STR_LIT>" , coreapi . CONF_DATA_TYPE_STRING , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( "<STR_LIT>" , coreapi . CONF_DATA_TYPE_STRING , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( "<STR_LIT>" , coreapi . CONF_DATA_TYPE_STRING , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( "<STR_LIT>" , coreapi . CONF_DATA_TYPE_STRING , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( "<STR_LIT>" , coreapi . CONF_DATA_TYPE_STRING , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ] <EOL> _confgroups = "<STR_LIT>" % len ( _confmatrix ) <EOL> def __init__ ( self , session , objid , verbose = False , values = None ) : <EOL> '''<STR_LIT:U+0020>''' <EOL> super ( Ns2ScriptedMobility , self ) . __init__ ( session = session , objid = objid , <EOL> verbose = verbose , values = values ) <EOL> self . _netifs = { } <EOL> self . _netifslock = threading . Lock ( ) <EOL> if values is None : <EOL> values = session . mobility . getconfig ( objid , self . _name , <EOL> self . getdefaultvalues ( ) ) [ <NUM_LIT:1> ] <EOL> self . file = self . valueof ( "<STR_LIT:file>" , values ) <EOL> self . refresh_ms = int ( self . valueof ( "<STR_LIT>" , values ) ) <EOL> self . loop = ( self . valueof ( "<STR_LIT>" , values ) . lower ( ) == "<STR_LIT>" ) <EOL> self . autostart = self . valueof ( "<STR_LIT>" , values ) <EOL> self . parsemap ( self . valueof ( "<STR_LIT>" , values ) ) <EOL> self . script_start = self . valueof ( "<STR_LIT>" , values ) <EOL> self . script_pause = self . valueof ( "<STR_LIT>" , values ) <EOL> self . script_stop = self . valueof ( "<STR_LIT>" , values ) <EOL> if self . verbose : <EOL> self . session . info ( "<STR_LIT>" "<STR_LIT>" % ( objid , self . file ) ) <EOL> self . readscriptfile ( ) <EOL> self . copywaypoints ( ) <EOL> self . setendtime ( ) <EOL> @ classmethod <EOL> def configure_mob ( cls , session , msg ) : <EOL> '''<STR_LIT>''' <EOL> return cls . configure ( session . mobility , msg ) <EOL> def readscriptfile ( self ) : <EOL> '''<STR_LIT>''' <EOL> filename = self . findfile ( self . file ) <EOL> try : <EOL> f = open ( filename , '<STR_LIT:r>' ) <EOL> except IOError , e : <EOL> self . session . warn ( "<STR_LIT>" "<STR_LIT>" % ( self . file , e ) ) <EOL> return <EOL> if self . verbose : <EOL> self . session . info ( "<STR_LIT>" % filename ) <EOL> ln = <NUM_LIT:0> <EOL> ix = iy = iz = None <EOL> inodenum = None <EOL> for line in f : <EOL> ln += <NUM_LIT:1> <EOL> if line [ : <NUM_LIT:2> ] != '<STR_LIT>' : <EOL> continue <EOL> try : <EOL> if line [ : <NUM_LIT:8> ] == "<STR_LIT>" : <EOL> if ix is not None and iy is not None : <EOL> self . addinitial ( self . map ( inodenum ) , ix , iy , iz ) <EOL> ix = iy = iz = None <EOL> parts = line . split ( ) <EOL> time = float ( parts [ <NUM_LIT:2> ] ) <EOL> nodenum = parts [ <NUM_LIT:3> ] [ <NUM_LIT:1> + parts [ <NUM_LIT:3> ] . index ( '<STR_LIT:(>' ) : parts [ <NUM_LIT:3> ] . index ( '<STR_LIT:)>' ) ] <EOL> x = float ( parts [ <NUM_LIT:5> ] ) <EOL> y = float ( parts [ <NUM_LIT:6> ] ) <EOL> z = None <EOL> speed = float ( parts [ <NUM_LIT:7> ] . strip ( '<STR_LIT:">' ) ) <EOL> self . addwaypoint ( time , self . map ( nodenum ) , x , y , z , speed ) <EOL> elif line [ : <NUM_LIT:7> ] == "<STR_LIT>" : <EOL> parts = line . split ( ) <EOL> time = <NUM_LIT:0.0> <EOL> nodenum = parts [ <NUM_LIT:0> ] [ <NUM_LIT:1> + parts [ <NUM_LIT:0> ] . index ( '<STR_LIT:(>' ) : parts [ <NUM_LIT:0> ] . index ( '<STR_LIT:)>' ) ] <EOL> if parts [ <NUM_LIT:2> ] == '<STR_LIT>' : <EOL> if ix is not None and iy is not None : <EOL> self . addinitial ( self . map ( inodenum ) , ix , iy , iz ) <EOL> ix = iy = iz = None <EOL> ix = float ( parts [ <NUM_LIT:3> ] ) <EOL> elif parts [ <NUM_LIT:2> ] == '<STR_LIT>' : <EOL> iy = float ( parts [ <NUM_LIT:3> ] ) <EOL> elif parts [ <NUM_LIT:2> ] == '<STR_LIT>' : <EOL> iz = float ( parts [ <NUM_LIT:3> ] ) <EOL> self . addinitial ( self . map ( nodenum ) , ix , iy , iz ) <EOL> ix = iy = iz = None <EOL> inodenum = nodenum <EOL> else : <EOL> raise ValueError <EOL> except ValueError , e : <EOL> self . session . warn ( "<STR_LIT>" % ( ln , self . file , line , e ) ) <EOL> continue <EOL> if ix is not None and iy is not None : <EOL> self . addinitial ( self . map ( inodenum ) , ix , iy , iz ) <EOL> def findfile ( self , fn ) : <EOL> '''<STR_LIT>''' <EOL> if os . path . exists ( fn ) : <EOL> return fn <EOL> if self . session . filename is not None : <EOL> d = os . path . dirname ( self . session . filename ) <EOL> sessfn = os . path . join ( d , fn ) <EOL> if ( os . path . exists ( sessfn ) ) : <EOL> return sessfn <EOL> if self . session . user is not None : <EOL> userfn = os . path . join ( '<STR_LIT>' , self . session . user , '<STR_LIT>' , '<STR_LIT>' , fn ) <EOL> if ( os . path . exists ( userfn ) ) : <EOL> return userfn <EOL> return fn <EOL> def parsemap ( self , mapstr ) : <EOL> '''<STR_LIT>''' <EOL> self . nodemap = { } <EOL> if mapstr . strip ( ) == '<STR_LIT>' : <EOL> return <EOL> for pair in mapstr . split ( '<STR_LIT:U+002C>' ) : <EOL> parts = pair . split ( '<STR_LIT::>' ) <EOL> try : <EOL> if len ( parts ) != <NUM_LIT:2> : <EOL> raise ValueError <EOL> self . nodemap [ int ( parts [ <NUM_LIT:0> ] ) ] = int ( parts [ <NUM_LIT:1> ] ) <EOL> except ValueError : <EOL> self . session . warn ( "<STR_LIT>" ) <EOL> return <EOL> def map ( self , nodenum ) : <EOL> '''<STR_LIT>''' <EOL> nodenum = int ( nodenum ) <EOL> try : <EOL> return self . nodemap [ nodenum ] <EOL> except KeyError : <EOL> return nodenum <EOL> def startup ( self ) : <EOL> '''<STR_LIT>''' <EOL> if self . autostart == '<STR_LIT>' : <EOL> if self . verbose : <EOL> self . session . info ( "<STR_LIT>" % self . wlan . name ) <EOL> return <EOL> try : <EOL> t = float ( self . autostart ) <EOL> except ValueError : <EOL> self . session . warn ( "<STR_LIT>" "<STR_LIT:%s>" % ( self . autostart , self . wlan . name ) ) <EOL> return <EOL> self . movenodesinitial ( ) <EOL> if self . verbose : <EOL> self . session . info ( "<STR_LIT>" % ( self . wlan . name , t ) ) <EOL> self . state = self . STATE_RUNNING <EOL> self . session . evq . add_event ( t , self . run ) <EOL> def start ( self ) : <EOL> '''<STR_LIT>''' <EOL> laststate = self . state <EOL> super ( Ns2ScriptedMobility , self ) . start ( ) <EOL> if laststate == self . STATE_PAUSED : <EOL> self . statescript ( "<STR_LIT>" ) <EOL> def run ( self ) : <EOL> '''<STR_LIT>''' <EOL> super ( Ns2ScriptedMobility , self ) . run ( ) <EOL> self . statescript ( "<STR_LIT>" ) <EOL> def pause ( self ) : <EOL> super ( Ns2ScriptedMobility , self ) . pause ( ) <EOL> self . statescript ( "<STR_LIT>" ) <EOL> def stop ( self , move_initial = True ) : <EOL> super ( Ns2ScriptedMobility , self ) . stop ( move_initial = move_initial ) <EOL> self . statescript ( "<STR_LIT>" ) <EOL> def statescript ( self , typestr ) : <EOL> filename = None <EOL> if typestr == "<STR_LIT>" or typestr == "<STR_LIT>" : <EOL> filename = self . script_start <EOL> elif typestr == "<STR_LIT>" : <EOL> filename = self . script_pause <EOL> elif typestr == "<STR_LIT>" : <EOL> filename = self . script_stop <EOL> if filename is None or filename == '<STR_LIT>' : <EOL> return <EOL> filename = self . findfile ( filename ) <EOL> try : <EOL> check_call ( [ "<STR_LIT>" , filename , typestr ] , <EOL> cwd = self . session . sessiondir , <EOL> env = self . session . getenviron ( ) ) <EOL> except Exception , e : <EOL> self . session . warn ( "<STR_LIT>" "<STR_LIT:%s>" % ( filename , typestr , e ) ) </s>
