Example #1
0
    /// <summary>
    /// Creates a new component to be used.
    /// </summary>
    /// <param name="type"> Either a logic-gate-type or Custom</param>
    /// <param name="color"> Color used by the custom-type asset</param>
    /// <param name="name"> Name of the component</param>
    /// <param name="inputs">Number of inputs</param>
    /// <param name="outputs">Number of outputs</param>
    /// <param name="truthTable">Truth table of the component as a simple int array. Would be converted to a bool array later.</param>
    /// <returns></returns>
    public Component CreateComponent(Component.Type type, Color color, string name, int inputs = 0, int outputs = 0, ulong[] truthTable = null)
    {
        Component newComp = Instantiate(componentPrefab, this.transform).GetComponent <Component>();

        newComp.type = type;
        if (name == null)
        {
            newComp.name = newComp.compName = newComp.type.ToString();
            print("Name is null !");
        }
        else
        {
            newComp.name = newComp.compName = name;
        }
        if (color != new Color(-1f, -1f, -1f))
        {
            newComp.color = color;
        }
        else
        {
            newComp.color = Color.HSVToRGB((Mathf.Abs(newComp.name.GetHashCode()) % 512) / 512f, 0.9f, 0.7f);
        }
        newComp.inputs  = inputs;
        newComp.outputs = outputs;
        if (truthTable != null)
        {
            newComp.truthTable = truthTable;
        }
        else
        {
            print("Truth table is null !");
        }
        newComp.SetCorrectGateType((int)newComp.type);
        return(newComp);
    }
Example #2
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);
            }
        }
Example #3
0
        /// <summary>
        /// Registers a new component and sends the command to add a new queue to the camel instance
        /// </summary>
        /// <param name="c">Component to add</param>
        /// <returns>True on sucess</returns>
        public bool register(string instruction, string uri)
        {
            string type;

            try { type = instruction.Split(' ')[1]; }
            catch
            {
                type = instruction.Split(':')[1];
            }
            Component.Type t = Component.Type.GUI;
            switch (type)
            {
            case "broker":
                t = Component.Type.Broker;
                break;

            case "gui":
                t = Component.Type.GUI;
                break;

            case "solver":
                t = Component.Type.Solver;
                break;

            case "generator":
                t = Component.Type.Generator;
                break;
            }
            Component oldc = registered.Find(x => (x.uri == uri));

            if (oldc != null)
            {
                unregister(uri);
            }
            Component c = new Component(t, uri);

            configureRoute(c);
            registered.Add(c);
            Console.Out.WriteLine("Broker:\t New Component (" + c.type + ") registerd");
            //try to flush queue
            flushQueue(c);
            return(true);
        }