the library-mode server -- opens an arbitrary socket, writes its details (host and port number) to stdout, and serves for a single connection on that socket. when the connection is closed, the server quits and the application will usually terminate
Inheritance: BaseServer
Example #1
0
        public void Main(string[] args)
        {
            Dictionary <string, object> options = parse_args(new Dictionary <string, ArgSpec> {
                { "-m", new ArgSpec {
                      name = "mode",
                      type = delegate(string val) {
                          val = val.ToLower();
                          if (val == "lib" || val == "library")
                          {
                              return(ServingMode.LIB);
                          }
                          else if (val == "simple")
                          {
                              return(ServingMode.SIMPLE);
                          }
                          else if (val == "threaded")
                          {
                              return(ServingMode.THREADED);
                          }
                          else
                          {
                              throw new ArgumentException("invalid mode: " + val);
                          }
                      },
                      defaultvalue = ServingMode.SIMPLE,
                  } },
                { "-h", new ArgSpec {
                      name         = "host",
                      type         = delegate(string val) { return(val); },
                      defaultvalue = "127.0.0.1",
                  } },
                { "-p", new ArgSpec {
                      name         = "port",
                      type         = delegate(string val) { return(Int32.Parse(val)); },
                      defaultvalue = 0,
                  } },
            },
                                                             args);

            ServingMode mode   = (ServingMode)options["mode"];
            BaseServer  server = null;

            switch (mode)
            {
            case ServingMode.SIMPLE:
                if ((int)options["port"] == 0)
                {
                    throw new ArgumentException("simple mode requires specifying a port");
                }
                server = new SimpleServer(processorFactory,
                                          new SocketTransportFactory((string)options["host"], (int)options["port"]));
                break;

            case ServingMode.THREADED:
                if ((int)options["port"] == 0)
                {
                    throw new ArgumentException("threaded mode requires specifying a port");
                }
                server = new ThreadedServer(processorFactory,
                                            new SocketTransportFactory((string)options["host"], (int)options["port"]));
                break;

            case ServingMode.LIB:
                server = new LibraryModeServer(processorFactory,
                                               new SocketTransportFactory((string)options["host"], (int)options["port"]));
                break;

            default:
                throw new ArgumentException("invalid mode: " + mode);
            }

            server.Serve();
        }
Example #2
0
        public void Main(string[] args)
        {
            Dictionary<string, object> options = parse_args(new Dictionary<string, ArgSpec> {
                {"-m", new ArgSpec {
                        name = "mode",
                        type = delegate(string val) {
                            val = val.ToLower();
                            if (val == "lib" || val == "library") {
                                return ServingMode.LIB;
                            }
                            else if (val == "simple") {
                                return ServingMode.SIMPLE;
                            }
                            else if (val == "threaded") {
                                return ServingMode.THREADED;
                            }
                            else {
                                throw new ArgumentException("invalid mode: " + val);
                            }
                        },
                        defaultvalue = ServingMode.SIMPLE,
                    }},
                    {"-h", new ArgSpec {
                        name = "host",
                        type = delegate(string val) {return val;},
                        defaultvalue = "127.0.0.1",
                    }},
                    {"-p", new ArgSpec {
                        name = "port",
                        type = delegate(string val) {return Int32.Parse(val);},
                        defaultvalue = 0,
                    }},
                },
                args);

            ServingMode mode = (ServingMode)options["mode"];
            BaseServer server = null;

            switch (mode)
            {
                case ServingMode.SIMPLE:
                    if ((int)options["port"] == 0) {
                        throw new ArgumentException("simple mode requires specifying a port");
                    }
                    server = new SimpleServer(processorFactory,
                                              new SocketTransportFactory((string)options["host"], (int)options["port"]));
                    break;
                case ServingMode.THREADED:
                    if ((int)options["port"] == 0) {
                        throw new ArgumentException("threaded mode requires specifying a port");
                    }
                    server = new ThreadedServer(processorFactory,
                                                new SocketTransportFactory((string)options["host"], (int)options["port"]));
                    break;
                case ServingMode.LIB:
                    server = new LibraryModeServer(processorFactory,
                                                   new SocketTransportFactory((string)options["host"], (int)options["port"]));
                    break;
                default:
                    throw new ArgumentException("invalid mode: " + mode);
            }

            server.Serve();
        }