00001 #ifndef _STR_SimpleTelnet_H_ // -*-c++-*-
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #ifdef __GNUG__
00022 # pragma interface
00023 #endif
00024
00025
00026
00027
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
00038
00039
00040
00041
00042
00043
00044
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
00065
00066
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
00076 bool doProtocol();
00077
00078
00079 void updatePlugFlags();
00080
00081
00082 const StrChunkPtr userRead();
00083
00084
00085
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
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 };
00234 };
00235
00236 #endif