Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  

SimpleTelnetClient.h

00001 #ifndef _STR_SimpleTelnet_H_  // -*-c++-*-
00002 
00003 /*
00004  * Copyright 1991-2002 Eric M. Hopper <hopper@omnifarious.org>
00005  * 
00006  *     This program is free software; you can redistribute it and/or modify it
00007  *     under the terms of the GNU Lesser General Public License as published
00008  *     by the Free Software Foundation; either version 2 of the License, or
00009  *     (at your option) any later version.
00010  * 
00011  *     This program is distributed in the hope that it will be useful, but
00012  *     WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  *     Lesser General Public License for more details.
00015  * 
00016  *     You should have received a copy of the GNU Lesser General Public
00017  *     License along with this program; if not, write to the Free Software
00018  *     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00019  */
00020 
00021 #ifdef __GNUG__
00022 #  pragma interface
00023 #endif
00024 
00025 /* $Header: /home/hopper/src/cvs/C++/StrMod/StrMod/SimpleTelnetClient.h,v 1.4 2002/08/29 00:58:04 hopper Exp $ */
00026 
00027 // For a log, see ../ChangeLog
00028 
00029 #include <StrMod/StreamModule.h>
00030 #include <StrMod/STR_ClassIdent.h>
00031 
00032 #define _STR_SimpleTelnet_H_
00033 
00034 namespace strmod {
00035 namespace strmod {
00036 
00037 /** \class SimpleTelnetClient SimpleTelnetClient.h StrMod/SimpleTelnetClient.h
00038  * \brief Only will handle the case when the server will do supress go ahead,
00039  * and echo.
00040  *
00041  * This is a very simple telnet protocol handler.  It won't support ANY options
00042  * besides the server doing supress go ahead, and echo, and it requires the
00043  * server to support and enable those options.  This means that it will only do
00044  * 'character at a time' mode.
00045  */
00046 class SimpleTelnetClient : public StreamModule {
00047    class UPlug;
00048    class SPlug;
00049    friend class UPlug;
00050    friend class SPlug;
00051  public:
00052    enum Sides { ToServer, ToUser };
00053    static const STR_ClassIdent identifier;
00054 
00055    SimpleTelnetClient();
00056    ~SimpleTelnetClient();
00057 
00058    inline virtual int AreYouA(const lcore::ClassIdent &cid) const;
00059 
00060    inline virtual bool canCreate(int side) const;
00061    inline virtual bool ownsPlug(const Plug *plug) const;
00062    virtual bool deletePlug(Plug *plug);
00063 
00064    /** Resets the states of the sent_do_supga_ and sent_do_echo_ flags.
00065     * Resetting these flags has the effect of causing the client to attempt to
00066     * renegotiate these options
00067     */
00068    void reset();
00069 
00070  protected:
00071    virtual const lcore::ClassIdent *i_GetIdent() const  { return &identifier; }
00072 
00073    virtual Plug *i_MakePlug(int side);
00074 
00075    //: Handles sending telnet protocol messages for initial negotiation
00076    bool doProtocol();
00077    //: Checks the state of things and updates all the readable and writeable
00078    //: flags.
00079    void updatePlugFlags();
00080 
00081    //: Called when a read is done from the user plug.
00082    const StrChunkPtr userRead();
00083 
00084    //: Called when a write is done on the server plug.
00085    // <p>This handles properly replying to the messages the server sends.</p>
00086    void serverWrite(const StrChunkPtr &ptr);
00087 
00088  private:
00089    class UPlug : public Plug {
00090     public:
00091       friend class SimpleTelnetClient;
00092                 friend class SPlug;
00093 
00094       static const STR_ClassIdent identifier;
00095 
00096       UPlug(SimpleTelnetClient &parent) : Plug(parent)  { }
00097       ~UPlug()                                          { }
00098 
00099       inline virtual int AreYouA(const lcore::ClassIdent &cid) const;
00100 
00101       inline SimpleTelnetClient &getParent() const;
00102       virtual int side() const                          { return(ToUser); }
00103 
00104     protected:
00105       virtual const lcore::ClassIdent *i_GetIdent() const {
00106          return &identifier;
00107       }
00108 
00109       virtual bool needsNotifyReadable() const          { return(true); }
00110       inline virtual void otherIsReadable();
00111 
00112       inline virtual const StrChunkPtr i_Read();
00113       inline virtual void i_Write(const StrChunkPtr &chnk);
00114 
00115       inline bool canReadOther() const;
00116    };
00117 
00118    class SPlug : public Plug {
00119     public:
00120       friend class SimpleTelnetClient;
00121                 friend class UPlug;
00122 
00123       static const STR_ClassIdent identifier;
00124 
00125       SPlug(SimpleTelnetClient &parent) : Plug(parent)  { }
00126       ~SPlug()                                          { }
00127 
00128       inline virtual int AreYouA(const lcore::ClassIdent &cid) const;
00129 
00130       inline SimpleTelnetClient &getParent() const;
00131       virtual int side() const                          { return(ToServer); }
00132 
00133     protected:
00134       virtual const lcore::ClassIdent *i_GetIdent() const {
00135          return &identifier;
00136       }
00137 
00138       virtual bool needsNotifyWriteable() const         { return(true); }
00139       inline virtual void otherIsWriteable();
00140 
00141       inline virtual const StrChunkPtr i_Read();
00142       inline virtual void i_Write(const StrChunkPtr &chnk);
00143 
00144       inline bool canWriteOther() const;
00145    };
00146 
00147  private:
00148    bool sent_do_supga_;
00149    bool sent_do_echo_;
00150    StrChunkPtr toserver_;
00151    StrChunkPtr touser_;
00152    bool uplugcreated_;
00153    UPlug userplug_;
00154    bool splugcreated_;
00155    SPlug serverplug_;
00156 };
00157 
00158 //------------------------------inline function--------------------------------
00159 
00160 inline int SimpleTelnetClient::AreYouA(const lcore::ClassIdent &cid) const
00161 {
00162    return((cid == identifier) || StreamModule::AreYouA(cid));
00163 }
00164 
00165 inline bool SimpleTelnetClient::canCreate(int side) const
00166 {
00167    return(((side == ToServer) && !splugcreated_) ||
00168           ((side == ToUser)   && !uplugcreated_));
00169 }
00170 
00171 inline  bool SimpleTelnetClient::ownsPlug(const Plug *plug) const
00172 {
00173    return((splugcreated_ && (plug == &serverplug_)) ||
00174           (uplugcreated_ && (plug == &userplug_)));
00175 }
00176 
00177 //--
00178 
00179 inline int SimpleTelnetClient::UPlug::AreYouA(const lcore::ClassIdent &cid) const
00180 {
00181    return((identifier == cid) || Plug::AreYouA(cid));
00182 }
00183 
00184 inline SimpleTelnetClient &SimpleTelnetClient::UPlug::getParent() const
00185 {
00186    return(static_cast<SimpleTelnetClient &>(Plug::getParent()));
00187 }
00188 
00189 inline void SimpleTelnetClient::UPlug::otherIsReadable()
00190 {
00191    getParent().updatePlugFlags();
00192 }
00193 
00194 inline const StrChunkPtr SimpleTelnetClient::UPlug::i_Read()
00195 {
00196    return(getParent().userRead());
00197 }
00198 
00199 inline bool SimpleTelnetClient::UPlug::canReadOther() const
00200 {
00201    Plug *other = pluggedInto();
00202    return((other == NULL) ? false : getFlagsFrom(*other).canread_);
00203 }
00204 
00205 //--
00206 
00207 inline int SimpleTelnetClient::SPlug::AreYouA(const lcore::ClassIdent &cid) const
00208 {
00209    return((identifier == cid) || Plug::AreYouA(cid));
00210 }
00211 
00212 inline SimpleTelnetClient &SimpleTelnetClient::SPlug::getParent() const
00213 {
00214    return(static_cast<SimpleTelnetClient &>(Plug::getParent()));
00215 }
00216 
00217 inline void SimpleTelnetClient::SPlug::otherIsWriteable()
00218 {
00219    getParent().updatePlugFlags();
00220 }
00221 
00222 inline void SimpleTelnetClient::SPlug::i_Write(const StrChunkPtr &chnk)
00223 {
00224    getParent().serverWrite(chnk);
00225 }
00226 
00227 inline bool SimpleTelnetClient::SPlug::canWriteOther() const
00228 {
00229    Plug *other = pluggedInto();
00230    return((other == NULL) ? false : getFlagsFrom(*other).canwrite_);
00231 }
00232 
00233 };  // namespace strmod
00234 };  // namespace strmod
00235 
00236 #endif

Generated on Wed Jan 29 00:32:44 2003 for libNet by doxygen1.3-rc1