00001 #ifndef NETWORK_HAS_BEEN_INCLUDED
00002 #define NETWORK_HAS_BEEN_INCLUDED
00003
00004 #ifdef WIN32
00005 #include <windows.h>
00006 #include <winsock.h>
00007 #include <io.h>
00008 #else
00009 #include <sys/types.h>
00010 #include <sys/socket.h>
00011 #include <netinet/in.h>
00012 #include <arpa/inet.h>
00013 #include <netdb.h>
00014 #endif
00015
00016 #include "std/config.H"
00017 #include "tty/tty.H"
00018 #include "net_stream.H"
00019
00020 class Network_obs
00021 {
00022 public:
00023 enum event {accept_str=0, remove_str};
00024
00025 virtual ~Network_obs() {}
00026 Network_obs() {}
00027
00028 virtual void notify_net( const event &e, NetStream *s ) = 0;
00029 };
00030
00032
00033 class DllImpExp Network: public FD_EVENT {
00034 friend class NetStream;
00035 static int NETWORK_SERVER_BACKLOG;
00036
00037 protected:
00038 int nStreams_;
00039 NetStream *streams_[256];
00040 str_list tags_;
00041 int port_;
00042 str_ptr name_;
00043 bool first_;
00044 int _at_barrier;
00045
00046 ARRAY<Network_obs *> _obs_list;
00047
00048 void die(const char *msg);
00049
00050 NetStream *wait_for_connect();
00051 char *configure (int port, int backlog = NETWORK_SERVER_BACKLOG);
00052 void accept_stream(void);
00053 void remove_stream(NetStream *s);
00054
00055
00056 void sample () { accept_stream(); }
00057
00058
00059
00060 virtual void add_client(NetStream * ) { }
00061 virtual void interpret (NETenum , NetStream * ) { }
00062 virtual NetStream *new_stream(int port, const char *host)
00063 { return new NetStream(port,host);}
00064 virtual NetStream *new_stream(int fd, struct sockaddr_in *a)
00065 { return new NetStream(fd,a); }
00066
00067
00068 public:
00069 Network() : nStreams_(0),name_(""), first_(0), _at_barrier(0) { }
00070 virtual ~Network() { cerr << "removing network" << endl;
00071 FD_MANAGER::rem_event(this); }
00072
00073 void connect_to (NetStream *s);
00074 void add_stream (NetStream *s) { streams_[nStreams_++] = s;
00075 s->add_network(this);
00076 s->subscribe(tags_); }
00077 void start ( int port=0);
00078 void start (char *host,int port) { start();
00079 connect_to(new_stream(port,host));}
00080 void send_stream(ostrstream &);
00081 void flush_data ();
00082
00083 void subscribe (Cstr_ptr &tag) { tags_ += tag;
00084 for (int i=0; i < nStreams_; i++)
00085 streams_[i]->subscribe(tag); }
00086 NetStream *operator[] (int i) { return streams_[i]; }
00087 int num_streams(void) const { return nStreams_; }
00088 int port (void) const { return port_; }
00089 Cstr_ptr &name (void) const { return name_; }
00090 int processing (void) const;
00091
00092 void barrier ();
00093 void add_net_obs(Network_obs *o) { _obs_list += o; }
00094 void rem_net_obs(Network_obs *o) { _obs_list -= o; }
00095 void notify_net (Network_obs::event e,
00096 NetStream *s) { for(int i=0;i<_obs_list.num();i++)
00097 _obs_list[i]->notify_net(e,s); }
00098 };
00099
00100
00101 #ifdef WIN32
00102 ssize_t write_win32(int fildes, const void *buf, size_t nbyte);
00103 ssize_t read_win32 (int fildes, void *buf, size_t nbyte);
00104 DllImpExp int num_bytes_to_read(int fildes);
00105 #else
00106 int num_bytes_to_read(int fildes);
00107 #endif
00108
00109 #endif