/* * The Unix Channel * * by Michel Beaudouin-Lafon * * Copyright 1990-1993 * Laboratoire de Recherche en Informatique (LRI) * * Servers * * $Id$ * $CurLog$ */ #include "Server.h" /*?class UchServer The class \typ{UchServer} derives from \typ{UchStream}. It is associated to a channel set to monitor the connected clients in parallel. Whenever a new client connects to the server, the virtual function \fun{CreateClient} is called. This function must return a pointer to an object of class \typ{UchClient} (or a derived class of \typ{UchClient}). Because the server and the clients may be on different machines with different architectures, the byte swapping is always done by the server, by convention. (NOTE: byte swapping is not currently implemented) ?*/ /*?class UchClient Class \typ{UchClient} derives from \typ{UchMsgStream}. It is still an abstract class because the virtual functions \fun{NewMessage} and \fun{ConvertAnswer} of \typ{UchMsgStream} are not defined in \typ{UchClient}. Instances of \fun{UchClient} only know that they may belong to an instance of \typ{UchServer}. Thus the virtual function \fun{Delete} is redefined to achieve a clean removal from the server's list. ?*/ // server ports // constructors, // destructor // copy // /*?nodoc?*/ UchServer :: UchServer (const UchServer& sp) : UchStream (sp), Clients () { } /*? Construct a server port bound to address \var{a}. When using Internet domain addresses, \var{a} should refer to the wildcard address so that clients can connect from any machine. This can be done with the instruction \com{new UchInetAddress(ANYADDR)}. ?*/ UchServer :: UchServer (UchBaseMultiplexer& m, UchAddress* a) : UchStream (a, 0), Clients () { if (Listen () < 0) { SysError (ErrWarn, "UchServer::UchServer"); } else { SetMode (IORead); m.Add (this); } } /*?nodoc?*/ UchServer :: ~UchServer () { UchClient* cl = 0; while (cl = Clients.First ()) cl->Delete (); } /*?nodoc?*/ UchChannel* UchServer :: Copy () const { return new UchServer (*this); } /*? Remove a client from the set of clients connected to the server. This is normally done automatically whenever the client disconnects itself, as a side effect of destroying the client. ?*/ void UchServer :: RemoveClient (UchClient* cl) { if (Clients.Remove (cl)) { cl->MyServer = 0; HandleRemove (cl); } else Error (ErrWarn, "UchServer::RemoveClient", "not owned by this server"); } /*? This function removes the server from its channel set. This has the effect of ignoring any incoming connections. This function will also make \fun{Run} exit, if there is no other channel in the channel set of the server. Unless you added your own channels to this channel set, this will be the case if there is no client currently connected. This is the only way to exit properly from \fun{Run}. ?*/ void UchServer :: Unlisten () { if (Mpx) Mpx->Remove (*this); } // handle connections on server port // /*?nodoc?*/ void UchServer :: HandleRead () { int fd = Accept (); if (fd < 0) { SysError (ErrWarn, "UchServer::HandleRead: Accept"); return; } UchClient* cl = CreateClient (fd); Clients.Append (cl); if (Mpx) Mpx->Add (cl); } // Default virtual function for handling new connections // If a class MY_CLIENT is derived from UchClient, // a class MY_SERVER must be derived from UchServer, // with MY_SERVER::CreateClient // doing at least a return new MY_CLIENT (fd) // /*? This virtual function is called whenever a new client connects to the server. The default action is to return \com{new UchClient(ch)}. This needs to be redefined only if you create a derived class of \typ{UchClient}, say \typ{MY\_CLIENT}, in which case it should be redefined in a derived class of \typ{UchServer} to return \com{new MY\_CLIENT(ch)}. ?*/ UchClient* UchServer :: CreateClient (int fd) { return new UchClient (*this, fd); } /*? This virtual function is called whenever a client is removed (\fun{RemoveClient}). It is a hook for the application to take whatever action; the default action is to do nothing. When this function is called, the client is already removed from the client list of the server. ?*/ void UchServer :: HandleRemove (UchClient*) { // nothing } // default error functions // /*?nextdoc?*/ bool UchServer :: SysError (errtype how, const char* who, int excl1, int excl2) { return ::SysError (how, who, excl1, excl2); } /*? Each server port class can have its own error handling routines. Their default action is to call the global functions \fun{Error} and \fun{SysError}. They can be called from the following functions: \fun{Error} can be called from \fun{RemoveClient} when the client does not belong to this server; \fun{SysError} can be called from \fun{Setup} when the socket could not be setup correctly, and from \fun{HandleRead} when the connection could not be accepted. ?*/ void UchServer :: Error (errtype how, const char* who, const char* what) { ::Error (how, who, what); } /*? Send a message to all clients currently connected to this server. If \var{flush} is true, the output buffer of each client is flushed. ?*/ void UchServer :: Broadcast (UchMessage& msg, bool flush) { #ifndef CPLUS_BUG19 CcuListIterOf li (Clients); while (++li) (*li)->Send (msg, flush); #else CcuListIter li (Clients); while (++li) ((UchClient*)(*li))->Send (msg, flush); #endif } /*? Send a message to all clients currently connected to this server, except \var{exclude}. If \var{flush} is true, the output buffer of each client is flushed. ?*/ void UchServer :: Broadcast (UchMessage& msg, UchClient* excl, bool flush) { #ifndef CPLUS_BUG19 CcuListIterOf li (Clients); while (++li) if (*li != excl) (*li)->Send (msg, flush); #else CcuListIter li (Clients); while (++li) { UchClient* a = (UchClient*)(*li); if (a != excl) a->Send (msg, flush); } #endif } /*?nextdoc?*/ void UchServer :: Broadcast (UchMsgBuffer& buf, bool flush) { #ifndef CPLUS_BUG19 CcuListIterOf li (Clients); while (++li) (*li)->Send (buf, flush); #else CcuListIter li (Clients); while (++li) ((UchClient*)(*li))->Send (buf, flush); #endif } /*? These functions are similar to the previous ones, except that they take a buffer instead of a message. The buffer {\em must} contain a converted message. It is more efficient to broadcast a buffer than a message because there is less message conversion overhead. ?*/ void UchServer :: Broadcast (UchMsgBuffer& buf, UchClient* excl, bool flush) { #ifndef CPLUS_BUG19 CcuListIterOf li (Clients); while (++li) if (*li != excl) (*li)->Send (buf, flush); #else CcuListIter li (Clients); while (++li) { UchClient* a = (UchClient*)(*li); if (a != excl) a->Send (buf, flush); } #endif } /*?nodoc?*/ UchClient :: UchClient (const UchClient& cl) : UchMsgStream (cl), MyServer (0) { } /*? Construct a new client connected to the server on file descriptor fd. ?*/ UchClient :: UchClient (UchServer& s, int fd) : UchMsgStream (), MyServer (&s) { SetMode (IOReadSelect); UchChannel::Open (fd); } /*?nodoc?*/ UchClient :: ~UchClient () { if (MyServer) { UchServer* s = MyServer; s->Error (ErrWarn, "~UchClient", "client still in a server; deleting anyway ...\n"); s->RemoveClient (this); if (s->Mpx) s->Mpx->Remove (*this); // calls the destructor if no more refs } } /*?nodoc?*/ UchChannel* UchClient :: Copy () const { return new UchClient (*this); } /*? This function must be used to delete a client explicitly. It is not safe to use the operator delete. This function is called when an end of file is read from the client; this means that you usually do not need to call it. ?*/ void UchClient :: Delete () { if (MyServer) { UchServer* s = MyServer; s->RemoveClient (this); if (s->Mpx) s->Mpx->Remove (*this); // calls the destructor if no more refs } } #ifdef DOC /*? Return the server corresponding to a given client. ?*/ UchServer* UchClient :: GetServer () { } #endif /* DOC */