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

Class that implements a table of information about users. More...

#include <ClientTable.h>

Collaboration diagram for forest::ClientTable:

Classes

struct  Client
 
struct  Session
 

Public Types

enum  sessionState {
  NUL_STATE, IDLE, PENDING, SUSPENDED,
  CONNECTED
}
 
enum  privileges {
  NUL_PRIV, LIMITED, STANDARD, ADMIN,
  ROOT
}
 

Public Member Functions

 ClientTable (int, int)
 Constructor for ClientTable, allocates space and initializes table. More...
 
 ~ClientTable ()
 Destructor for ClientTable, frees dynamic storage.
 
bool init ()
 Initialize lock and condition variables. More...
 
const RateSpecgetDefRates () const
 Get the intial default rate spec for new clients. More...
 
const RateSpecgetTotalRates () const
 Get the intial total rate spec for new clients. More...
 
bool validClient (int) const
 
int firstClient ()
 Get the first client in the list of valid clients. More...
 
int nextClient (int)
 Get the index of the next client. More...
 
int firstSession (int) const
 Get the first session index for a client. More...
 
int nextSession (int, int) const
 Get the next session index for a client. More...
 
bool isLocked (int) const
 
int getClient (const string &)
 Lock a client's table entry. More...
 
void releaseClient (int)
 Release a previously locked client table entry. More...
 
int getSession (fAdr_t)
 Get a session by its forest address and lock its client's table entry. More...
 
const string & getPassword (int) const
 Get a client's password. More...
 
const string & getClientName (int) const
 Get a client's name. More...
 
const string & getRealName (int) const
 Get a client's real world name. More...
 
const string & getEmail (int) const
 Get a client's email address. More...
 
privileges getPrivileges (int) const
 Get a client's privileges. More...
 
RateSpecgetDefRates (int) const
 Get the default rate spec for a client. More...
 
RateSpecgetTotalRates (int) const
 Get the total rate spec for a client. More...
 
RateSpecgetAvailRates (int) const
 Get the available rate spec for a client. More...
 
int getNumSess (int) const
 Get the number of sessions for a client. More...
 
bool checkPassword (int, string &) const
 Get a client's password. More...
 
int getNumClients () const
 
int getMaxClients () const
 
int getMaxClx () const
 
int getClientIndex (int) const
 Get the index of a client for a given session. More...
 
fAdr_t getClientAdr (int) const
 Get the client address for a session. More...
 
ipa_t getClientIp (int) const
 Get the client address for a session. More...
 
fAdr_t getRouterAdr (int) const
 Get the address of the client's router for a session. More...
 
sessionState getState (int) const
 
time_t getStartTime (int) const
 Get the address of the client's router for a session. More...
 
RateSpecgetSessRates (int) const
 Get the rate spec for a session. More...
 
int addClient (string &, string &, privileges, int=0)
 Add a new client. More...
 
void removeClient (int)
 Remove a client. More...
 
int addSession (fAdr_t, fAdr_t, int)
 Add a new session. More...
 
void removeSession (int)
 Remove a session. More...
 
void setClientName (int, const string &)
 Set a client's name. More...
 
void setPassword (int, const string &)
 Set a client's password. More...
 
void setPrivileges (int, privileges)
 Set a client's privileges. More...
 
void setRealName (int, const string &)
 Set a client's real world name. More...
 
void setEmail (int, const string &)
 Set a client's email address. More...
 
void setClientIndex (int, int)
 
void setClientIp (int, ipa_t)
 Set the client address for a session. More...
 
void setRouterAdr (int, fAdr_t)
 Set the address of the client's router for a session. More...
 
void setState (int, sessionState)
 Set the state for a session. More...
 
void setStartTime (int, time_t)
 Set the start time for a session. More...
 
bool readEntry (istream &, int=0)
 Read a client record from an input file and initialize its table entry. More...
 
bool read (istream &)
 Read a client entry from a NetBuffer. More...
 
string & toString (string &, bool=false)
 Create a string representation of the client table Does no locking. More...
 
string & client2string (int, string &, bool=false) const
 Construct a string representation of a client. More...
 
string & session2string (int, string &) const
 Construct a string representation of a session. More...
 
void write (ostream &, bool=false)
 Write the complete client table to an output stream. More...
 
void lockMap ()
 Lock the client table. More...
 
void unlockMap ()
 Unlock the client table.
 

Private Member Functions

uint64_t key (fAdr_t) const
 helper functions More...
 
bool readEntry (istream &)
 
int fileSize ()
 

Private Attributes

int maxCli
 max number of clients
 
int maxSess
 max number of active sessions
 
int maxClx
 largest defined clx
 
Sessionsvec
 vector of session structs
 
UiClist * sessLists
 circular lists of session indexes
 
RateSpec defRates
 initial default rates
 
RateSpec totalRates
 initial total rates
 
Clientcvec
 vector of client structs
 
fstream clientFile
 file stream for client file
 
UiSetPair * clients
 active and free client indexes
 
IdMap * sessMap
 maps address to a session index
 
map< string, int > * nameMap
 maps client name to client index
 
pthread_mutex_t mapLock
 must hold during add/remove ops More...
 

Static Private Attributes

static const int RECORD_SIZE = 256
 

of bytes per record


 

Detailed Description

Class that implements a table of information about users.

Table entries are accessed using a "client index", which can be obtained using the getClient() method. This also locks the client's table entry to permit exclusive access to the data for that client and its sessions. Other methods also lock table entries.

Definition at line 29 of file ClientTable.h.

Constructor & Destructor Documentation

forest::ClientTable::ClientTable ( int  maxClients,
int  maxSessions 
)

Constructor for ClientTable, allocates space and initializes table.

Definition at line 15 of file ClientTable-sav.cpp.

Here is the call graph for this function:

Member Function Documentation

int forest::ClientTable::addClient ( string &  cname,
string &  pwd,
privileges  priv,
int  clx = 0 
)

Add a new client.

Attempts to add a new client to the table. Can fail if the specified user name is already in use, or if there is no more space. On return, the new client's table entry is locked; the caller must release it when done.

Parameters
cnameis the client name
pwdis the client password
privis the assigned privilege level
clxis an optional argument specifying the client index to be used for this new client; if clx == 0, the client index is assigned automatically (this is the default behavior)
Returns
the index of the new table entry or 0 on failure; can fail if cname clashes with an existing name, or there is no space left

Definition at line 217 of file ClientTable-sav.cpp.

Here is the call graph for this function:

Here is the caller graph for this function:

int forest::ClientTable::addSession ( fAdr_t  cliAdr,
fAdr_t  rtrAdr,
int  clx 
)

Add a new session.

Attempts to add a new session to an existing client.

Parameters
cliAdris the forest address assigned to client
rtrAdris the forest address of the router assigned to client
clxis the client index of a valid client for which the caller is assumed to hold a lock
Returns
the index of the new session or 0 on failure; fails if clx there is already a session using the specified client address

Definition at line 267 of file ClientTable-sav.cpp.

Here is the call graph for this function:

Here is the caller graph for this function:

bool forest::ClientTable::checkPassword ( int  clx,
string &  pwd 
) const
inline

Get a client's password.

Parameters
clxis a valid client index
pwdis a reference to a string containing the client's password
Returns
true if the provided password is consistent with the stored value

Definition at line 249 of file ClientTable.h.

Here is the caller graph for this function:

string & forest::ClientTable::client2string ( int  clx,
string &  s,
bool  includeSess = false 
) const

Construct a string representation of a client.

This metghod does no locking.

Parameters
clxis the client index of the client to be written
sis a reference to a string in which result is returned
includeSessis true if sessions are to be included, default=false
Returns
a reference to s

Definition at line 435 of file ClientTable-sav.cpp.

Here is the call graph for this function:

Here is the caller graph for this function:

int forest::ClientTable::firstClient ( )

Get the first client in the list of valid clients.

Returns
the index of the first valid client, or 0 if there is no valid client; on a successful return, the client is locked

Definition at line 152 of file ClientTable-sav.cpp.

Here is the call graph for this function:

Here is the caller graph for this function:

int forest::ClientTable::firstSession ( int  clx) const
inline

Get the first session index for a client.

This method is used to iterate through the active sessions of a client. The order of the sessions indices is arbitrary. The caller should already hold a lock for the client.

Parameters
clxis a client index
Returns
the first session index for clx, or 0 if there is no session

Definition at line 201 of file ClientTable.h.

Here is the caller graph for this function:

RateSpec & forest::ClientTable::getAvailRates ( int  clx) const
inline

Get the available rate spec for a client.

Parameters
clxis a client index
Returns
a non-const reference to the rate spec for clx

Definition at line 289 of file ClientTable.h.

Here is the caller graph for this function:

int forest::ClientTable::getClient ( const string &  cname)

Lock a client's table entry.

Parameters
clxis a client index
Returns
true if able to a lock on the specified client; return false if the client index does not refer to a valid client bool ClientTable::lockClient(int clx) { lockMap(); if (!clients->isIn(clx)) { unlockMap(); return 0; } while (cvec[clx].busyBit) { // wait until client's entry is not busy pthread_cond_wait(&cvec[clx].busyCond,&mapLock); if (!clients->isIn(clx)) { pthread_cond_signal(&cvec[clx].busyCond); unlockMap(); return false; } } cvec[clx].busyBit = true; // set busyBit to lock client table entry unlockMap(); return true; }Get a client by name and lock its table entry.
Parameters
cnameis a client name
Returns
the client index associated with the given client name, or 0 if the name does not match any client; on a successful return, the table entry for the client is locked; the caller must release it when done using it

Definition at line 93 of file ClientTable-sav.cpp.

Here is the call graph for this function:

Here is the caller graph for this function:

fAdr_t forest::ClientTable::getClientAdr ( int  sess) const
inline

Get the client address for a session.

Parameters
sessis a session index
Returns
the Forest address of the client in this session

Definition at line 305 of file ClientTable.h.

Here is the caller graph for this function:

int forest::ClientTable::getClientIndex ( int  sess) const
inline

Get the index of a client for a given session.

Parameters
sessis the session index
Returns
the client index, or 0 if not a valid session

Definition at line 321 of file ClientTable.h.

Here is the caller graph for this function:

ipa_t forest::ClientTable::getClientIp ( int  sess) const
inline

Get the client address for a session.

Parameters
sessis a session index
Returns
the Forest address of the client in this session

Definition at line 313 of file ClientTable.h.

Here is the caller graph for this function:

const string & forest::ClientTable::getClientName ( int  clx) const
inline

Get a client's name.

Parameters
clxis a valid client index
Returns
a const reference to the client name string

Definition at line 232 of file ClientTable.h.

Here is the caller graph for this function:

const RateSpec & forest::ClientTable::getDefRates ( ) const
inline

Get the intial default rate spec for new clients.

Returns
a const reference to the initial default rate spec

Definition at line 178 of file ClientTable.h.

Here is the caller graph for this function:

RateSpec & forest::ClientTable::getDefRates ( int  clx) const
inline

Get the default rate spec for a client.

Parameters
clxis a client index
Returns
a non-const reference to the rate spec for clx

Definition at line 281 of file ClientTable.h.

const string & forest::ClientTable::getEmail ( int  clx) const
inline

Get a client's email address.

Parameters
clxis a valid client index
Returns
a const reference to the client's email string

Definition at line 265 of file ClientTable.h.

Here is the caller graph for this function:

int forest::ClientTable::getNumSess ( int  clx) const
inline

Get the number of sessions for a client.

Parameters
clxis a valid client index
Returns
a the number of active or pending sessions for this client.

Definition at line 240 of file ClientTable.h.

const string & forest::ClientTable::getPassword ( int  clx) const
inline

Get a client's password.

The caller should already hold a lock for the client.

Parameters
clxis a valid client index
Returns
a const reference to the password string.

Definition at line 224 of file ClientTable.h.

Here is the caller graph for this function:

ClientTable::privileges forest::ClientTable::getPrivileges ( int  clx) const
inline

Get a client's privileges.

Parameters
clxis a valid client index
Returns
the client's privileges.

Definition at line 273 of file ClientTable.h.

Here is the caller graph for this function:

const string & forest::ClientTable::getRealName ( int  clx) const
inline

Get a client's real world name.

Parameters
clxis a valid client index
Returns
a const reference to the client's realName string

Definition at line 257 of file ClientTable.h.

Here is the caller graph for this function:

fAdr_t forest::ClientTable::getRouterAdr ( int  sess) const
inline

Get the address of the client's router for a session.

Parameters
sessis a session index
Returns
the Forest address of the router used by the client in this session

Definition at line 329 of file ClientTable.h.

Here is the caller graph for this function:

int forest::ClientTable::getSession ( fAdr_t  cliAdr)

Get a session by its forest address and lock its client's table entry.

Parameters
cliAdris a forest address
Returns
the session index associated with the given client address, or 0 if the name does not match any known session; on a successful return, the table entry for the session's client is locked; the caller must release it when done using it

Definition at line 129 of file ClientTable-sav.cpp.

Here is the call graph for this function:

Here is the caller graph for this function:

RateSpec & forest::ClientTable::getSessRates ( int  sess) const
inline

Get the rate spec for a session.

Parameters
sessis a session index
Returns
a non-const reference to the rate spec for the session

Definition at line 345 of file ClientTable.h.

Here is the caller graph for this function:

time_t forest::ClientTable::getStartTime ( int  sess) const
inline

Get the address of the client's router for a session.

Parameters
sessis a session index
Returns
the time this session started (in seconds since the epoch)

Definition at line 337 of file ClientTable.h.

Here is the caller graph for this function:

const RateSpec & forest::ClientTable::getTotalRates ( ) const
inline

Get the intial total rate spec for new clients.

Returns
a const reference to the initial total rate spec

Definition at line 185 of file ClientTable.h.

Here is the caller graph for this function:

RateSpec & forest::ClientTable::getTotalRates ( int  clx) const
inline

Get the total rate spec for a client.

Parameters
clxis a client index
Returns
a non-const reference to the total rate spec for clx

Definition at line 297 of file ClientTable.h.

bool forest::ClientTable::init ( )

Initialize lock and condition variables.

Definition at line 37 of file ClientTable-sav.cpp.

Here is the caller graph for this function:

uint64_t forest::ClientTable::key ( fAdr_t  cliAdr) const
inlineprivate

helper functions

Compute key for use with sessMap.

Parameters
cliAdris a client address
Returns
a 64 bit hash key

Definition at line 428 of file ClientTable.h.

Here is the caller graph for this function:

void forest::ClientTable::lockMap ( )
inline

Lock the client table.

This method is meant primarily for internal use. Applications should normally just lock single clients using the methods provided for that purpose. Use extreme caution when using this method directly.

Definition at line 438 of file ClientTable.h.

Here is the caller graph for this function:

int forest::ClientTable::nextClient ( int  clx)

Get the index of the next client.

The caller is assumed to have a lock on the current client.

Parameters
clxis the index of a valid client
Returns
the index of the next client in the list of valid clients or 0 if there is no next client; on return, the lock on clx is released and the next client is locked.

Definition at line 176 of file ClientTable-sav.cpp.

Here is the call graph for this function:

Here is the caller graph for this function:

int forest::ClientTable::nextSession ( int  sess,
int  clx 
) const
inline

Get the next session index for a client.

This method is used to iterate through the active sessions of a client. The order of the sessions indices is arbitrary. The caller should already hold a lock for the client.

Parameters
sessis a session index
clxis a client index
Returns
the next session index following sess in the list for clx, or 0 if there is no next index

Definition at line 214 of file ClientTable.h.

Here is the caller graph for this function:

bool forest::ClientTable::read ( istream &  in)

Read a client entry from a NetBuffer.

The entry must be on a line by itself.
An entry consists of a client name, password, real name (in quotes),
an email address and a default rate spec.
Parameters
bufis a NetBuffer that contains a valid client table entry
clxspecifies the client number for this entry
Returns
clx on success, 0 if the buf does not contain a valid entry and -1 on some other error int ClientTable::readRecord(NetBuffer& buf, int clx) { string cname, pwd, privString, realName, email; RateSpec defRates, totalRates;

if (!buf.skipSpaceInLine(in)) return false; if (!buf.readName(cname) || !buf.verify(',') || !buf.readWord(pwd) || !buf.verify(',') || !buf.readWord(privString) || !buf.verify(',') || !buf.readString(realName) || !buf.verify(',') || !buf.readWord(email) || !buf.verify(',') || !buf.readRspec(defRates) || !buf.verify(',') || !buf.readRspec(totalRates)) { return 0; } buf.nextLine();

privileges priv; if (privString == "limited") priv = LIMITED; else if (privString == "standard") priv = STANDARD; else if (privString == "admin") priv = ADMIN; else if (privString == "root") priv = ROOT; else priv = NUL_PRIV;

if (addClient(cname, pwd, priv, clx) == 0) return -1; setRealName(clx,realName); setEmail(clx,email); getDefRates(clx) = defRates; getTotalRates(clx) = totalRates; getAvailRates(clx) = totalRates; releaseClient(clx); return 1; }Read client table entries from an input stream. The first line of the input must contain an integer, giving the number of entries to be read. The input may include blank lines and comment lines (any text starting with '#'). The operation may fail if the input is misformatted or if an entry does not pass some basic sanity checks. In this case, an error message is printed that identifies the entry on which a problem was detected

Parameters
inis an open input stream
Returns
true on success, false on failure

Definition at line 392 of file ClientTable.cpp.

Here is the call graph for this function:

Here is the caller graph for this function:

bool forest::ClientTable::readEntry ( istream &  in,
int  clx = 0 
)

Read a client record from an input file and initialize its table entry.

A record includes a client name, password, real name (in quotes), an email address, a default rate spec and a total rate spec.

Parameters
inis an open file stream
clxis an optional argument specifying the client index for this entry; if zero, a client index is selected automatically (this is the default behavior)

Definition at line 300 of file ClientTable.cpp.

Here is the call graph for this function:

Here is the caller graph for this function:

void forest::ClientTable::releaseClient ( int  clx)

Release a previously locked client table entry.

Waiting threads are signalled to let them proceed.

Parameters
clxis the index of a locked comtree.

Definition at line 115 of file ClientTable-sav.cpp.

Here is the call graph for this function:

Here is the caller graph for this function:

void forest::ClientTable::removeClient ( int  clx)

Remove a client.

Assumes that the calling thread has already locked the client. The lock is released on return.

Parameters
clxis the index of the client to be deleted

Definition at line 247 of file ClientTable-sav.cpp.

Here is the call graph for this function:

void forest::ClientTable::removeSession ( int  sess)

Remove a session.

Assumes that calling thread holds a lock on the client for this session.

Parameters
sessis the index of the session to be deleted

Definition at line 289 of file ClientTable-sav.cpp.

Here is the call graph for this function:

Here is the caller graph for this function:

string & forest::ClientTable::session2string ( int  sess,
string &  s 
) const

Construct a string representation of a session.

Does no locking.

Parameters
sessis the client index of the session to be written
sis a reference to a string in which result is returned
Returns
a reference to s

Definition at line 466 of file ClientTable-sav.cpp.

Here is the call graph for this function:

Here is the caller graph for this function:

void forest::ClientTable::setClientIp ( int  sess,
ipa_t  ipa 
)
inline

Set the client address for a session.

Parameters
sessis a session index inline void ClientTable::setClientAdr(int sess, fAdr_t cadr) { svec[sess].cliAdr = cadr; }Set the client IP address for a session.
sessis a session index

Definition at line 399 of file ClientTable.h.

Here is the caller graph for this function:

void forest::ClientTable::setClientName ( int  clx,
const string &  cname 
)
inline

Set a client's name.

Parameters
clxis a valid client index
Returns
a const reference to the client name string

Definition at line 353 of file ClientTable.h.

Here is the caller graph for this function:

void forest::ClientTable::setEmail ( int  clx,
const string &  email 
)
inline

Set a client's email address.

Parameters
clxis a valid client index
emailis the client's new email address

Definition at line 377 of file ClientTable.h.

Here is the caller graph for this function:

void forest::ClientTable::setPassword ( int  clx,
const string &  pwd 
)
inline

Set a client's password.

Parameters
clxis a valid client index
Returns
a const reference to the password string

Definition at line 361 of file ClientTable.h.

Here is the caller graph for this function:

void forest::ClientTable::setPrivileges ( int  clx,
ClientTable::privileges  priv 
)
inline

Set a client's privileges.

Parameters
clxis a valid client index
privis the client's new privileges

Definition at line 385 of file ClientTable.h.

Here is the caller graph for this function:

void forest::ClientTable::setRealName ( int  clx,
const string &  realName 
)
inline

Set a client's real world name.

Parameters
clxis a valid client index
Returns
a const reference to the client's realName string

Definition at line 369 of file ClientTable.h.

Here is the caller graph for this function:

void forest::ClientTable::setRouterAdr ( int  sess,
fAdr_t  radr 
)
inline

Set the address of the client's router for a session.

Parameters
sessis a session index

Definition at line 406 of file ClientTable.h.

Here is the caller graph for this function:

void forest::ClientTable::setStartTime ( int  sess,
time_t  t 
)
inline

Set the start time for a session.

Parameters
sessis a session index

Definition at line 420 of file ClientTable.h.

Here is the caller graph for this function:

void forest::ClientTable::setState ( int  sess,
sessionState  state 
)
inline

Set the state for a session.

Parameters
sessis a session index

Definition at line 413 of file ClientTable.h.

Here is the caller graph for this function:

string & forest::ClientTable::toString ( string &  s,
bool  includeSess = false 
)

Create a string representation of the client table Does no locking.

Parameters
sis a reference to a string in which the result is returned
includeSessis true if sessions are to be included, default=false
Returns
a reference to s

Definition at line 483 of file ClientTable-sav.cpp.

Here is the call graph for this function:

void forest::ClientTable::write ( ostream &  out,
bool  includeSess = false 
)

Write the complete client table to an output stream.

Does no locking.

Parameters
outis a reference to an open output stream
includeSessis true if sessions are to be included, default=false

Definition at line 495 of file ClientTable-sav.cpp.

Here is the call graph for this function:

Member Data Documentation

pthread_mutex_t forest::ClientTable::mapLock
private

must hold during add/remove ops

and while locking client

Definition at line 154 of file ClientTable.h.


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