Example #1
0
        /// <summary>
        /// This method completes the initialization of the server. If a
        /// relay is used, it performs the first registration of the
        /// device at the relay. Before it is called, the server
        /// properties must have been set up. Normally, you don’t need to
        /// call this method, since it is called by Run if necessary.
        /// </summary>
        public void Open()
        {
            Contract.Requires(!IsOpen);
            const int relayPort = 80;
            const int localPort = 80;

            // default configuration for the book - must be removed for use on devices without Ethernet:
            StreamFactory = new SocketStreamFactory();

            Contract.Requires(StreamFactory != null);
            // Difference to GSIoT book: a stream factory MUST be
            // plugged in as part of the server's configuration!

            if (RelayDomain != null)
            {
                Contract.Requires(StreamFactory != null);
                Contract.Requires(RelayDomain.Length > 0);
                Contract.Requires(RelaySecretKey != null);
                Contract.Requires(RelaySecretKey.Length > 0);
                if ((RelayDomain == "gsioT-FFMQ-TTD5") ||
                    (RelayDomain == "<insert your relay domain here>"))
                {
                    throw new Exception(
                        "Please use your own relay domain!\r\n" +
                        "See http://www.gsiot.info/yaler/ for more information on how to\r\n" +
                        "get your own relay domain and secret relay key.");
                }
                DebugPrint("StreamFactory.Listen(\"try.yaler.net\", relayPort, RelayDomain, RelaySecretKey)");  // added jcc
                streamListener = StreamFactory.Listen("try.yaler.net", relayPort, RelayDomain, RelaySecretKey);
            }
            else
            {
                DebugPrint("StreamFactory.Listen(localPort)");  // added jcc
                streamListener = StreamFactory.Listen(localPort);
            }
            Contract.Ensures(streamListener != null);
            Contract.Requires(RequestRouting != null);

            serviceRootPath = streamListener.LocalUrl;
            Trace.TraceInformation("Base Uri: " + serviceRootPath + "/");

            Diagnostics = new ServerDiagnostics();
            Diagnostics.StartTime = DateTime.Now;

            IsOpen = true;
            DebugPrint("HttpServer: server opened");
        }