Ejemplo n.º 1
0
        public void Start(string configFile)
        {
            config = ServiceConfig.FromXml(configFile);
            log.DebugFormat("Start from {0}", configFile);

            if (config.PushPull != null)
            {
                log.InfoFormat("Starting listening for Pull: {0}", config.PushPull.ListenAddress);
                NetMQContext ctx1 = NetMQContext.Create();
                InitLinks(ctx1, _push_links, config.PushPull, ZmqSocketType.Push);
                puller = ctx1.CreatePullSocket();
                puller.ReceiveReady += this.PullerReceiveReady;
                puller.Bind(config.PushPull.ListenAddress);
                this.source_pp = new CancellationTokenSource();
                this.token_pp  = this.source_pp.Token;
                Task newTask = Task.Factory.StartNew(this.OnPull, this.token_pp);
                this.tasks.Add(newTask);
            }
            if (config.PubSub != null)
            {
                log.InfoFormat("Starting listening for subscriber: {0}", config.PubSub.ListenAddress);
                NetMQContext ctx2 = NetMQContext.Create();
                InitLinks(ctx2, _sub_links, config.PubSub, ZmqSocketType.Sub);
                foreach (var link in _sub_links)
                {
                    link.Value.socket.ReceiveReady += SubOnReceiveReady;
                }
                publisher = ctx2.CreatePublisherSocket();
                publisher.Bind(config.PubSub.ListenAddress);
                this.source_ps = new CancellationTokenSource();
                this.token_ps  = this.source_ps.Token;
                Task newTask = Task.Factory.StartNew(this.OnSubscribe, this.token_ps);
                this.tasks.Add(newTask);
            }
            if (config.ReqRep != null)
            {
                log.InfoFormat("Starting listening for Request: {0}", config.ReqRep.ListenAddress);
                NetMQContext ctx3 = NetMQContext.Create();
                //InitLinks(ctx3, _req_links, config.ReqRep, ZmqSocketType.Req); delete by xsw 2014-09-17
                responser = ctx3.CreateResponseSocket();
                responser.ReceiveReady += this.RepReceiveReady;
                responser.Bind(config.ReqRep.ListenAddress);
                this.source_rr = new CancellationTokenSource();
                this.token_rr  = this.source_rr.Token;
                Task newTask = Task.Factory.StartNew(this.OnResponse, this.token_rr);
                this.tasks.Add(newTask);
            }
        }
Ejemplo n.º 2
0
        public static ServiceConfig FromXml(string file)
        {
            ServiceConfig sc = new ServiceConfig();

            try
            {
                log.InfoFormat("Parsing '{0}' ...", file);
                XDocument doc    = XDocument.Load(file);
                var       root   = doc.Root;
                var       psNode = root.Element("ps");
                if (psNode != null)
                {
                    var PubSub = new QueueLinks();
                    sc.PubSub = PubSub;
                    var pubNode = psNode.Element("publisher");
                    PubSub.ListenAddress = pubNode.Attribute("address").Value;
                    foreach (var node in psNode.Element("subscribe").Elements())
                    {
                        XAttribute addAttr = node.Attribute("address");
                        PubSub.Links.Add(new LinkAddress {
                            Name = node.Attribute("name").Value, Address = addAttr != null ? addAttr.Value : null
                        });
                    }
                }
                var ppNode = root.Element("pp");
                if (ppNode != null)
                {
                    var pp = new QueueLinks();
                    sc.PushPull = pp;
                    var pullNode = ppNode.Element("pull");
                    pp.ListenAddress = pullNode.Attribute("address").Value;
                    foreach (var node in ppNode.Element("push").Elements())
                    {
                        XAttribute addAttr = node.Attribute("address");
                        pp.Links.Add(new LinkAddress {
                            Name = node.Attribute("name").Value, Address = addAttr != null ? addAttr.Value : null
                        });
                    }
                }

                var pairNode = root.Element("rr");
                if (pairNode != null)
                {
                    var rr = new QueueLinks();
                    sc.ReqRep = rr;
                    var repNode = pairNode.Element("response");
                    rr.ListenAddress = repNode.Attribute("address").Value;
                    foreach (var node in pairNode.Element("request").Elements())
                    {
                        XAttribute addAttr = node.Attribute("address");
                        rr.Links.Add(new LinkAddress {
                            Name = node.Attribute("name").Value, Address = addAttr != null ? addAttr.Value : null
                        });
                    }
                }

                var settingNode = root.Element("settings");
                if (settingNode != null)
                {
                    var settingsNodes = root.Element("settings").Elements();
                    foreach (var node in settingsNodes)
                    {
                        sc.Properties.Add(node.Attribute("key").Value, node.Attribute("value").Value);
                    }
                }
            }
            catch (Exception e)
            {
                log.Error(e);
            }
            return(sc);
        }