예제 #1
0
    public void InitializeSubscriptionSocket(string topic)
    {
        if (!subscriptionSocketForTopic.ContainsKey(topic))
        {
            subscriptionSocketForTopic.Add(topic, new SubscriberSocket(IPHeader + subport));
            subscriptionSocketForTopic [topic].Subscribe(topic);

            //André: Is this necessary??
//			subscriptionSocketForTopic[topic].Options.SendHighWatermark = PupilSettings.numberOfMessages;// 6;

            subscriptionSocketForTopic[topic].ReceiveReady += (s, a) =>
            {
                int i = 0;

                NetMQMessage m = new NetMQMessage();

                while (a.Socket.TryReceiveMultipartMessage(ref m))
                {
                    // We read all the messages from the socket, but disregard the ones after a certain point
                    //				if ( i > PupilSettings.numberOfMessages ) // 6)
                    //					continue;

                    string msgType = m[0].ConvertToString();
                    mStream = new MemoryStream(m[1].ToByteArray());
                    byte[] thirdFrame = null;
                    if (m.FrameCount >= 3)
                    {
                        thirdFrame = m[2].ToByteArray();
                    }

                    if (PupilSettings.Instance.debug.printMessageType)
                    {
                        Debug.Log(msgType);
                    }

                    if (PupilSettings.Instance.debug.printMessage)
                    {
                        Debug.Log(MessagePackSerializer.ToJson(m[1].ToByteArray()));
                    }

                    if (PupilTools.ReceiveDataIsSet)
                    {
                        PupilTools.ReceiveData(msgType, MessagePackSerializer.Deserialize <Dictionary <string, object> > (mStream), thirdFrame);
                        continue;
                    }

                    switch (msgType)
                    {
                    case "notify.calibration.successful":
                        PupilTools.CalibrationFinished();
                        Debug.Log(msgType);
                        break;

                    case "notify.calibration.failed":
                        PupilTools.CalibrationFailed();
                        Debug.Log(msgType);
                        break;

                    case "gaze":
                    case "pupil.0":
                    case "pupil.1":
                        var dictionary = MessagePackSerializer.Deserialize <Dictionary <string, object> > (mStream);
                        var confidence = PupilTools.ConfidenceForDictionary(dictionary);
                        if (PupilTools.IsCalibrating)
                        {
                            string eyeID = PupilTools.EyeIDForDictionary(dictionary);
                            PupilTools.UpdateCalibrationMarkerColor(eyeID, confidence);
                            break;
                        }
                        if (confidence > 0.6f)
                        {
                            if (msgType == "gaze")
                            {
                                PupilTools.gazeDictionary = dictionary;
                            }
                            else if (msgType == "pupil.0")
                            {
                                PupilTools.pupil0Dictionary = dictionary;
                            }
                            else if (msgType == "pupil.1")
                            {
                                PupilTools.pupil1Dictionary = dictionary;
                            }
                        }
                        break;

                    case "frame.eye.0":
                    case "frame.eye.1":
                        break;

                    default:
                        Debug.Log(msgType);
                        //					foreach (var item in MessagePackSerializer.Deserialize<Dictionary<string,object>> (mStream))
                        //					{
                        //						Debug.Log(item.Key);
                        //						Debug.Log(item.Value.ToString());
                        //					}
                        break;
                    }

                    i++;
                }
            };
        }
    }