Example #1
0
        public Component sendMessage(Message m, Component.Type recipientType)
        {
            String json = JsonConvert.SerializeObject(m);

            json = json.Replace("request_id", "request-id");
            byte[] body = System.Text.Encoding.Default.GetBytes(json);
            if (recipientType == Component.Type.ALL)
            {
                foreach (Component c in registered)
                {
                    rqModel.BasicPublish(OUTEXCHANGE, c.id.ToString(), false, rqPPersistent, body);
                    Console.WriteLine("S1: Send to " + c.id);
                }

                return(null);
            }
            else if (recipientType == Component.Type.ALLEXCEPTBROKER)
            {
                foreach (Component c in registered)
                {
                    if (c.type != Component.Type.Broker)
                    {
                        rqModel.BasicPublish(OUTEXCHANGE, c.id.ToString(), false, rqPPersistent, body);
                    }
                }
                Console.Out.WriteLine("Send:\t To all except broker:\t" + json);
                return(null);
            }
            //try to find idling component
            else
            {
                Component c = registered.Find(x => (x.type == recipientType && !x.busy));
                //try to find a fitting (but busy) component
                if (c == null)
                {
                    c = registered.Find(x => (x.type == recipientType));
                }
                if (c != null)
                {
                    json = JsonConvert.SerializeObject(m);
                    json = json.Replace("request_id", "request-id");
                    body = System.Text.Encoding.Default.GetBytes(json);
                    rqModel.BasicPublish(OUTEXCHANGE, c.id.ToString(), false, rqPPersistent, body);
                    Console.Out.WriteLine("Send:\t To " + recipientType.ToString() + ":\t" + json);
                }
                return(c);
            }
        }