forest-net
an overlay networks for large-scale virtual worlds
|
Class that implements a table of information about users. More...
#include <ClientTable.h>
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 RateSpec & | getDefRates () const |
Get the intial default rate spec for new clients. More... | |
const RateSpec & | getTotalRates () 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... | |
RateSpec & | getDefRates (int) const |
Get the default rate spec for a client. More... | |
RateSpec & | getTotalRates (int) const |
Get the total rate spec for a client. More... | |
RateSpec & | getAvailRates (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... | |
RateSpec & | getSessRates (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 | |
Session * | svec |
vector of session structs | |
UiClist * | sessLists |
circular lists of session indexes | |
RateSpec | defRates |
initial default rates | |
RateSpec | totalRates |
initial total rates | |
Client * | cvec |
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 | |
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.
forest::ClientTable::ClientTable | ( | int | maxClients, |
int | maxSessions | ||
) |
Constructor for ClientTable, allocates space and initializes table.
Definition at line 15 of file ClientTable-sav.cpp.
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.
cname | is the client name |
pwd | is the client password |
priv | is the assigned privilege level |
clx | is 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) |
Definition at line 217 of file ClientTable-sav.cpp.
Add a new session.
Attempts to add a new session to an existing client.
cliAdr | is the forest address assigned to client |
rtrAdr | is the forest address of the router assigned to client |
clx | is the client index of a valid client for which the caller is assumed to hold a lock |
Definition at line 267 of file ClientTable-sav.cpp.
|
inline |
Get a client's password.
clx | is a valid client index |
pwd | is a reference to a string containing the client's password |
Definition at line 249 of file ClientTable.h.
string & forest::ClientTable::client2string | ( | int | clx, |
string & | s, | ||
bool | includeSess = false |
||
) | const |
Construct a string representation of a client.
This metghod does no locking.
clx | is the client index of the client to be written |
s | is a reference to a string in which result is returned |
includeSess | is true if sessions are to be included, default=false |
Definition at line 435 of file ClientTable-sav.cpp.
int forest::ClientTable::firstClient | ( | ) |
Get the first client in the list of valid clients.
Definition at line 152 of file ClientTable-sav.cpp.
|
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.
clx | is a client index |
Definition at line 201 of file ClientTable.h.
|
inline |
Get the available rate spec for a client.
clx | is a client index |
Definition at line 289 of file ClientTable.h.
int forest::ClientTable::getClient | ( | const string & | cname) |
Lock a client's table entry.
clx | is a client index |
cname | is a client name |
Definition at line 93 of file ClientTable-sav.cpp.
|
inline |
Get the client address for a session.
sess | is a session index |
Definition at line 305 of file ClientTable.h.
|
inline |
Get the index of a client for a given session.
sess | is the session index |
Definition at line 321 of file ClientTable.h.
|
inline |
Get the client address for a session.
sess | is a session index |
Definition at line 313 of file ClientTable.h.
|
inline |
Get a client's name.
clx | is a valid client index |
Definition at line 232 of file ClientTable.h.
|
inline |
Get the intial default rate spec for new clients.
Definition at line 178 of file ClientTable.h.
|
inline |
Get the default rate spec for a client.
clx | is a client index |
Definition at line 281 of file ClientTable.h.
|
inline |
Get a client's email address.
clx | is a valid client index |
Definition at line 265 of file ClientTable.h.
|
inline |
Get the number of sessions for a client.
clx | is a valid client index |
Definition at line 240 of file ClientTable.h.
|
inline |
Get a client's password.
The caller should already hold a lock for the client.
clx | is a valid client index |
Definition at line 224 of file ClientTable.h.
|
inline |
Get a client's privileges.
clx | is a valid client index |
Definition at line 273 of file ClientTable.h.
|
inline |
Get a client's real world name.
clx | is a valid client index |
Definition at line 257 of file ClientTable.h.
|
inline |
Get the address of the client's router for a session.
sess | is a session index |
Definition at line 329 of file ClientTable.h.
int forest::ClientTable::getSession | ( | fAdr_t | cliAdr) |
Get a session by its forest address and lock its client's table entry.
cliAdr | is a forest address |
Definition at line 129 of file ClientTable-sav.cpp.
|
inline |
Get the rate spec for a session.
sess | is a session index |
Definition at line 345 of file ClientTable.h.
|
inline |
Get the address of the client's router for a session.
sess | is a session index |
Definition at line 337 of file ClientTable.h.
|
inline |
Get the intial total rate spec for new clients.
Definition at line 185 of file ClientTable.h.
|
inline |
Get the total rate spec for a client.
clx | is a client index |
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.
|
inlineprivate |
helper functions
Compute key for use with sessMap.
cliAdr | is a client address |
Definition at line 428 of file ClientTable.h.
|
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.
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.
clx | is the index of a valid client |
Definition at line 176 of file ClientTable-sav.cpp.
|
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.
sess | is a session index |
clx | is a client index |
Definition at line 214 of file ClientTable.h.
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.
buf | is a NetBuffer that contains a valid client table entry |
clx | specifies the client number for this entry |
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
in | is an open input stream |
Definition at line 392 of file ClientTable.cpp.
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.
in | is an open file stream |
clx | is 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.
void forest::ClientTable::releaseClient | ( | int | clx) |
Release a previously locked client table entry.
Waiting threads are signalled to let them proceed.
clx | is the index of a locked comtree. |
Definition at line 115 of file ClientTable-sav.cpp.
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.
clx | is the index of the client to be deleted |
Definition at line 247 of file ClientTable-sav.cpp.
void forest::ClientTable::removeSession | ( | int | sess) |
Remove a session.
Assumes that calling thread holds a lock on the client for this session.
sess | is the index of the session to be deleted |
Definition at line 289 of file ClientTable-sav.cpp.
string & forest::ClientTable::session2string | ( | int | sess, |
string & | s | ||
) | const |
Construct a string representation of a session.
Does no locking.
sess | is the client index of the session to be written |
s | is a reference to a string in which result is returned |
Definition at line 466 of file ClientTable-sav.cpp.
|
inline |
Set the client address for a session.
sess | is a session index inline void ClientTable::setClientAdr(int sess, fAdr_t cadr) { svec[sess].cliAdr = cadr; }Set the client IP address for a session. |
sess | is a session index |
Definition at line 399 of file ClientTable.h.
|
inline |
Set a client's name.
clx | is a valid client index |
Definition at line 353 of file ClientTable.h.
|
inline |
Set a client's email address.
clx | is a valid client index |
is the client's new email address |
Definition at line 377 of file ClientTable.h.
|
inline |
Set a client's password.
clx | is a valid client index |
Definition at line 361 of file ClientTable.h.
|
inline |
Set a client's privileges.
clx | is a valid client index |
priv | is the client's new privileges |
Definition at line 385 of file ClientTable.h.
|
inline |
Set a client's real world name.
clx | is a valid client index |
Definition at line 369 of file ClientTable.h.
|
inline |
Set the address of the client's router for a session.
sess | is a session index |
Definition at line 406 of file ClientTable.h.
|
inline |
Set the start time for a session.
sess | is a session index |
Definition at line 420 of file ClientTable.h.
|
inline |
Set the state for a session.
sess | is a session index |
Definition at line 413 of file ClientTable.h.
string & forest::ClientTable::toString | ( | string & | s, |
bool | includeSess = false |
||
) |
Create a string representation of the client table Does no locking.
s | is a reference to a string in which the result is returned |
includeSess | is true if sessions are to be included, default=false |
Definition at line 483 of file ClientTable-sav.cpp.
void forest::ClientTable::write | ( | ostream & | out, |
bool | includeSess = false |
||
) |
Write the complete client table to an output stream.
Does no locking.
out | is a reference to an open output stream |
includeSess | is true if sessions are to be included, default=false |
Definition at line 495 of file ClientTable-sav.cpp.
|
private |
must hold during add/remove ops
and while locking client
Definition at line 154 of file ClientTable.h.