예제 #1
0
        protected async Task <ServiceDefinition> PullServiceDefinition(string servicetype, CancellationToken cancel)
        {
            var e3 = new MessageEntry(MessageEntryType.GetServiceDesc, "");

            e3.ServicePath = m_ServiceName;
            if (servicetype != null)
            {
                e3.AddElement("ServiceType", servicetype);
            }

            var res = await ProcessRequest(e3, cancel);

            var def = res.FindElement("servicedef").CastData <string>();

            if (def == "")
            {
                throw new ServiceNotFoundException("Could not find service definition");
            }
            var d = new ServiceDefinition();

            d.FromString(def);

            if (servicetype == null)
            {
                if (res.elements.Count(x => x.ElementName == "attributes") != 0)
                {
                    lock (this)
                    {
                        Attributes = (Dictionary <string, object>)node.UnpackMapType <string, object>(res.FindElement("attributes").CastData <MessageElementMap <string> >(), null);
                    }
                }
            }

            return(d);
        }
예제 #2
0
 public ServiceDefinition ServiceDef()
 {
     if (sdef == null)
     {
         sdef = new ServiceDefinition();
         sdef.FromString(DefString());
     }
     return(sdef);
 }
예제 #3
0
        static int Main(string[] args)
        {
            try
            {
                bool   show_help    = false;
                bool   show_version = false;
                bool   thunksource  = false;
                string lang         = null;
                var    sources      = new List <Source>();
                string outfile      = null;
                var    include_dirs = new List <string>();
                string output_dir   = ".";

                var p = new OptionSet()
                {
                    { "thunksource", "generate thunk source code", v => thunksource = v != null },
                    { "version", "print program version", v => show_version = v != null },
                    { "output-dir=", "directory for output", v => output_dir = v },
                    { "lang=", "language to generate sources for for (only csharp currently supported)", v => lang = v },
                    { "import=", "input file for use in imports", v => sources.Add(new Source {
                            filename = v, is_import = true
                        }) },
                    { "I|include-path=", "include path", v => include_dirs.Add(v) },
                    { "outfile=", "unified output file (csharp only)", v => outfile = v },
                    { "h|help", "show this message and exit", v => show_help = v != null },
                };

                try
                {
                    foreach (var s in p.Parse(args))
                    {
                        sources.Add(new Source {
                            filename = s, is_import = false
                        });
                    }
                }
                catch (OptionException e)
                {
                    Console.Write("RobotRaconteurWebGen: fatal error: ");
                    Console.WriteLine(e.Message);
                    Console.WriteLine("Try 'RobotRaconteurWebGen --help' for more information.");
                    return(1);
                }


                if (show_help)
                {
                    ShowHelp(p);
                    return(0);
                }

                if (show_version)
                {
                    ShowVersion(p);
                    return(0);
                }

                if (!thunksource)
                {
                    Console.WriteLine("RobotRaconteurWebGen: fatal error: no command specified");
                    Console.WriteLine("Try 'RobotRaconteurWebGen --help' for more information.");
                    return(1);
                }

                if (sources.Count(x => x.is_import == false) == 0)
                {
                    Console.WriteLine("RobotRaconteurWebGen: fatal error: no files specified for thunksource");
                    return(1001);
                }

                if (lang != "csharp")
                {
                    Console.WriteLine("RobotRaconteurWebGen: fatal error: unknown or no language specified");
                    return(1012);
                }

                var robdef_path = Environment.GetEnvironmentVariable("ROBOTRACONTEUR_ROBDEF_PATH");
                if (robdef_path != null)
                {
                    robdef_path = robdef_path.Trim();
                    var env_dirs = robdef_path.Split(Path.PathSeparator);
                    include_dirs.AddRange(env_dirs);
                }

                foreach (var s in sources)
                {
                    if (!File.Exists(s.filename) && !Path.IsPathFullyQualified(s.filename))
                    {
                        foreach (var inc in include_dirs)
                        {
                            string s3 = Path.Join(inc, s.filename);
                            if (File.Exists(s3))
                            {
                                s.filename = s3;
                                break;
                            }
                        }
                    }
                }

                foreach (var s in sources)
                {
                    if (!File.Exists(s.filename))
                    {
                        Console.WriteLine("RobotRaconteurWebGen: fatal error: input file not found {0}", s.filename);
                        return(1002);
                    }
                }

                if (!Directory.Exists(output_dir))
                {
                    Console.WriteLine("RobotRaconteurWebGen: fatal error: output director not found {0}", output_dir);
                    return(1003);
                }

                foreach (var s in sources)
                {
                    ServiceDefinition d = null;
                    string            def;
                    try
                    {
                        StreamReader sr = new StreamReader(s.filename);
                        def = sr.ReadToEnd();
                        sr.Close();
                    }
                    catch (Exception)
                    {
                        Console.WriteLine("RobotRaconteurWebGen: fatal error: could not open file {0}", s.filename);
                        return(1004);
                    }

                    try
                    {
                        d = new ServiceDefinition();
                        ServiceDefinitionParseInfo parse_info = default(ServiceDefinitionParseInfo);
                        parse_info.RobDefFilePath = s.filename;
                        d.FromString(def, parse_info);
                        string a = d.ToString();
                        s.full_text   = def;
                        s.service_def = d;
                    }
                    catch (ServiceDefinitionParseException ee)
                    {
                        Console.WriteLine("{0}({1}): error: {2}", s.filename, ee.ParseInfo.LineNumber, ee.ShortMessage);
                        return(1005);
                    }
                }

                try
                {
                    ServiceDefinitionUtil.VerifyServiceDefinitions(sources.Select(x => x.service_def).ToDictionary(x => x.Name));
                }
                catch (ServiceDefinitionParseException ee)
                {
                    Console.WriteLine("{0}({1}): error: {2}", ee.ParseInfo.RobDefFilePath, ee.ParseInfo.LineNumber, ee.ShortMessage);
                    return(1007);
                }
                catch (ServiceDefinitionVerifyException ee)
                {
                    Console.WriteLine("{0}({1}): error: {2}", ee.ParseInfo.RobDefFilePath, ee.ParseInfo.LineNumber, ee.ShortMessage);
                    return(1008);
                }
                catch (Exception ee)
                {
                    Console.WriteLine("RobotRaconteurWebGen: fatal error: could not verify service definition set {0}", ee.Message);
                    return(1009);
                }

                if (outfile == null)
                {
                    foreach (var s in sources)
                    {
                        if (s.is_import)
                        {
                            continue;
                        }
                        try
                        {
                            CSharpServiceLangGen.GenerateFiles(s.service_def, s.full_text, output_dir);
                        }
                        catch (Exception ee)
                        {
                            Console.WriteLine("{0}: error: could not generate thunksource files {1}", s.filename, ee.Message);
                            return(1010);
                        }
                    }
                    return(0);
                }
                else
                {
                    StreamWriter f2;
                    try
                    {
                        f2 = new StreamWriter(outfile);
                    }
                    catch (Exception)
                    {
                        Console.WriteLine("RobotRaconteurWebGen: fatal error: could not open outfile for writing {0}", outfile);
                        return(1015);
                    }

                    using (f2)
                    {
                        CSharpServiceLangGen.GenerateOneFileHeader(f2);

                        foreach (var s in sources)
                        {
                            if (s.is_import)
                            {
                                continue;
                            }
                            try
                            {
                                CSharpServiceLangGen.GenerateOneFilePart(s.service_def, s.full_text, f2);
                            }
                            catch (Exception ee)
                            {
                                Console.WriteLine("{0}: error: could not generate thunksource files {1}", s.filename, ee.Message);
                                return(1010);
                            }
                        }
                    }
                    return(0);
                }
            }
            catch (Exception e)
            {
                Console.Write("RobotRaconteurWebGen: error: ");
                Console.WriteLine(e.Message);
                return(1);
            }

            Console.WriteLine("RobotRaconteurGen: error: unknown internal error");

            return(7);
        }
예제 #4
0
        static void Main(string[] args)
        {
            string command = args[0];

            if (command == "loopback")
            {
                var t = new TcpTransport();
                t.StartServer(22332);
                RobotRaconteurNode.s.RegisterTransport(t);

                RobotRaconteurNode.s.RegisterServiceType(new com.robotraconteur.testing.TestService1.com__robotraconteur__testing__TestService1Factory());
                RobotRaconteurNode.s.RegisterServiceType(new com.robotraconteur.testing.TestService2.com__robotraconteur__testing__TestService2Factory());

                var s1 = new RobotRaconteurTestServiceSupport();
                s1.RegisterServices(t);

                ServiceTestClient s = new ServiceTestClient();
                s.RunFullTest("rr+tcp://localhost:22332/?service=RobotRaconteurTestService",
                              "rr+tcp://localhost:22332/?service=RobotRaconteurTestService_auth")
                .GetAwaiter().GetResult();

                RobotRaconteurNode.s.Shutdown();
            }
            else
            if (command == "client")
            {
                var url      = args[1];
                var auth_url = args[2];
                int count    = 1;
                if (args.Length >= 4)
                {
                    count = Int32.Parse(args[3]);
                }

                var t  = new TcpTransport();
                var t2 = new LocalTransport();

                RobotRaconteurNode.s.RegisterTransport(t);
                RobotRaconteurNode.s.RegisterTransport(t2);


                RobotRaconteurNode.s.RegisterServiceType(new com.robotraconteur.testing.TestService1.com__robotraconteur__testing__TestService1Factory());
                RobotRaconteurNode.s.RegisterServiceType(new com.robotraconteur.testing.TestService2.com__robotraconteur__testing__TestService2Factory());

                for (int i = 0; i < count; i++)
                {
                    ServiceTestClient s = new ServiceTestClient();
                    s.RunFullTest(url, auth_url).GetAwaiter().GetResult();
                }

                RobotRaconteurNode.s.Shutdown();

                Console.WriteLine("Test complete, no errors detected");
            }
            else if (command == "server")
            {
                int port = int.Parse(args[1]);

                //NodeID id = new NodeID(args[2]);
                string name = args[2];


                var t2 = new LocalTransport();
                t2.StartServerAsNodeName(name);


                var t = new TcpTransport();
                RobotRaconteurNode.s.RegisterTransport(t2);

                t.StartServer(port);

                RobotRaconteurNode.s.RegisterTransport(t);
                t.EnableNodeAnnounce();

                try
                {
                    t.LoadTlsNodeCertificate();
                }
                catch (Exception)
                {
                    Console.WriteLine("Warning: Could not load TLS certificate");
                }

                RobotRaconteurNode.s.RegisterServiceType(new com.robotraconteur.testing.TestService1.com__robotraconteur__testing__TestService1Factory());
                RobotRaconteurNode.s.RegisterServiceType(new com.robotraconteur.testing.TestService2.com__robotraconteur__testing__TestService2Factory());
                RobotRaconteurNode.s.RegisterServiceType(new com.robotraconteur.testing.TestService3.com__robotraconteur__testing__TestService3Factory());

                var s = new RobotRaconteurTestServiceSupport();
                s.RegisterServices(t);

                var s2 = new RobotRaconteurTestServiceSupport2();
                s2.RegisterServices(t);

                Console.WriteLine("Press enter to quit...");
                Console.ReadLine();

                RobotRaconteurNode.s.Shutdown();
            }
            else if (command == "findservicebytype")
            {
                string   servicetype = args[1];
                string[] schemes     = args[2].Split(new char[] { ',' });

                var t = new TcpTransport();
                t.EnableNodeDiscoveryListening();
                RobotRaconteurNode.s.RegisterTransport(t);


                System.Threading.Thread.Sleep(5000);

                var r = RobotRaconteurNode.s.FindServiceByType(servicetype, schemes).Result;

                foreach (var e in r)
                {
                    Console.WriteLine("Name: " + e.Name);
                    Console.WriteLine("RootObjectType: " + e.RootObjectType);
                    Console.WriteLine("RootObjectImplements: " + e.ConnectionURL);
                    Console.WriteLine("ConnectionURL: " + String.Join(", ", e.ConnectionURL));
                    Console.WriteLine("NodeID: " + e.NodeID.ToString());
                    Console.WriteLine("NodeName: " + e.NodeName);
                    Console.WriteLine();
                }

                RobotRaconteurNode.s.Shutdown();
            }
            else if (command == "findnodebyid")
            {
                var      nodeid  = new NodeID(args[1]);
                string[] schemes = args[2].Split(new char[] { ',' });

                var t = new TcpTransport();
                t.EnableNodeDiscoveryListening();
                RobotRaconteurNode.s.RegisterTransport(t);

                System.Threading.Thread.Sleep(6000);

                var r = RobotRaconteurNode.s.FindNodeByID(nodeid, schemes).Result;

                foreach (var e in r)
                {
                    Console.WriteLine("NodeID: " + e.NodeID);
                    Console.WriteLine("NodeName: " + e.NodeName);
                    Console.WriteLine("ConnectionURL: " + String.Join(", ", e.ConnectionURL));
                    Console.WriteLine();
                }

                RobotRaconteurNode.s.Shutdown();
            }
            else if (command == "findnodebyname")
            {
                var      name    = args[1];
                string[] schemes = args[2].Split(new char[] { ',' });

                var t = new TcpTransport();
                t.EnableNodeDiscoveryListening();
                RobotRaconteurNode.s.RegisterTransport(t);

                System.Threading.Thread.Sleep(6000);

                var r = RobotRaconteurNode.s.FindNodeByName(name, schemes).Result;

                foreach (var e in r)
                {
                    Console.WriteLine("NodeID: " + e.NodeID);
                    Console.WriteLine("NodeName: " + e.NodeName);
                    Console.WriteLine("ConnectionURL: " + String.Join(", ", e.ConnectionURL));
                    Console.WriteLine();
                }

                RobotRaconteurNode.s.Shutdown();
            }
            else if (command == "robdeftest")
            {
                var robdef_filenames = args.Skip(1).ToArray();

                var defs  = new Dictionary <string, ServiceDefinition>();
                var defs2 = new Dictionary <string, ServiceDefinition>();

                foreach (var fname in robdef_filenames)
                {
                    string robdef_text = new StreamReader(fname).ReadToEnd();
                    var    def         = new ServiceDefinition();
                    def.FromString(robdef_text);
                    defs.Add(def.Name, def);
                    string robdef_text2 = def.ToString();
                    var    def3         = new ServiceDefinition();
                    def3.FromString(robdef_text2);
                    defs2.Add(def3.Name, def3);
                }

                ServiceDefinitionUtil.VerifyServiceDefinitions(defs);

                foreach (var n in defs.Keys)
                {
                    if (!ServiceDefinitionUtil.CompareServiceDefinitions(defs[n], defs2[n]))
                    {
                        throw new Exception("Service definition parse does not match");
                    }
                }

                foreach (var def in defs.Values)
                {
                    Console.WriteLine(def.ToString());
                }


                foreach (var def in defs.Values)

                {
                    foreach (var c in def.Constants.Values)

                    {
                        if (c.Name == "strconst")
                        {
                            var strconst = c.ValueToString();
                            Console.WriteLine("strconst " + strconst);

                            var strconst2 = ConstantDefinition.EscapeString(strconst);
                            var strconst3 = ConstantDefinition.UnescapeString(strconst2);

                            if (strconst3 != strconst)
                            {
                                throw new Exception("");
                            }
                        }

                        if (c.Name == "int32const")
                        {
                            Console.WriteLine("int32const: " + c.ValueToScalar <int>());
                        }

                        if (c.Name == "int32const_array")
                        {
                            var a = c.ValueToArray <int>();
                            Console.WriteLine("int32const_array: " + a.Length);
                        }

                        if (c.Name == "doubleconst_array")
                        {
                            var a = c.ValueToArray <double>();
                            Console.WriteLine("doubleconst_array: " + a.Length);
                        }

                        if (c.Name == "structconst")
                        {
                            var s = c.ValueToStructFields();
                            foreach (var f in s)
                            {
                                Console.Write(f.Name + ": " + f.ConstantRefName + " ");
                            }
                            Console.WriteLine();
                        }
                    }
                }

                ServiceDefinition def1;
                if (defs.TryGetValue("com.robotraconteur.testing.TestService1", out def1))
                {
                    var entry = def1.Objects["testroot"];

                    var p1 = (PropertyDefinition)entry.Members["d1"];
                    if (p1.Direction != MemberDefinition_Direction.both)
                    {
                        throw new Exception();
                    }

                    var p2 = (PipeDefinition)entry.Members["p1"];
                    if (p2.Direction != MemberDefinition_Direction.both)
                    {
                        throw new Exception();
                    }
                    if (p2.IsUnreliable)
                    {
                        throw new Exception();
                    }

                    var w1 = (WireDefinition)entry.Members["w1"];
                    if (w1.Direction != MemberDefinition_Direction.both)
                    {
                        throw new Exception();
                    }

                    var m1 = (MemoryDefinition)entry.Members["m1"];
                    if (m1.Direction != MemberDefinition_Direction.both)
                    {
                        throw new Exception();
                    }
                }

                ServiceDefinition def2;
                if (defs.TryGetValue("com.robotraconteur.testing.TestService3", out def2))
                {
                    var entry = def2.Objects["testroot3"];

                    var p1 = (PropertyDefinition)entry.Members["readme"];
                    if (p1.Direction != MemberDefinition_Direction.readonly_)
                    {
                        throw new Exception();
                    }

                    var p2 = (PropertyDefinition)entry.Members["writeme"];
                    if (p2.Direction != MemberDefinition_Direction.writeonly)
                    {
                        throw new Exception();
                    }

                    var p3 = (PipeDefinition)entry.Members["unreliable1"];
                    if (p3.Direction != MemberDefinition_Direction.readonly_)
                    {
                        throw new Exception();
                    }
                    if (!p3.IsUnreliable)
                    {
                        throw new Exception();
                    }

                    var p4 = (PipeDefinition)entry.Members["unreliable2"];
                    if (p4.Direction != MemberDefinition_Direction.both)
                    {
                        throw new Exception();
                    }
                    if (!p4.IsUnreliable)
                    {
                        throw new Exception();
                    }

                    var w1 = (WireDefinition)entry.Members["peekwire"];
                    if (w1.Direction != MemberDefinition_Direction.readonly_)
                    {
                        throw new Exception();
                    }

                    var w2 = (WireDefinition)entry.Members["pokewire"];
                    if (w2.Direction != MemberDefinition_Direction.writeonly)
                    {
                        throw new Exception();
                    }

                    var m1 = (MemoryDefinition)entry.Members["readmem"];
                    if (m1.Direction != MemberDefinition_Direction.readonly_)
                    {
                        throw new Exception();
                    }

                    Console.WriteLine("Found it");
                }

                return;
            }
            else
            if (command == "loopback2")
            {
                var t = new TcpTransport();
                t.StartServer(22332);
                RobotRaconteurNode.s.RegisterTransport(t);

                RobotRaconteurNode.s.RegisterServiceType(new com.robotraconteur.testing.TestService1.com__robotraconteur__testing__TestService1Factory());
                RobotRaconteurNode.s.RegisterServiceType(new com.robotraconteur.testing.TestService2.com__robotraconteur__testing__TestService2Factory());
                RobotRaconteurNode.s.RegisterServiceType(new com.robotraconteur.testing.TestService3.com__robotraconteur__testing__TestService3Factory());

                var s1 = new RobotRaconteurTestServiceSupport2();
                s1.RegisterServices(t);

                ServiceTestClient2 s = new ServiceTestClient2();
                s.RunFullTest("rr+tcp://localhost:22332/?service=RobotRaconteurTestService2")
                .GetAwaiter().GetResult();

                RobotRaconteurNode.s.Shutdown();
            }
            else
            if (command == "client2")
            {
                var url   = args[1];
                int count = 1;
                if (args.Length >= 3)
                {
                    count = Int32.Parse(args[2]);
                }

                var t = new TcpTransport();

                RobotRaconteurNode.s.RegisterTransport(t);


                RobotRaconteurNode.s.RegisterServiceType(new com.robotraconteur.testing.TestService1.com__robotraconteur__testing__TestService1Factory());
                RobotRaconteurNode.s.RegisterServiceType(new com.robotraconteur.testing.TestService2.com__robotraconteur__testing__TestService2Factory());
                RobotRaconteurNode.s.RegisterServiceType(new com.robotraconteur.testing.TestService3.com__robotraconteur__testing__TestService3Factory());

                for (int i = 0; i < count; i++)
                {
                    ServiceTestClient2 s = new ServiceTestClient2();
                    s.RunFullTest(url).GetAwaiter().GetResult();
                }

                RobotRaconteurNode.s.Shutdown();

                Console.WriteLine("Test complete, no errors detected");
            }
            else
            {
                throw new Exception("Unknown command");
            }
        }