示例#1
0
        public static void Main(string[] args)
        {
            string webPort = "8088";

            Console.WriteLine("MigService test APP");
            Console.WriteLine("URL: http://localhost:{0}", webPort);

            var migService = new MigService();

            // Add and configure the Web gateway
            var web = migService.AddGateway("WebServiceGateway");

            web.SetOption("HomePath", "html");
            web.SetOption("BaseUrl", "/pages/");
            web.SetOption("Host", "*");
            web.SetOption("Port", webPort);
            web.SetOption("Password", "");
            web.SetOption("EnableFileCaching", "False");

            // Add and configure the Web Socket gateway
            var ws = migService.AddGateway("WebSocketGateway");

            ws.SetOption("Port", "8181");

            migService.StartService();

            migService.RegisterApi("myapp/demo", (request) =>
            {
                Console.WriteLine("Received API call from source {0}\n", request.Context.Source);
                Console.WriteLine("[Context data]\n{0}\n", MigService.JsonSerialize(request.Context.Data, true));
                Console.WriteLine("[Mig Command]\n{0}\n", MigService.JsonSerialize(request.Command, true));

                var cmd = request.Command;

                // cmd.Domain is the first element in the API URL (myapp)
                // cmd.Address is the second element in the API URL (demo)
                // cmd.Command is the third element in the API URL (greet | echo | ping)
                // cmd.GetOption(<n>) will give all the subsequent elements in the API URL (0...n)

                switch (cmd.Command)
                {
                case "greet":
                    var name = cmd.GetOption(0);
                    migService.RaiseEvent(typeof(MainClass), cmd.Domain, cmd.Address, "Reply to Greet", "Greet.User", name);
                    break;

                case "echo":
                    string fullRequestPath = cmd.OriginalRequest;
                    migService.RaiseEvent(typeof(MainClass), cmd.Domain, cmd.Address, "Reply to Echo", "Echo.Data", fullRequestPath);
                    break;

                case "ping":
                    migService.RaiseEvent(typeof(MainClass), cmd.Domain, cmd.Address, "Reply to Ping", "Ping.Reply", "PONG");
                    break;
                }

                return(new ResponseStatus(Status.Ok));
            });

            while (true)
            {
                Thread.Sleep(10000);
            }
        }
示例#2
0
        public static void Main(string[] args)
        {
            string webPort = "8088";

            Console.WriteLine("MigService test APP");
            Console.WriteLine("URL: http://localhost:{0}", webPort);

            var migService = new MigService();

            // Add and configure the Web gateway
            var web = migService.AddGateway("WebServiceGateway");

            web.SetOption("HomePath", "html");
            web.SetOption("BaseUrl", "/pages/");
            web.SetOption("Host", "*");
            web.SetOption("Port", webPort);
            web.SetOption("Password", "");
            web.SetOption("EnableFileCaching", "False");

            // Add and configure the Web Socket gateway
            var ws = migService.AddGateway("WebSocketGateway");

            ws.SetOption("Port", "8181");

            // Configuration can also be loaded from a file as shown below

            /*
             * MigServiceConfiguration configuration;
             * // Construct an instance of the XmlSerializer with the type
             * // of object that is being deserialized.
             * XmlSerializer mySerializer = new XmlSerializer(typeof(MigServiceConfiguration));
             * // To read the file, create a FileStream.
             * FileStream myFileStream = new FileStream("systemconfig.xml", FileMode.Open);
             * // Call the Deserialize method and cast to the object type.
             * configuration = (MigServiceConfiguration)mySerializer.Deserialize(myFileStream);
             * // Set the configuration
             * migService.Configuration = configuration;
             */

            migService.StartService();

            // Enable some interfaces for testing...

            /*
             * var zwave = migService.AddInterface("HomeAutomation.ZWave", "MIG.HomeAutomation.dll");
             * zwave.SetOption("Port", "/dev/ttyUSB0");
             * migService.EnableInterface("HomeAutomation.ZWave");
             */

            /*
             * var upnp = migService.AddInterface("Protocols.UPnP", "MIG.Protocols.dll");
             * migService.EnableInterface("Protocols.UPnP");
             */

            migService.RegisterApi("myapp/demo", (request) =>
            {
                Console.WriteLine("Received API call from source {0}\n", request.Context.Source);
                Console.WriteLine("[Context data]\n{0}\n", MigService.JsonSerialize(request.Context.Data, true));
                Console.WriteLine("[Mig Command]\n{0}\n", MigService.JsonSerialize(request.Command, true));

                var cmd = request.Command;

                // cmd.Domain is the first element in the API URL (myapp)
                // cmd.Address is the second element in the API URL (demo)
                // cmd.Command is the third element in the API URL (greet | echo | ping)
                // cmd.GetOption(<n>) will give all the subsequent elements in the API URL (0...n)

                switch (cmd.Command)
                {
                case "greet":
                    var name = cmd.GetOption(0);
                    migService.RaiseEvent(typeof(MainClass), cmd.Domain, cmd.Address, "Reply to Greet", "Greet.User", name);
                    break;

                case "echo":
                    string fullRequestPath = cmd.OriginalRequest;
                    migService.RaiseEvent(typeof(MainClass), cmd.Domain, cmd.Address, "Reply to Echo", "Echo.Data", fullRequestPath);
                    break;

                case "ping":
                    migService.RaiseEvent(typeof(MainClass), cmd.Domain, cmd.Address, "Reply to Ping", "Ping.Reply", "PONG");
                    break;
                }

                return(new ResponseStatus(Status.Ok));
            });

            while (true)
            {
                Thread.Sleep(10000);
            }
        }
示例#3
0
        public static void Main(string[] args)
        {
            var    Log            = MigService.Log;
            string webServicePort = "8088";
            string webSocketPort  = "8181";

            string authUser  = "******";
            string authPass  = "******";
            string authRealm = "MIG Secure Zone";

            Log.Info("MigService test APP");
            Log.Info("URL: http://localhost:{0}", webServicePort);

            var migService = new MigService();

            // Add and configure the WebService gateway
            var web = (WebServiceGateway)migService.AddGateway(Gateways.WebServiceGateway);

            web.SetOption(WebServiceGatewayOptions.HomePath, "html");
            web.SetOption(WebServiceGatewayOptions.BaseUrl, "/pages/");
            // for deploying modern web app (eg. Angular 2 apps)
            web.SetOption(WebServiceGatewayOptions.UrlAliasPrefix, "app/*:app/index.html");
            web.SetOption(WebServiceGatewayOptions.Host, "*");
            web.SetOption(WebServiceGatewayOptions.Port, webServicePort);
            if (!String.IsNullOrEmpty(authUser) && !String.IsNullOrEmpty(authPass))
            {
                //web.SetOption(WebServiceGatewayOptions.Authentication, WebAuthenticationSchema.Basic);
                web.SetOption(WebServiceGatewayOptions.Authentication, WebAuthenticationSchema.Digest);
                web.SetOption(WebServiceGatewayOptions.AuthenticationRealm, authRealm);
                web.UserAuthenticationHandler += (sender, eventArgs) =>
                {
                    if (eventArgs.Username == authUser)
                    {
                        // WebServiceGateway requires password to be encrypted using the `Digest.CreatePassword(..)` method.
                        // This applies both to 'Digest' and 'Basic' authentication methods.
                        string password = Digest.CreatePassword(authUser, authRealm, authPass);
                        return(new User(authUser, authRealm, password));
                    }
                    return(null);
                };
            }
            web.SetOption(WebServiceGatewayOptions.EnableFileCaching, "False");

            // Add and configure the WebSocket gateway
            var ws = (WebSocketGateway)migService.AddGateway(Gateways.WebSocketGateway);

            ws.SetOption(WebSocketGatewayOptions.Port, webSocketPort);
            // WebSocketGateway access via authorization token
            ws.SetOption(WebSocketGatewayOptions.Authentication, WebAuthenticationSchema.Token);

            /*
             * if (!String.IsNullOrEmpty(authUser) && !String.IsNullOrEmpty(authPass))
             * {
             *  //ws.SetOption(WebSocketGatewayOptions.Authentication, WebAuthenticationSchema.Basic);
             *  ws.SetOption(WebSocketGatewayOptions.Authentication, WebAuthenticationSchema.Digest);
             *  ws.SetOption(WebSocketGatewayOptions.AuthenticationRealm, authRealm);
             *  ((WebSocketGateway) ws).UserAuthenticationHandler += (sender, eventArgs) =>
             *  {
             *      if (eventArgs.Username == authUser)
             *      {
             *          return new User(authUser, authRealm, authPass);
             *      }
             *      return null;
             *  };
             * }
             */

            migService.StartService();

            // API commands and events are exposed to all active gateways (WebService and WebSocket in this example)
            migService.RegisterApi("myapp/demo", (request) =>
            {
                Log.Debug("Received API call over {0}\n", request.Context.Source);
                Log.Debug("[Context data]\n{0}\n", request.Context.Data);
                Log.Debug("[Mig Command]\n{0}\n", MigService.JsonSerialize(request.Command, true));

                var cmd = request.Command;

                // cmd.Domain is the first element in the API URL (myapp)
                // cmd.Address is the second element in the API URL (demo)
                // cmd.Command is the third element in the API URL (greet | echo | ping)
                // cmd.GetOption(<n>) will give all the subsequent elements in the API URL (0...n)

                switch (cmd.Command)
                {
                case ApiCommands.Token:
                    // authorization token will expire in 5 seconds
                    var token = ws.GetAuthorizationToken(5);
                    return(new ResponseText(token.Value));

                case ApiCommands.Greet:
                    var name = cmd.GetOption(0);
                    migService.RaiseEvent(
                        typeof(MainClass),
                        cmd.Domain,
                        cmd.Address,
                        "Reply to Greet",
                        "Greet.User",
                        name
                        );
                    break;

                case ApiCommands.Echo:
                    string fullRequestPath = cmd.OriginalRequest;
                    migService.RaiseEvent(
                        typeof(MainClass),
                        cmd.Domain,
                        cmd.Address,
                        "Reply to Echo",
                        "Echo.Data",
                        fullRequestPath
                        );
                    break;

                case ApiCommands.Ping:
                    migService.RaiseEvent(
                        typeof(MainClass),
                        cmd.Domain,
                        cmd.Address,
                        "Reply to Ping",
                        "Ping.Reply",
                        "PONG"
                        );
                    break;
                }

                return(new ResponseStatus(Status.Ok));
            });

            while (true)
            {
                Thread.Sleep(5000);
            }
        }