# mstate.h

    00001 //
    00002 // mstate.h
    00003 //
    00004 // Copyright (C) 1996 Limit Point Systems, Inc.
    00005 //
    00006 // Author: Curtis Janssen <cljanss@limitpt.com>
    00007 // Maintainer: LPS
    00008 //
    00009 // This file is part of the SC Toolkit.
    00010 //
    00011 // The SC Toolkit is free software; you can redistribute it and/or modify
    00012 // it under the terms of the GNU Library General Public License as published by
    00013 // the Free Software Foundation; either version 2, or (at your option)
    00014 // any later version.
    00015 //
    00016 // The SC Toolkit is distributed in the hope that it will be useful,
    00017 // but WITHOUT ANY WARRANTY; without even the implied warranty of
    00018 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    00019 // GNU Library General Public License for more details.
    00020 //
    00021 // You should have received a copy of the GNU Library General Public License
    00022 // along with the SC Toolkit; see the file COPYING.LIB.  If not, write to
    00023 // the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
    00024 //
    00025 // The U.S. Government is granted a limited license as per AL 91-7.
    00026 //
    00027
    00028 #ifdef __GNUC__
    00029 #pragma interface
    00030 #endif
    00031
    00032 #ifndef _util_group_mstate_h
    00033 #define _util_group_mstate_h
    00034
    00035 #include <util/state/state.h>
    00036 #include <util/state/statein.h>
    00037 #include <util/state/stateout.h>
    00038 #include <util/group/message.h>
    00039
    00040 namespace sc {
    00041
    00045 class MsgStateSend: public StateOut {
    00046   private:
    00047     // do not allow copy constructor or assignment
    00048     MsgStateSend(const MsgStateSend&);
    00049     void operator=(const MsgStateSend&);
    00050   protected:
    00051     Ref<MessageGrp> grp;
    00052     int nbuf; // the number of bytes used in the buffer
    00053     int bufsize; // the allocated size of the data buffer
    00054     char* buffer; // the data buffer
    00055     char* send_buffer; // the buffer used to send data (includes nbuf)
    00056     int nheader; // nbuf + nheader = the number of bytes in send_buffer to send
    00057     int* nbuf_buffer; // the pointer to the nbuf stored in the buffer
    00058
    00059     int put_array_void(const void*, int);
    00060   public:
    00061     MsgStateSend(const Ref<MessageGrp>&);
    00062     virtual ~MsgStateSend();
    00063
    00065     virtual void flush() = 0;
    00066
    00069     void set_buffer_size(int);
    00070
    00073     int put(const ClassDesc*);
    00074     int put(char r);
    00075     int put(unsigned int r);
    00076     int put(int r);
    00077     int put(float r);
    00078     int put(double r);
    00079     int put(const char*,int);
    00080     int put(const int*,int);
    00081     int put(const unsigned int*,int);
    00082     int put(const float*,int);
    00083     int put(const double*,int);
    00084 };
    00085
    00089 class MsgStateBufRecv: public StateIn {
    00090   private:
    00091     // do not allow copy constructor or assignment
    00092     MsgStateBufRecv(const MsgStateBufRecv&);
    00093     void operator=(const MsgStateBufRecv&);
    00094   protected:
    00095     Ref<MessageGrp> grp;
    00096     int nbuf; // the number of bytes used in the buffer
    00097     int ibuf; // the current pointer withing the buffer
    00098     int bufsize; // the allocated size of the buffer
    00099     char* buffer; // the data buffer
    00100     char* send_buffer; // the buffer used to send data (includes nbuf)
    00101     int nheader; // nbuf + nheader = the number of bytes in send_buffer to send
    00102     int* nbuf_buffer; // the pointer to the nbuf stored in the buffer
    00103
    00104     int get_array_void(void*,int);
    00105
    00107     virtual void next_buffer() = 0;
    00108   public:
    00110     MsgStateBufRecv(const Ref<MessageGrp>&);
    00112     MsgStateBufRecv();
    00113
    00114     virtual ~MsgStateBufRecv();
    00115
    00118     void set_buffer_size(int);
    00119 };
    00120
    00123 class MsgStateRecv: public MsgStateBufRecv {
    00124   private:
    00125     // do not allow copy constructor or assignment
    00126     MsgStateRecv(const MsgStateRecv&);
    00127     void operator=(const MsgStateRecv&);
    00128   public:
    00130     MsgStateRecv(const Ref<MessageGrp>&);
    00131
    00132     virtual ~MsgStateRecv();
    00133
    00137     int version(const ClassDesc*);
    00138
    00141     int get(const ClassDesc**);
    00142     int get(char&r, const char *key = 0);
    00143     int get(unsigned int&r, const char *key = 0);
    00144     int get(int&r, const char *key = 0);
    00145     int get(float&r, const char *key = 0);
    00146     int get(double&r, const char *key = 0);
    00147     int get(char*&);
    00148     int get(unsigned int*&);
    00149     int get(int*&);
    00150     int get(float*&);
    00151     int get(double*&);
    00152 };
    00153
    00157 class StateSend: public MsgStateSend {
    00158   private:
    00159     // do not allow copy constructor or assignment
    00160     StateSend(const StateSend&);
    00161     void operator=(const StateSend&);
    00162   private:
    00163     int target_;
    00164   public:
    00166     StateSend(const Ref<MessageGrp>&);
    00167
    00168     ~StateSend();
    00170     void target(int);
    00172     void flush();
    00173 };
    00174
    00178 class StateRecv: public MsgStateRecv {
    00179   private:
    00180     // do not allow copy constructor or assignment
    00181     StateRecv(const StateRecv&);
    00182     void operator=(const StateRecv&);
    00183   private:
    00184     int source_;
    00185   protected:
    00186     void next_buffer();
    00187   public:
    00189     StateRecv(const Ref<MessageGrp>&);
    00191     void source(int);
    00192 };
    00193
    00197 class BcastStateSend: public MsgStateSend {
    00198   private:
    00199     // do not allow copy constructor or assignment
    00200     BcastStateSend(const BcastStateSend&);
    00201     void operator=(const BcastStateSend&);
    00202   public:
    00204     BcastStateSend(const Ref<MessageGrp>&);
    00205
    00206     ~BcastStateSend();
    00208     void flush();
    00209 };
    00210
    00214 class BcastStateRecv: public MsgStateRecv {
    00215   private:
    00216     // do not allow copy constructor or assignment
    00217     BcastStateRecv(const BcastStateRecv&);
    00218     void operator=(const BcastStateRecv&);
    00219   protected:
    00220     int source_;
    00221     void next_buffer();
    00222   public:
    00224     BcastStateRecv(const Ref<MessageGrp>&, int source = 0);
    00226     void source(int s);
    00227 };
    00228
    00232 class BcastState {
    00233   private:
    00234     BcastStateRecv *recv_;
    00235     BcastStateSend *send_;
    00236   public:
    00238     BcastState(const Ref<MessageGrp> &, int source = 0);
    00239
    00240     ~BcastState();
    00241
    00245     void bcast(int &);
    00246     void bcast(double &);
    00247     void bcast(int *&, int);
    00248     void bcast(double *&, int);
    00249     template <class T> void bcast(Ref<T>&a)
    00250         {
    00251           if (recv_) {
    00252               a << SavableState::restore_state(*recv_);
    00253             }
    00254           else if (send_) {
    00255               SavableState::save_state(a.pointer(),*send_);
    00256             }
    00257         }
    00258
    00261     void flush();
    00262
    00265     void forget_references();
    00266
    00268     void set_buffer_size(int);
    00269 };
    00270
    00274 class BcastStateInBin: public MsgStateBufRecv {
    00275   private:
    00276     // do not allow copy constructor or assignment
    00277     BcastStateInBin(const BcastStateRecv&);
    00278     void operator=(const BcastStateRecv&);
    00279   protected:
    00280     int opened_;
    00281     int file_position_;
    00282     std::streambuf *buf_;
    00283
    00284     void next_buffer();
    00285     int get_array_void(void*, int);
    00286   public:
    00288     BcastStateInBin(const Ref<KeyVal> &);
    00290     BcastStateInBin(const Ref<MessageGrp>&, const char *filename);
    00291
    00292     ~BcastStateInBin();
    00293
    00294     virtual int open(const char *name);
    00295     virtual void close();
    00296
    00297     void seek(int loc);
    00298     int seekable();
    00299     int tell();
    00300     int use_directory();
    00301 };
    00302
    00303 }
    00304
    00305 #endif
    00306
    00307 // Local Variables:
    00308 // mode: c++
    00309 // c-file-style: "CLJ"
    00310 // End: