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

rtnetwork.h

Go to the documentation of this file.
00001 /*
00002  * rtnetwork.h
00003  * 
00004  * Copyright (c) 2000-2004 by Florian Fischer (florianfischer@gmx.de)
00005  * and Martin Trautmann (martintrautmann@gmx.de) 
00006  * 
00007  * This file may be distributed and/or modified under the terms of the 
00008  * GNU General Public License version 2 as published by the Free Software 
00009  * Foundation. 
00010  * 
00011  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00012  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00013  * 
00014  */
00015 
00016 #if !defined(__LRT_NETWORK__) && !defined(__SYMBIAN32__) // && !(defined(__WIN32) && !defined(__MSVC__))
00017 #define __LRT_NETWORK__
00018 #define __NETWORK__ // alternate macro
00019 
00028 #include "rtstreams.h"
00029 #include "rtmap.h"
00030 
00031 namespace lrt {
00032 
00033 class String;
00034 template<class T> class StringMap;
00035 class Time;
00036 
00037 class Socket;
00038 class ServerSocket;
00039 class NetworkInputStream;
00040 class NetworkOutputStream;
00041 
00043 class Network {
00044 public: // types
00046         enum HttpMethod { GET = 0, POST = 1, HEAD = 2 }; // change methodTable in network.cpp if this is changed!
00048         struct Page { 
00049                 Page() : headers(false), code(0), text() {} 
00050                 bool isError() { return (code / 100 != 2); } 
00051                 StringMap<String> headers; 
00052                 String text; 
00053                 int code; 
00054                 String message; 
00055         };
00056 public: // static
00061         static Socket* create(const String& server, int port);
00065         static ServerSocket* createServer(int port);
00066 
00068         static const Time& getTimeout();
00072         static void setTimeout(const Time&);
00073 
00076         static void splitURL(const String& url, String& proto, String& server, int& port, String& file);
00084         static bool httpRequest(const String& url, HttpMethod method, Page& page);
00086         static bool httpRequest(const String& server, int port, HttpMethod method, const String& file, Page& page);
00087 
00090         static String urlEncode(const StringMap<String>& data);
00092         static String urlEncode(const String& str);
00093 
00094 private:
00095         static Time timeout;
00096 };
00097 
00102 class Socket {
00103 public:
00104         virtual ~Socket(); 
00105 
00107         NetworkInputStream* getInput();
00109         NetworkOutputStream* getOutput();
00111         void close();
00113         bool isClosed();
00115         String getPeerName();
00116 
00117 protected:
00118         Socket(void* sock);
00119         friend class ServerSocket; // but why is this necessary?
00120         friend class Network;
00121         friend class NetworkInputStream;
00122         friend class NetworkOutputStream;
00123 
00124         void* sock;
00125         NetworkInputStream* in;
00126         NetworkOutputStream* out;
00127 };
00128 
00132 class ServerSocket : public lrt::Socket {
00133 public:
00134         ~ServerSocket();
00136         Socket* accept();
00137 
00138 protected:
00139         ServerSocket(void* sock);
00140         friend class Network;
00141 };
00142 
00144 class NetworkInputStream : public InputStream {
00145 public:
00146         ~NetworkInputStream();
00147 
00148         virtual int read();
00149         virtual int read(Array<char> &b, int off, int len);
00150         virtual bool eos();
00152         virtual void close();
00153 
00154 
00155 protected:
00156         NetworkInputStream(Socket* sock);
00157         friend class Socket; 
00158 
00159         Socket* sock;
00160 };
00161 
00163 class NetworkOutputStream: public OutputStream {
00164 public:
00165         virtual ~NetworkOutputStream();
00166 
00167         virtual bool write(int b);
00168         virtual bool write(const Array<char> &b, int off, int len);
00169         virtual bool write(const Array<char> &b);
00171         virtual void close();
00172 
00173 protected:
00174         NetworkOutputStream(Socket* sock);
00175         friend class Socket; 
00176 
00177         Socket* sock;
00178 };
00179 
00180 } // namespace
00181 
00182 #endif
00183 

This is the RoboTour API documentation version 0.1 • Copyright (C) by Florian Fischer