summaryrefslogtreecommitdiff
path: root/IvyTransduct/IvyTransduct.cs
blob: b8fc3662e41ee15fa1ae7e55e6983e7e5c4248e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
using System;
using System.Collections.Generic;
using System.Text;
using IvyBus;
using Gnu;
using System.Xml;

namespace IvyTransduct
{
    class IvyTransduct
    {
        public const String helpmsg =
            "usage: IvyProbe [options] filename\n" +
            "\t-b BUS\tspecifies the Ivy bus domain\n" +
            "\t-n ivyname (default IvyTransduct)\n" +
            "\t-d\tdebug\n" +
            "\t-h\thelp\n\n" +
            "\t-f filename ( message to transduct)";
        static Ivy bus;
        static bool debug = false;

        static void resentCB(object sender, IvyMessageEventArgs args)
        {
            IvyApplicationBinding binding = sender as IvyApplicationBinding;
            foreach (string msg in (List<string>)binding.CallbackArguments[0])
            {
                try
                {
                    string msg_out = string.Format(msg, args.GetArguments());
                    if (debug)
                        Console.WriteLine("resent msg: {0} => {1}", args, msg_out);
                    bus.SendMsg(msg_out);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Error sending msg {0} : {1}", msg, ex.Message);
                }
            }
           
        }
        static void Main(string[] args)
        {
            string domain = Ivy.GetDomain(null);
           
            string name = "IvyTransduct";

            GetOpt opt = new GetOpt(args, "n:b:dh");
            Arg a;
            while ((a = opt.NextArg()) != null)
            {
                switch (a.Flag)
                {
                    case 'd':
                        debug = true;
                        break;

                    case 'b':
                        domain = a.Parameter;
                        break;

                    case 'n':
                        name = a.Parameter;
                        break;

                    case 'h':
                    default:
                        System.Console.Out.WriteLine(helpmsg);
                        System.Environment.Exit(0);
                        break;

                }
            }
            bus = new Ivy(name, name + " ready");
           
            foreach (string filename in opt.Extras)
            {
                if (debug)
                    Console.WriteLine("Read File {0}", filename);
                try
                {
                    XmlDocument xmlDoc = new XmlDocument();
                    xmlDoc.Load(filename);
                    foreach (XmlElement msg in xmlDoc.SelectNodes("/transduct/message"))
                    {
                        string msg_in = msg.SelectSingleNode("input").Attributes["bind"].Value;
                        if (debug) Console.WriteLine("message: " + msg_in);
                        List<string> outputs = new List<string>();
                        foreach ( XmlElement item in msg.SelectNodes( "output/item" ) )
                        {
                            string msg_out = item.Attributes["msg"].Value;
                            if (debug) Console.WriteLine("\t out: " + msg_out);
                            outputs.Add(msg_out);
                        }
                        bus.BindMsg(msg_in, resentCB, outputs);
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Error reading file {0} : {1}", filename, ex.Message);
                }
            }
            bus.Start(domain);
        }
    }
}