public void move(Pupil.SurfaceData3D data, string surface = "screen") { //Debug.Log("move"); Pupil.GazeOnSurface gaze = new Pupil.GazeOnSurface(); double maxConfidence = 0; foreach (Pupil.GazeOnSurface gos in data.gaze_on_srf) { if (gos.confidence > maxConfidence) { maxConfidence = gos.confidence; gaze = gos; } } if (!gaze.on_srf || !data.name.Equals(surface)) { if (gazeOnSurface == true) { gazeLeftSurface = Time.time; } gazeOnSurface = false; return; } else { if (GameController.Instance.joinedPlayersToPosition.Keys.Count < MazeGenerator.Instance.numPlayers) { processGaze(); } gazeOnSurface = true; gazeX = (float)gaze.norm_pos[0]; gazeY = (float)gaze.norm_pos[1]; } }
void Update() { if (calibrationDoneClient != null) { Calibration.Instance.CalibrationDone(calibrationDoneClient); calibrationDoneClient = null; } if (newData) { if (IPHeaders[turn] == null) { newData = false; surfaceData = null; return; } if (currentClient == null) { newData = false; surfaceData = null; return; } if (currentClient.gaze_controller != null) { if (surfaceData == null) { return; } foreach (Pupil.GazeOnSurface gos in surfaceData.gaze_on_srf) { if (gos == null) { return; } if (gos.norm_pos == null) { return; } if (gos.on_srf) { currentClient.gaze_controller.move(surfaceData, currentClient.surface_name); } } } newData = false; surfaceData = null; } }
void NetMQClient() { AsyncIO.ForceDotNet.Force(); NetMQConfig.ManualTerminationTakeOver(); NetMQConfig.ContextCreate(true); List <string> subports = new List <string>(); // subports for each client connection // loop through all clients and try to connect to them foreach (PupilConfiguration.PupilClient c in clients) { string subport = ""; string IPHeader = ">tcp://" + c.ip + ":"; bool frameReceived = false; Debug.LogFormat("Requesting socket for {0}:{1} ({2})", c.ip, c.port, c.name); RequestSocket requestSocket; try { // validate ip header if (!validateIPHeader(c.ip, c.port)) { Debug.LogWarningFormat("{0}:{1} is not a valid ip header for client {2}", c.ip, c.port, c.name); string failHeader = ""; subports.Add(failHeader); IPHeaders.Add(failHeader); c.is_connected = false; continue; } requestSocket = new RequestSocket(IPHeader + c.port); if (requestSocket != null) { requestSocket.SendFrame("SUB_PORT"); timeout = new System.TimeSpan(0, 0, 0, 100); frameReceived = requestSocket.TryReceiveFrameString(timeout, out subport); // request subport, will be saved in var subport for this client if (frameReceived) { if (c.initially_active) { subports.Add(subport); IPHeaders.Add(IPHeader); c.is_connected = true; } else { string failHeader = ""; subports.Add(failHeader); IPHeaders.Add(failHeader); c.is_connected = false; Debug.LogWarningFormat("Skipped connection to client {0}:{1} ({2})", c.ip, c.port, c.name); } } else { string failHeader = ""; subports.Add(failHeader); IPHeaders.Add(failHeader); c.is_connected = false; Debug.LogWarningFormat("Could not connect to client {0}:{1} ({2}). Make sure address is corect and pupil remote service is running", c.ip, c.port, c.name); } requestSockets.Add(requestSocket); } } catch (Exception e) { Debug.LogWarningFormat("Could not reach to client {0}:{1} ({2}): {4}", c.ip, c.port, c.name, e.ToString()); } } isConnected = true; // check if all clients are connected if (isConnected) { Debug.LogFormat("Connected to {0} sockets", IPHeaders.Count); foreach (String header in IPHeaders) { if (header.Equals("")) { subscriberSockets.Add(new SubscriberSocket()); continue; } else { SubscriberSocket subscriberSocket = new SubscriberSocket(header + subports[IPHeaders.IndexOf(header)]); if (clients[IPHeaders.IndexOf(header)].detect_surface) { subscriberSocket.Subscribe("surface"); } subscriberSocket.Subscribe("pupil."); subscriberSocket.Subscribe("notify."); //subscriberSocket.Subscribe("calibration."); //subscriberSocket.Subscribe("logging.info"); //subscriberSocket.Subscribe("calibration_routines.calibrate"); //subscriberSocket.Subscribe("frame."); //subscriberSocket.Subscribe("gaze."); subscriberSockets.Add(subscriberSocket); } } var msg = new NetMQMessage(); turn = 0; // used receive a message from each client in turn while (!stop_thread_) { if (IPHeaders.Count != clients.Count) { break; } turn = ++turn % IPHeaders.Count; if (IPHeaders[turn].Equals("") || clients[turn].is_connected == false) { continue; } timeout = new System.TimeSpan(0, 0, 0, 0, 1); // wait 200ms to receive a message bool stillAlive = subscriberSockets[turn].TryReceiveMultipartMessage(timeout, ref (msg)); if (stillAlive) { try { string msgType = msg[0].ConvertToString(); var message = MsgPack.Unpacking.UnpackObject(msg[1].ToByteArray()); MsgPack.MessagePackObject mmap = message.Value; if (msgType.Contains("pupil")) { // pupil detected lock (thisLock_) { pupilData = JsonUtility.FromJson <Pupil.PupilData3D>(mmap.ToString()); } } if (msgType.Contains("frame")) { } if (msgType.Contains("gaze")) { } if (msgType.Contains("surfaces")) { // surface detected lock (thisLock_) { if (!newData) { newData = true; surfaceData = JsonUtility.FromJson <Pupil.SurfaceData3D>(mmap.ToString()); currentClient = clients[turn]; } } } if (msgType.Equals("notify.calibration.started")) { //Debug.LogFormat("Calibration for client {0} started: {1}", clients[turn].name, mmap.ToString()); } if (msgType.Equals("notify.calibration.failed")) { calibrationDoneClient = clients[turn]; //Debug.LogFormat("Calibration for client {0} failed", clients[turn].name); } if (msgType.Equals("notify.calibration.successful")) { clients[turn].is_calibrated = true; calibrationDoneClient = clients[turn]; //Debug.LogFormat("Calibration for client {0} successful", clients[turn].name); } if (msgType.Equals("notify.calibration.calibration_data")) { //Debug.LogFormat("New calibration data for client {0}: {1}", clients[turn].name, mmap.ToString()); } if (msgType.Equals("logging.info")) { //Debug.LogFormat("logging info for client {0}: {1}", clients[turn].name, mmap.ToString()); } if (msgType.Equals("calibration_routines.calibrate")) { //Debug.LogFormat("Calibration info for client {0}: {1}", clients[turn].name, mmap.ToString()); } } catch { Debug.LogWarningFormat("Failed to deserialize pupil data for client {0}", clients[turn].name); } } } foreach (SubscriberSocket s in subscriberSockets) { s.Close(); } subscriberSockets.Clear(); } else { Debug.LogWarning("Failed to connect to all clients specified in config file"); } NetMQConfig.ContextTerminate(); }