00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
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
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 }