forest-net
an overlay networks for large-scale virtual worlds
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator
forest::Avatar Class Reference

Class that implements a simulated avatar in a virtual world. More...

#include <Avatar.h>

Collaboration diagram for forest::Avatar:

Public Member Functions

 Avatar (ipa_t, comt_t, comt_t)
 Constructor allocates space and initializes private data. More...
 
bool init (ipa_t, string &, string &, char *)
 Perform all required initialization. More...
 
void run (uint32_t)
 Main Avatar processing loop. More...
 

Static Public Attributes

static const int STATUS_REPORT = 1
 status report payload code
 

Private Types

enum  STATE { IDLE, JOINING, LEAVING }
 

Private Member Functions

int groupNum (int, int)
 Return the multicast group number associated with given position. More...
 
bool login (ipa_t, string, string)
 Send username and password to the Client manager, and receive. More...
 
bool setupWalls (const char *)
 Setup the internal representation of the walls. More...
 
bool separated (int, int)
 Determine if two adjacent squares are separated by a wall. More...
 
void updateVisSet ()
 
void computeVisSet (int, set< int > &)
 Compute a visbility set for a given square in the virtual world. More...
 
bool isVis (int, int)
 Determine if two squares are visible from each other. More...
 
bool linesIntersect (double, double, double, double, double, double, double, double)
 
void updateStatus (uint32_t)
 Update status of avatar based on passage of time. More...
 
void updateNearby (int)
 Update the set of nearby Avatars. More...
 
void updateSubs ()
 Update subscriptions without changing comtrees. More...
 
void sendStatus (int)
 Send a status packet on the multicast group for the current location. More...
 
void startComtSwitch (comt_t, uint32_t)
 Start the process of switching to a new comtree. More...
 
bool completeComtSwitch (pktx, uint32_t)
 Attempt to complete the process of switching to a new comtree. More...
 
void send2comtCtl (CtlPkt::CpType, bool=false)
 Send join or leave packet packet to the ComtreeController. More...
 
comt_t check4command ()
 Check for a new command from remote display program. More...
 
void forwardReport (uint32_t, int, int=0)
 Send a status report to the remote controller for this avatar. More...
 
void subscribeAll ()
 Subscribe to all multicasts for regions that are currently visible and that we're not already subscribed to.
 
void unsubscribeAll ()
 Unsubscribe from all multicasts that we're currently subscribed to.
 
void subscribe (list< int > &)
 
void unsubscribe (list< int > &)
 Unsubscribe from a list of multicast groups. More...
 
bool connect ()
 Send initial connect packet to forest router Uses comtree 1, which is for user signalling.
 
bool disconnect ()
 Send final disconnect packet to forest router.
 
void send (int)
 Send packet to forest router and recycle storage.
 
int receive ()
 Return next waiting packet or 0 if there is none.
 

Private Attributes

fAdr_t myAdr
 forest address of host
 
ipa_t myIp
 IP address of interface.
 
fAdr_t rtrAdr
 forest address of router
 
ipa_t rtrIp
 IP address of router.
 
ipp_t rtrPort
 IP port of router.
 
fAdr_t ccAdr
 forest address of ComtreeController
 
int sock
 socket number for forest network
 
ipp_t listenPort
 port to connect to Controller on
 
int listenSock
 listen socket for remote driver
 
int connSock
 for connected socket
 
uint64_t nonce
 nonce used when connecting
 
comt_t comt
 current comtree
 
comt_t firstComt
 lower range of comtrees
 
comt_t lastComt
 upper range of comtrees
 
int x
 x coordinate in virtual world
 
int y
 y coordinate in virtual world
 
double direction
 direction avatar is facing in deg
 
double deltaDir
 change in direction per period
 
double speed
 speed moving in UNITS/update period
 
int worldSize
 

of grid squares per dimension


 
char * walls
 array of walls
 
set< int > * myVisSet
 myVisSet contains multicast groups More...
 
STATE switchState
 current state
 
uint32_t switchTimer
 time last signalling packet sent
 
int switchCnt
 number of attempts so far
 
comt_t nextComt
 comtree we're switching to
 
int seqNum
 sequence number of control packet
 
long long subSeqNum
 sequence number for subscriptions
 
set< int > * mySubs
 multicast group subscriptions
 
HashSet * visibleAvatars
 set of visible avatars
 
HashSet * nearAvatars
 set of nearby avatars More...
 
int numVisible
 number of visible avatars
 
int numNear
 number of nearby avatars
 
PacketStoreps
 pointer to packet store
 

Static Private Attributes

static const int UPDATE_PERIOD = 50
 

ms between status updates


 
static const int LISTEN_PORT = 30130
 port for remote controller
 
static const int NUM_ITEMS = 10
 

of items in status report


 
static const int GRID = 10000
 xy extent of one grid square
 
static const int MAXNEAR = 1000
 max # of nearby avatars
 
static const int SLOW =100
 slow avatar speed
 
static const int MEDIUM =250
 medium avatar speed
 
static const int FAST =600
 fast avatar speed
 
static const int STOPPED = 0
 
static const int MAX_VIS =20
 max distance can "see" (in squares)
 
static const uint32_t SWITCH_TIMEOUT = 500000
 500 ms timeout
 
static const bool RETRY = true
 flag used to signal retry
 

Detailed Description

Class that implements a simulated avatar in a virtual world.

This class implements an avatar in a very simple virtual world. The world is just a square area that is divided into a set of grid squares. The avatar wanders randomly around the square, issuing status reports periodically, indicating where it is, the direction in which it's moving and its velocity. The status reports are sent on a multicast group associated with the the grid square it's currently in. The Avatar also subscribes to multicasts for all squares within its "visibility range".

Definition at line 36 of file Avatar.h.

Constructor & Destructor Documentation

forest::Avatar::Avatar ( ipa_t  mipa,
comt_t  fc,
comt_t  lc 
)

Constructor allocates space and initializes private data.

Parameters
mipais this host's IP address
fcis the first comtree in the range used by the avatar
lcis the last comtree in the range used by the avatar

Definition at line 69 of file Avatar-save.cpp.

Member Function Documentation

comt_t forest::Avatar::check4command ( )
private

Check for a new command from remote display program.

Check to see if the remote display has sent a comand and respond appropriately. Commands take the form of pairs (c,i) where c is a character that identifies a specific command, and i is an integer that provides an optional parameter value for the command. The current commands are defined below.

j steer the Avatar to the left l steer the Avatar to the right i speed up the Avatar k slow down the Avatar c change comtree to the given parameter value

Note that the integer parameter is sent even when the command doesn't use it

Returns
the specified comtree value when a 'c' command is received or 0 in all other cases

Definition at line 703 of file Avatar-save.cpp.

Here is the call graph for this function:

Here is the caller graph for this function:

bool forest::Avatar::completeComtSwitch ( pktx  px,
uint32_t  now 
)
private

Attempt to complete the process of switching to a new comtree.

This involves completing signalling interactions with ComtCtl, including re-sending signalling packets when timeouts occur.

Parameters
pis a packet number, or 0 if the caller just wants to check for a timeout
nowis the current time
Returns
false so long as the switch is in progress; return true if the switch completes successfully, or if it fails (either because the ComtCtl sent a negative response or because it never responded after repeated attempts); in the latter case, set comt to 0

Definition at line 519 of file Avatar-save.cpp.

Here is the call graph for this function:

Here is the caller graph for this function:

void forest::Avatar::computeVisSet ( int  g1,
set< int > &  vSet 
)
private

Compute a visbility set for a given square in the virtual world.

Parameters
g1is the multicast group number for a square in the virtual world
vSetis a reference to a visibility set in which the result is to be returned

Definition at line 266 of file Avatar-save.cpp.

Here is the call graph for this function:

void forest::Avatar::forwardReport ( uint32_t  now,
int  avType,
int  px = 0 
)
private

Send a status report to the remote controller for this avatar.

Parameters
nowis the current time
avTypeis the type code for the avatar whose status is being sent: 1 means the Avatar for "this" object, 2 a visible avatar and 3 an avatar that we can "hear" but not see
pis an optional packet number; if present and non-zero, its payload is unpacked to produce the report to send to the avatar

Definition at line 621 of file Avatar-save.cpp.

Here is the call graph for this function:

Here is the caller graph for this function:

int forest::Avatar::groupNum ( int  x1,
int  y1 
)
private

Return the multicast group number associated with given position.

Assumes that SIZE is an integer multiple of GRID

Parameters
x1is the x coordinate of the position of interest
y1is the y coordinate of the position of interest

Definition at line 987 of file Avatar-save.cpp.

Here is the caller graph for this function:

bool forest::Avatar::init ( ipa_t  cmIpAdr,
string &  uname,
string &  pword,
char *  wallsFile 
)

Perform all required initialization.

Returns
true on success, false on failure

Definition at line 93 of file Avatar-save.cpp.

Here is the call graph for this function:

Here is the caller graph for this function:

bool forest::Avatar::isVis ( int  g1,
int  g2 
)
private

Determine if two squares are visible from each other.

Parameters
g1is the group number of the first square
g2is the group number of the second square

Definition at line 996 of file Avatar-save.cpp.

Here is the caller graph for this function:

bool forest::Avatar::login ( ipa_t  cmIpAdr,
string  uname,
string  pword 
)
private

Send username and password to the Client manager, and receive.

Parameters
cmIpAdris the IP address of the client manager
unameis the username to log in with
pwordis the password to log in with
Returns
true on success, false on failure

Definition at line 143 of file Avatar-save.cpp.

Here is the call graph for this function:

Here is the caller graph for this function:

void forest::Avatar::run ( uint32_t  finishTime)

Main Avatar processing loop.

Operates on a cycle with a period of UPDATE_PERIOD milliseconds, Each cycle, update the current position, direction, speed; issue new SUB_UNSUB packet if necessary; read all incoming status reports and update the set of nearby avatars; finally, send new status report

Parameters
finishTimeis the number of microseconds to to run before halting.

Definition at line 400 of file Avatar-save.cpp.

Here is the call graph for this function:

Here is the caller graph for this function:

void forest::Avatar::send2comtCtl ( CtlPkt::CpType  joinLeave,
bool  retry = false 
)
private

Send join or leave packet packet to the ComtreeController.

Parameters
joinLeaveis either CLIENT_JOIN_COMTREE or CLIENT_LEAVE_COMTREE, depending on whether we want to join or leave the comtree

Definition at line 663 of file Avatar-save.cpp.

Here is the call graph for this function:

Here is the caller graph for this function:

void forest::Avatar::sendStatus ( int  now)
private

Send a status packet on the multicast group for the current location.

Parameters
nowis the reference time for the status report

Definition at line 594 of file Avatar-save.cpp.

Here is the call graph for this function:

Here is the caller graph for this function:

bool forest::Avatar::separated ( int  c0,
int  c1 
)
private

Determine if two adjacent squares are separated by a wall.

Parameters
c0is the index of a square
c0is the index of an adjacent square (may be diagonal)
Returns
true if there are walls separating c0 and c1, else false

Definition at line 957 of file Avatar-save.cpp.

Here is the caller graph for this function:

bool forest::Avatar::setupWalls ( const char *  wallsFile)
private

Setup the internal representation of the walls.

Parameters
wallsFilecontains the specification of the walls in the virtual world
Returns
true on success, false on failure

Definition at line 215 of file Avatar-save.cpp.

Here is the caller graph for this function:

void forest::Avatar::startComtSwitch ( comt_t  newComt,
uint32_t  now 
)
private

Start the process of switching to a new comtree.

Unsubscribe to the current comtree and leave send signalling message to leave the current comtree.

Parameters
newComtis the number of the comtree we're switching to
nowis the current time

Definition at line 492 of file Avatar-save.cpp.

Here is the call graph for this function:

Here is the caller graph for this function:

void forest::Avatar::unsubscribe ( list< int > &  glist)
private

Unsubscribe from a list of multicast groups.

Parameters
glistis a reference to a list of multicast group numbers.

Definition at line 1150 of file Avatar-save.cpp.

Here is the call graph for this function:

Here is the caller graph for this function:

void forest::Avatar::updateNearby ( int  px)
private

Update the set of nearby Avatars.

If the given packet is a status report, check to see if the sending avatar is visible. If it is visible, but not in our set of nearby avatars, then add it. If it is not visible but is in our set of nearby avatars, then delete it. Note: we assume that the visibility range and avatar speeds are such that we will get at least one report from a newly invisible avatar.

Definition at line 1247 of file Avatar-save.cpp.

Here is the call graph for this function:

Here is the caller graph for this function:

void forest::Avatar::updateStatus ( uint32_t  now)
private

Update status of avatar based on passage of time.

Parameters
nowis the reference time for the simulated update
controlInputis the input code from the controller, or -1 if no controller is connected, or 0 if no current input

Definition at line 869 of file Avatar-save.cpp.

Here is the call graph for this function:

Here is the caller graph for this function:

void forest::Avatar::updateSubs ( )
private

Update subscriptions without changing comtrees.

Unsubscribe from all multicasts for squares that are not visible from our current position, then subscribe to all missing multicasts that are visible.

Definition at line 1209 of file Avatar-save.cpp.

Here is the call graph for this function:

Here is the caller graph for this function:

Member Data Documentation

set<int>* forest::Avatar::myVisSet
private

myVisSet contains multicast groups

for squares visible from (x,y)

Definition at line 85 of file Avatar.h.

HashSet* forest::Avatar::nearAvatars
private

set of nearby avatars

(those we receive packets from)

Definition at line 104 of file Avatar.h.


The documentation for this class was generated from the following files: