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

namingserviceimpl.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 #include <unistd.h>
00040 
00041 #include <mps/mps.h>
00042 #include <mps/mpsnaming.h>
00043 
00044 #include <mps/transport_inet.h>
00045 
00046 #include <string>
00047 #include <map>
00048 #include <exception>
00049 
00050 using namespace org::hebe::mps::naming;
00051 
00052 static bool debugOn = false;
00053 
00054 class NamingServiceImpl: public NamingService {
00055 private:
00056   typedef map<string, string> dict_t;
00057 
00058   dict_t dict;
00059 
00060 public:
00061   NamingServiceImpl() {}
00062   virtual ~NamingServiceImpl() {}
00063 
00064   virtual string resolve(string const &objectName);
00065   virtual bool bind(string const &objectName, string const &address, bool replace);
00066   virtual bool unbind(string const &objectName);
00067   virtual vector<Binding> enumerate();
00068 };
00069 
00070 string NamingServiceImpl::resolve(string const &objectName) {
00071   if (objectName.substr(0, 4) == "mps:")
00072     return objectName;
00073 
00074   dict_t::iterator i = dict.find(objectName);
00075   return (i == dict.end()) ? "" : (*i).second;
00076 }
00077 
00078 bool NamingServiceImpl::bind(string const &objectName, string const &address, bool replace)
00079 {
00080   if (objectName.substr(0, 4) == "mps:") {
00081     // Disallow binding of names that could be confused with addresses.
00082     return true;
00083   }
00084 
00085   dict_t::iterator i = dict.find(objectName);
00086   if (i != dict.end() && !replace)
00087     return false;
00088 
00089   if (debugOn)
00090     fprintf(stderr, "Binding \"%s\" to \"%s\"\n", objectName.c_str(), address.c_str());
00091 
00092   dict[objectName] = address;
00093   return true;
00094 }
00095 
00096 bool NamingServiceImpl::unbind(string const &objectName) {
00097   dict_t::iterator i = dict.find(objectName);
00098   if (i != dict.end()) {
00099     if (debugOn)
00100       fprintf(stderr, "Unbinding \"%s\" from \"%s\"\n", objectName.c_str(), (*i).second.c_str());
00101 
00102     dict.erase(i);
00103     return true;
00104   }
00105 
00106   return false;
00107 }
00108 
00109 vector<Binding> NamingServiceImpl::enumerate() {
00110   vector<Binding> results;
00111 
00112   for (dict_t::iterator i = dict.begin();
00113        i != dict.end();
00114        i++) {
00115     results.push_back(Binding((*i).first, (*i).second));
00116   }
00117 
00118   return results;
00119 }
00120 
00121 void usage() {
00122   fprintf(stderr,
00123           "Usage: mps_naming [-p <portnumber>] [-i <interfacename>] [-d]\n"
00124           "    <portnumber> defaults to 9091\n"
00125           "    <interfacename> defaults to \"localhost\"\n"
00126           "    -d is for debug information\n");
00127 }
00128 
00129 int main(int argc, char *argv[]) {
00130   int portNumber = 9091;
00131   string hostname = "";
00132 
00133   char optc;
00134   while ((optc = MPS_getopt(argc, argv, "dhp:i:")) != EOF) {
00135     switch (optc) {
00136       case 'd':
00137         debugOn = true;
00138         break;
00139 
00140       case 'h':
00141         usage();
00142         return 0;
00143 
00144       case 'p':
00145         portNumber = atoi(MPS_optarg);
00146         break;
00147 
00148       case 'i':
00149         hostname = MPS_optarg;
00150         break;
00151 
00152       default:
00153         fprintf(stderr, "mps_naming: invalid command-line parameter '%c'", optc);
00154         usage();
00155         return 1;
00156     }
00157   }
00158 
00159   try {
00160 
00161     ref<NamingServiceImpl> impl = new NamingServiceImpl();
00162     NamingServiceServer server("NamingService", impl);
00163 
00164     MPS::InetTransport::initialise(hostname,
00165                                    portNumber);
00166 
00167     impl->bind(server.getObjectName(),
00168                MPS::Transport::bindServer(&server, "mps:inet"),
00169                true);
00170 
00171     MPS::InetTransport::getFdMgr()->mainloop();
00172 
00173   } catch (FileDescriptor::Exception &e) {
00174     fprintf(stderr, "FileDescriptor::Exception: %s (%d %s)\n", e.what(),
00175             e.getErrno(), strerror(e.getErrno()));
00176     return 1;
00177   } catch (std::exception &se) {
00178     fprintf(stderr, "std::exception: %s\n", se.what());
00179     return 1;
00180   } catch (...) {
00181     fprintf(stderr, "UNCAUGHT EXCEPTION!\n");
00182     return 1;
00183   }
00184 
00185   return 0;
00186 }

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