Beispiel #1
0
        private void SocketReceiveReady(object sender, NetMQSocketEventArgs e)
        {
            var hasMore = false;
            var request = string.Empty;

            lock (_socketLock)
            {
                var receiveResult = _socket?.TryReceiveFrameString(out request, out hasMore);

                if (!receiveResult.HasValue || !receiveResult.Value || string.IsNullOrEmpty(request))
                {
                    return;
                }
            }

            // If the request is for a search, receive the instrument w/ the search parameters and pass it to the searcher
            if (request == MessageType.Search && hasMore)
            {
                HandleSearchRequest();
            }
            else if (request == MessageType.PredicateSearch && hasMore)
            {
                HandlePredicateSearchRequest();
            }
            else if (request == MessageType.AllInstruments) // If the request is for all the instruments, we don't need to receive anything else
            {
                HandleAllInstrumentsRequest();
            }
            else if (request == MessageType.AddInstrument && hasMore) // Request to add instrument
            {
                HandleInstrumentAdditionRequest();
            }
        }
Beispiel #2
0
    void Update()
    {
        //sender.TrySendFrame("work");
        //string temp;
        //if (receiver.TryReceiveFrameString(out temp)) {
        //    Debug.Log(temp);
        //}
        string temp2;

        if (response.TryReceiveFrameString(out temp2))
        {
            Debug.Log(temp2);
            response.TrySendFrame("ok");
        }
    }
Beispiel #3
0
        private void SocketReceiveReady(object sender, NetMQSocketEventArgs e)
        {
            var hasMore = false;
            var request = string.Empty;

            lock (_socketLock) {
                var receiveResult = _socket?.TryReceiveFrameString(out request, out hasMore);

                if (!receiveResult.HasValue || !receiveResult.Value || string.IsNullOrEmpty(request))
                {
                    return;
                }
            }

            var instruments = new List <Instrument>();

            using (var ms = new MemoryStream()) {
                // If the request is for a search, receive the instrument w/ the search parameters and pass it to the searcher
                if (request == "SEARCH" && hasMore)
                {
                    var buffer           = _socket.ReceiveFrameBytes();
                    var searchInstrument = MyUtils.ProtoBufDeserialize <Instrument>(buffer, ms);

                    _logger.Info($"Instruments Server: Received search request: {searchInstrument}");

                    try {
                        instruments = _instrumentManager.FindInstruments(null, searchInstrument);
                    }
                    catch (Exception ex) {
                        _logger.Error($"Instruments Server: Instrument search error: {ex.Message}");
                    }
                }
                else if (request == "ALL") // If the request is for all the instruments, we don't need to receive anything else
                {
                    _logger.Info("Instruments Server: received request for list of all instruments.");

                    try {
                        instruments = _instrumentManager.FindInstruments();
                    }
                    catch (Exception ex) {
                        _logger.Error($"Instruments Server: Instrument search error: {ex.Message}");
                    }
                }
                else if (request == "ADD" && hasMore) // Request to add instrument
                {
                    var buffer     = _socket.ReceiveFrameBytes();
                    var instrument = MyUtils.ProtoBufDeserialize <Instrument>(buffer, ms);

                    _logger.Info($"Instruments Server: Received instrument addition request. Instrument: {instrument}");

                    Instrument addedInstrument;

                    try {
                        addedInstrument = _instrumentManager.AddInstrument(instrument);
                    }
                    catch (Exception ex) {
                        addedInstrument = null;

                        _logger.Error($"Instruments Server: Instrument addition error: {ex.Message}");
                    }

                    _socket.SendMoreFrame(addedInstrument != null ? "SUCCESS" : "FAILURE");

                    _socket.SendFrame(MyUtils.ProtoBufSerialize(addedInstrument, ms));

                    return;
                }
                else // No request = loop again
                {
                    return;
                }

                var uncompressed = MyUtils.ProtoBufSerialize(instruments, ms); // Serialize the list of instruments

                ms.Read(uncompressed, 0, (int)ms.Length);                      // Get the uncompressed data

                var result = LZ4Codec.Encode(uncompressed, 0, (int)ms.Length); // Compress it
                // Before we send the result we must send the length of the uncompressed array, because it's needed for decompression
                _socket.SendMoreFrame(BitConverter.GetBytes(uncompressed.Length));
                // Then finally send the results
                _socket.SendFrame(result);
            }
        }
Beispiel #4
0
        public static MmiMessage ReceiveMessageAndData(NetMQSocket socket)
        {
            MmiMessage message;

            lock (socket)
            {
                string json;
                bool more;
                var msg = new Msg();
                msg.InitEmpty();
                if (!socket.TryReceiveFrameString(Timeout, out json, out more))
                {
                    throw new NetMQException("Timeout during receive");
                }

                message = new MmiMessage { JsonString = json };
                message.FillFromJson(json);

                // receive data
                if (socket.HasIn)
                {
                    byte[] bytes;
                    if (!socket.TryReceiveFrameBytes(Timeout, out bytes))
                    {
                        throw new NetMQException("Timeout during receive bytes");
                    }

                    message.Values = BytesToArray(bytes, message.DataType, message.Shape);
                }
            }

            return message;
        }
    // Update is called once per frame
    //
    void Update()
    {
        AsyncIO.ForceDotNet.Force();
        string top;
        string message;

        if (client.TryReceiveFrameString(out top))
        {
            if (client.TryReceiveFrameString(out message))
            {
                Debug.Log(message);
                string[] coord = message.Split();
                transX = int.Parse(coord [0]);
                transY = int.Parse(coord [1]);
            }
        }
        if (Input.GetKeyDown(KeyCode.N))
        {
            calCount++;
        }
        if (Input.GetKeyDown(KeyCode.C))
        {
            if (isVisible)
            {
                isVisible = false;
                targetPrefab.SetActive(false);
            }
            else
            {
                isVisible = true;
                targetPrefab.SetActive(true);
                server.SendMore("coord").Send("1180 564");
            }
        }

        if (calCount != curCount)
        {
            calCount = calCount % 10;
            curCount = calCount;

            gameObject.GetComponent <Image> ().sprite = calImages [calCount];
        }
        tr  = GameObject.FindGameObjectWithTag("RightEyeCamera").transform;
        vec = (tr.forward) + (tr.right * ((transX - 590) / 1000)) + (tr.up * (((-transY) + 282) / 1000));
        //vec = (tr.forward) + (tr.right * (transX)) + (tr.up * ((transY)));
        Vector3    orig = GameObject.FindGameObjectWithTag("RightEyeCamera").transform.position;
        RaycastHit hit;
        Ray        ray = new Ray(orig, vec);

        //Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        if (isVisible)
        {
            if (Physics.Raycast(ray, out hit, 100.0f))
            {
                //Debug.DrawLine (ray.origin, hit.point);
                targetPrefab.transform.position = hit.point;
                targetPrefab.transform.rotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
                targetPrefab.SetActive(true);
            }
            else
            {
                targetPrefab.SetActive(false);
            }
        }
    }
    // Update is called once per frame
    //
    void Update()
    {
        AsyncIO.ForceDotNet.Force();
        string top;
        string message;

        if (client.TryReceiveFrameString(out top))
        {
            if (client.TryReceiveFrameString(out message))
            {
                Debug.Log(message);
                string[] coord = message.Split();
                transX     = int.Parse(coord [0]);
                transY     = int.Parse(coord [1]);
                calibrated = true;
            }
        }
        if (Input.GetKeyDown(KeyCode.Space) || Input.GetKeyDown(KeyCode.JoystickButton0))
        {
            calCount++;
            textTime = 5.0f;
            if (calCount != 1)
            {
                calibrator.Send("Calibrate");
            }
            else
            {
                calibrated   = false;
                trackingMode = 0;
                targetPrefab.SetActive(false);
            }
            if (calCount == 10)
            {
                calibrated = true;
                targetPrefab.SetActive(true);
                uiText.text  = "Press A to recalibrate" + Environment.NewLine + "Press B to toggle eye tracking modes";
                textTime     = 0.0f;
                trackingMode = 1;
            }
        }
        if (Input.GetKeyDown(KeyCode.C) || Input.GetKeyDown(KeyCode.JoystickButton1))
        {
            if (calibrated)
            {
                if (trackingMode == 0)
                {
                    targetPrefab.SetActive(true);
                }
                if (trackingMode > 0)
                {
                    targetPrefab.SetActive(false);
                }
                trackingMode = (trackingMode + 1) % 3;
            }
            else if (calCount == 0)
            {
                //change gui
                uiText.text = "Press A to begin calibration";
                textTime    = 0.0f;
            }
        }

        if (calCount != curCount)
        {
            calCount = calCount % 10;
            curCount = calCount;

            gameObject.GetComponent <Image> ().sprite = calImages [calCount];
        }
        if (trackingMode == 0 && calibrated)
        {
            gameObject.GetComponent <Image> ().sprite = calImages [0];
        }
        if (trackingMode == 1)
        {
            tr  = GameObject.FindGameObjectWithTag("MainCamera").transform;
            vec = (tr.forward) + (tr.right * ((transX - 590) / 1000)) + (tr.up * (((-transY) + 282) / 1000));
            //vec = (tr.forward) + (tr.right * (transX)) + (tr.up * ((transY)));
            Vector3    orig = GameObject.FindGameObjectWithTag("MainCamera").transform.position;
            RaycastHit hit;
            Ray        ray = new Ray(orig, vec);
            //Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            if (Physics.Raycast(ray, out hit, 100.0f))
            {
                //Debug.DrawLine (ray.origin, hit.point);
                targetPrefab.transform.position = hit.point;
                targetPrefab.transform.rotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
                targetPrefab.SetActive(true);
            }
            else
            {
                targetPrefab.SetActive(false);
            }
        }
        else if (trackingMode == 2)
        {
            int sectionIndex = 0;
            if (transX > 368)
            {
                sectionIndex++;
                if (transX > 810)
                {
                    sectionIndex++;
                }
            }
            if (transY > 163)
            {
                sectionIndex += 3;
                if (transY > 400)
                {
                    sectionIndex += 3;
                }
            }
            gameObject.GetComponent <Image> ().sprite = secImages [sectionIndex];
        }
        if (textTime >= 2.0)
        {
            uiText.text = "";
        }
        else
        {
            textTime += Time.deltaTime;
        }
    }