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

stream.cc

Go to the documentation of this file.
00001 /**************************************************************************
00002  Copyright (c) 2000-2001, Tony Garnock-Jones
00003  All rights reserved.
00004 
00005  Redistribution and use in source and binary forms, with or without
00006  modification, are permitted provided that the following conditions are
00007  met:
00008 
00009      * Redistributions of source code must retain the above copyright
00010        notice, this list of conditions and the following disclaimer.
00011 
00012      * Redistributions in binary form must reproduce the above
00013        copyright notice, this list of conditions and the following
00014        disclaimer in the documentation and/or other materials provided
00015        with the distribution.
00016 
00017      * Neither the names of the copyright holders nor the names of this
00018        software's contributors may be used to endorse or promote
00019        products derived from this software without specific prior
00020        written permission.
00021 
00022  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00023  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00024  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00025  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
00026  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00027  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00028  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00029  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00030  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00031  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00032  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00033 **************************************************************************/
00034 
00035 #include <stdlib.h>
00036 #include <stdio.h>
00037 #include <string.h>
00038 #include <assert.h>
00039 
00040 #include <mps/mps.h>
00041 
00042 #define WANT_PROTOCOL_DEBUG 0
00043 
00044 #if WANT_PROTOCOL_DEBUG
00045 #define TPRINT()        ({ fprintf(stderr, "TPRINT: %s: %s line %d\n", \
00046                                 __PRETTY_FUNCTION__, __FILE__, __LINE__); fflush(NULL); })
00047 #else
00048 #define TPRINT()
00049 #endif
00050 
00051 namespace MPS {
00052 
00053 int InputStream::readint() {
00054   TPRINT();
00055 
00056   int result = 0;
00057   result = result | (unsigned char) read();
00058   result = (result << 8) | (unsigned char) read();
00059   result = (result << 8) | (unsigned char) read();
00060   result = (result << 8) | (unsigned char) read();
00061   return result;
00062 }
00063 
00064 string InputStream::readstring() {
00065   TPRINT();
00066 
00067   string result;
00068   char ch;
00069 
00070   while (1) {
00071     ch = (char) read();
00072     if (ch == 0)
00073       break;
00074 
00075     result.append(1, ch);
00076   }
00077 
00078   return result;
00079 }
00080 
00081 bool InputStream::readbool() {
00082   TPRINT();
00083 
00084   return (read() != 0);
00085 }
00086 
00087 long long InputStream::readlong() {
00088   TPRINT();
00089 
00090   long long result = 0;
00091   result = result | (unsigned char) read();
00092   result = (result << 8) | (unsigned char) read();
00093   result = (result << 8) | (unsigned char) read();
00094   result = (result << 8) | (unsigned char) read();
00095   result = (result << 8) | (unsigned char) read();
00096   result = (result << 8) | (unsigned char) read();
00097   result = (result << 8) | (unsigned char) read();
00098   result = (result << 8) | (unsigned char) read();
00099   return result;
00100 }
00101 
00102 char InputStream::readchar() {
00103   TPRINT();
00104 
00105   return (char) read();
00106 }
00107 
00108 string InputStream::readReference() {
00109   TPRINT();
00110 
00111   return readstring();
00112 }
00113 
00114 float InputStream::readfloat() {
00115   TPRINT();
00116 
00117   assert(sizeof(int) == sizeof(float));
00118   int intBits = readint();
00119   return * (float *) &intBits;
00120 }
00121 
00122 void OutputStream::writeint(int i) {
00123   TPRINT();
00124 
00125   write((i >> 24) & 255);
00126   write((i >> 16) & 255);
00127   write((i >> 8) & 255);
00128   write(i & 255);
00129 }
00130 
00131 void OutputStream::writestring(string const &s) {
00132   TPRINT();
00133 
00134   for (unsigned int i = 0; i < s.size(); i++) {
00135     char c = s[i];
00136     if (c == 0)
00137       break;
00138     write(c);
00139   }
00140   write(0);
00141 }
00142 
00143 void OutputStream::writebool(bool b) {
00144   TPRINT();
00145 
00146   write(b ? 1 : 0);
00147 }
00148 
00149 void OutputStream::writelong(long long i) {
00150   TPRINT();
00151 
00152   write((int) (i >> 56) & 255);
00153   write((int) (i >> 48) & 255);
00154   write((int) (i >> 40) & 255);
00155   write((int) (i >> 32) & 255);
00156   write((int) (i >> 24) & 255);
00157   write((int) (i >> 16) & 255);
00158   write((int) (i >> 8) & 255);
00159   write((int) i & 255);
00160 }
00161 
00162 void OutputStream::writechar(char ch) {
00163   TPRINT();
00164 
00165   write(ch);
00166 }
00167 
00168 void OutputStream::writeReference(ref<Interface> const &what) {
00169   TPRINT();
00170 
00171   InterfaceServer *server = what->_getInterfaceServer();
00172   string boundName = "";
00173 
00174   if (server == 0) {
00175     // No local server for this object, so we're not bound to any
00176     // local addresses; see if we're a proxy, and if so, send across
00177     // the remote address object the object we proxy for.
00178     boundName = what->_getRemoteAddress();
00179   } else {
00180     boundName = server->getBoundName(referenceTransport());
00181     if (boundName.empty()) {
00182       // Uhoh, our object isn't bound to our preferred transport!
00183       // Fall back on "inet" transport... dodgy? (yes)
00184       boundName = server->getBoundName("inet");
00185     }
00186   }
00187 
00188   if (boundName.empty()) {
00189     char buf[256];
00190     sprintf(buf, "Interface %p has no bound name for transport '%s'",
00191             (void *) what.get(),
00192             referenceTransport().c_str());
00193     throw MPSException(buf);
00194   }
00195 
00196   writestring(boundName);
00197 }
00198 
00199 void OutputStream::writefloat(float f) {
00200   TPRINT();
00201 
00202   assert(sizeof(float) == sizeof(int));
00203   writeint(* (int *) &f);
00204 }
00205 
00206 }

Generated at Wed Aug 15 01:05:16 2001 for mps-cpp by doxygen1.2.6 written by Dimitri van Heesch, © 1997-2001