public void CreateContext() { NetMQConfig.ContextCreate(); var isTerminated = VerifyTermination(); Assert.AreEqual(false, isTerminated); }
// Client thread which does not block Update() void NetMQClient() { AsyncIO.ForceDotNet.Force(); NetMQConfig.ManualTerminationTakeOver(); NetMQConfig.ContextCreate(true); //string msg; var timeout = new System.TimeSpan(0, 0, 1); //1sec Debug.Log("Connect to the server."); pairSocket = new NetMQ.Sockets.PairSocket(); pairSocket.Options.ReceiveHighWatermark = 0; //pairSocket.Connect("tcp://192.168.1.122:55555"); pairSocket.Connect("tcp://192.168.1.111:55555"); is_connected = true; while (is_connected && stop_thread_ == false) { is_connected = pairSocket.TryReceiveFrameString(timeout, out msg); pairSocket.TrySendFrame(timeout, final); } pairSocket.Close(); Debug.Log("ContextTerminate."); NetMQConfig.ContextTerminate(); NetMQConfig.Cleanup(); }
public Pupil(string IP = "127.0.0.1", string PORT = "50020") { NetMQConfig.ContextCreate(true); requestSocket = new RequestSocket(">tcp://" + IP + ":" + PORT); float t = Time.time; requestSocket.SendFrame("t"); string response = requestSocket.ReceiveFrameString(); requestSocket.SendFrame("T 0.0"); response = requestSocket.ReceiveFrameString(); SetTimestamp(Time.time); }
// Client thread which does not block Update() void NetMQServer() { AsyncIO.ForceDotNet.Force(); NetMQConfig.ManualTerminationTakeOver(); NetMQConfig.ContextCreate(true); string msg; var timeout = new System.TimeSpan(0, 0, 1); //1sec Log("Connect."); var socket = new ResponseSocket("@tcp://localhost:5557"); while (true) { lock (thisLock_) { if (stop_thread_) { break; } } // Log("Get request."); try { if (socket.TryReceiveFrameString(timeout, out msg)) { Log("recived: " + msg); socket.SendFrame("world"); } else { Log("Timed out, sleep"); Thread.Sleep(1000); } } catch (System.Exception ex) { Log(ex.Message); throw ex; } } socket.Close(); Log("ContextTerminate."); NetMQConfig.ContextTerminate(); }
private TimeSpan timeout = new System.TimeSpan(0, 0, 1); //1sec public void InitializeRequestSocket() { IPHeader = ">tcp://" + IP + ":"; Debug.Log("Attempting to connect to : " + IPHeader + PORT); if (!contextExists) { AsyncIO.ForceDotNet.Force(); NetMQConfig.ManualTerminationTakeOver(); NetMQConfig.ContextCreate(true); contextExists = true; } requestSocket = new RequestSocket(PupilTools.Settings.connection.IPHeader + PupilTools.Settings.connection.PORT); requestSocket.SendFrame("SUB_PORT"); isConnected = requestSocket.TryReceiveFrameString(timeout, out PupilTools.Settings.connection.subport); CheckPupilVersion(); }
public void InitializeRequestSocket() { Settings.IPHeader = ">tcp://" + Settings.IP + ":"; Debug.Log("Attempting to connect to : " + Settings.IPHeader + Settings.PORT); if (!_contextExists) { AsyncIO.ForceDotNet.Force(); NetMQConfig.ManualTerminationTakeOver(); NetMQConfig.ContextCreate(true); _contextExists = true; } RequestSocket = new RequestSocket(Settings.IPHeader + Settings.PORT); RequestSocket.SendFrame("SUB_PORT"); IsConnected = RequestSocket.TryReceiveFrameString(_timeout, out Settings.Subport); if (!IsConnected) { return; } GetAndSavePupilVersion(); }
// Client thread which does not block Update() void NetMQClient() { AsyncIO.ForceDotNet.Force(); NetMQConfig.ManualTerminationTakeOver(); NetMQConfig.ContextCreate(true); //string msg; var timeout = new System.TimeSpan(0, 0, 1); //1sec Debug.Log("Connect to the server."); var subSocket = new SubscriberSocket(); subSocket.Options.ReceiveHighWatermark = 0; subSocket.Connect("tcp://192.168.1.122:55555"); subSocket.Subscribe(""); bool is_connected = true; while (is_connected && stop_thread_ == false) { is_connected = subSocket.TryReceiveFrameString(timeout, out msg); //Debug.Log(msg); //byte[] decodedBytes = System.Convert.FromBase64String(msg); //Debug.Log(decodedBytes); //string decodedText = Encoding.UTF8.GetString(decodedBytes); //Debug.Log(decodedText); //Mat buffer = new Mat(decodedText, ImreadModes.Unchanged); //image = Cv2.ImDecode(buffer, ImreadModes.Unchanged); //using (var window = new Window("window", image: src, flags: WindowMode.AutoSize)) { //Cv2.WaitKey(); //} //Debug.Log(src); } subSocket.Close(); Debug.Log("ContextTerminate."); NetMQConfig.ContextTerminate(); NetMQConfig.Cleanup(); }
public void CycleCreateTerminate() { NetMQConfig.ContextCreate(true); var isTerminated = VerifyTermination(); Assert.AreEqual(false, isTerminated); // We use the Poller Test code. using (var rep = new ResponseSocket()) using (var req = new RequestSocket()) using (var poller = new NetMQPoller { rep }) { var port = rep.BindRandomPort("tcp://127.0.0.1"); req.Connect("tcp://127.0.0.1:" + port); rep.ReceiveReady += (s, e) => { bool more; Assert.AreEqual("Hello", e.Socket.ReceiveFrameString(out more)); Assert.False(more); e.Socket.SendFrame("World"); }; poller.RunAsync(); req.SendFrame("Hello"); bool more2; Assert.AreEqual("World", req.ReceiveFrameString(out more2)); Assert.IsFalse(more2); poller.Stop(); } NetMQConfig.ContextTerminate(); isTerminated = VerifyTermination(); Assert.AreEqual(true, isTerminated); }
public void TryToConnect() { IPHeader = ">tcp://" + IP + ":"; Debug.Log("Attempting to connect to : " + IPHeader + PORT); var timeout = new System.TimeSpan(0, 0, 1); //1sec if (!contextExists) { AsyncIO.ForceDotNet.Force(); NetMQConfig.ManualTerminationTakeOver(); NetMQConfig.ContextCreate(true); contextExists = true; } requestSocket = new RequestSocket(PupilSettings.Instance.connection.IPHeader + PupilSettings.Instance.connection.PORT); requestSocket.SendFrame("SUB_PORT"); isConnected = requestSocket.TryReceiveFrameString(timeout, out PupilSettings.Instance.connection.subport); }
void NetMQClient() { //thanks for Yuta Itoh sample code to connect via NetMQ with Pupil Service string IPHeader = ">tcp://" + ServerIP + ":"; var timeout = new System.TimeSpan(0, 0, 1); //1sec // Necessary to handle this NetMQ issue on Unity editor // https://github.com/zeromq/netmq/issues/526 AsyncIO.ForceDotNet.Force(); NetMQConfig.ManualTerminationTakeOver(); NetMQConfig.ContextCreate(true); string subport = ""; Debug.Log("Connect to the server: " + IPHeader + ServicePort + "."); _requestSocket = new RequestSocket(IPHeader + ServicePort); _requestSocket.SendFrame("SUB_PORT"); _isconnected = _requestSocket.TryReceiveFrameString(timeout, out subport); _lastT = DateTime.Now; if (_isconnected) { StartProcess(); var subscriberSocket = new SubscriberSocket(IPHeader + subport); subscriberSocket.Subscribe("gaze"); //subscribe for gaze data subscriberSocket.Subscribe("notify."); //subscribe for all notifications _setStatus(EStatus.ProcessingGaze); var msg = new NetMQMessage(); while (_isDone == false) { _isconnected = subscriberSocket.TryReceiveMultipartMessage(timeout, ref (msg)); if (_isconnected) { try { string msgType = msg[0].ConvertToString(); //Debug.Log(msgType); if (msgType == "gaze") { var message = MsgPack.Unpacking.UnpackObject(msg[1].ToByteArray()); MsgPack.MessagePackObject mmap = message.Value; lock (_dataLock) { _pupilData = JsonUtility.FromJson <Pupil.PupilData3D>(mmap.ToString()); if (_pupilData.confidence > 0.5f) { OnPacket(_pupilData); } } } //Debug.Log(message); } catch { // Debug.Log("Failed to unpack."); } } else { // Debug.Log("Failed to receive a message."); Thread.Sleep(500); } } StopProcess(); subscriberSocket.Close(); } else { Debug.Log("Failed to connect the server."); } _requestSocket.Close(); // Necessary to handle this NetMQ issue on Unity editor // https://github.com/zeromq/netmq/issues/526 Debug.Log("ContextTerminate."); NetMQConfig.ContextTerminate(); }
void NetMQClient() { //thanks for Yuta Itoh sample code to connect via NetMQ with Pupil Service string IPHeader = ">tcp://" + ServerIP + ":"; var timeout = new System.TimeSpan(0, 0, 1); //1sec // Necessary to handle this NetMQ issue on Unity editor // https://github.com/zeromq/netmq/issues/526 AsyncIO.ForceDotNet.Force(); NetMQConfig.ManualTerminationTakeOver(); NetMQConfig.ContextCreate(true); string subport = ""; print("Connect to the server: " + IPHeader + ServicePort + "."); Thread.Sleep(ServiceStartupDelay); _requestSocket = new RequestSocket(IPHeader + ServicePort); _requestSocket.SendFrame("SUB_PORT"); _isconnected = _requestSocket.TryReceiveFrameString(timeout, out subport); print(_isconnected + " isconnected"); _gazeFps.Reset(); _eyeFps [0].Reset(); _eyeFps [1].Reset(); if (_isconnected) { //_serviceStarted = true; StartProcess(); var subscriberSocket = new SubscriberSocket(IPHeader + subport); subscriberSocket.Subscribe("gaze"); //subscribe for gaze data subscriberSocket.Subscribe("notify."); //subscribe for all notifications _setStatus(EStatus.ProcessingGaze); var msg = new NetMQMessage(); while (_isDone == false) { _isconnected = subscriberSocket.TryReceiveMultipartMessage(timeout, ref (msg)); if (_isconnected) { try { string msgType = msg[0].ConvertToString(); UnityEngine.Debug.Log(msgType); if (msgType == "gaze") { var message = MsgPack.Unpacking.UnpackObject(msg[1].ToByteArray()); MsgPack.MessagePackObject mmap = message.Value; lock (_dataLock) { _pupilData = JsonUtility.FromJson <Pupil.PupilData3D>(mmap.ToString()); if (_pupilData.confidence > 0.5f) { //UnityEngine.Debug.Log(_pupilData.base_data[0].id); OnPacket(_pupilData); } } } else if (msgType == "notify.eye_process.started") { var message = MsgPack.Unpacking.UnpackObject(msg[1].ToByteArray()); MsgPack.MessagePackObject mmap = message.Value; var id = JsonUtility.FromJson <Pupil.EyeStatus>(mmap.ToString()); UnityEngine.Debug.Log(id.eye_id); } //Debug.Log(message); } catch { // Debug.Log("Failed to unpack."); } } else { print("Failed to receive a message."); Thread.Sleep(500); } } StopProcess(); subscriberSocket.Close(); } else { print("Failed to connect the server."); //If needed here could come a retry connection. } //Can only send request via IPC if the connection has been established, otherwise we are facing, errors and potential freezing. if (_serviceStarted && _isconnected) { StopService(); } //Kill process if (serviceProcess != null) { UnityEngine.Debug.Log("Killing Pupil service"); serviceProcess.Kill(); serviceProcess.Close(); } _requestSocket.Close(); // Necessary to handle this NetMQ issue on Unity editor // https://github.com/zeromq/netmq/issues/526 print("ContextTerminate."); NetMQConfig.ContextTerminate(); }
// Use this for initialization void Start() { AsyncIO.ForceDotNet.Force(); NetMQConfig.ManualTerminationTakeOver(); NetMQConfig.ContextCreate(true); var _server_thread = new Thread(() => { Log("Start server."); var _server = new ReliableServer("tcp://*:6669"); var index = 0; while (true) { lock (_threadLock) { if (_endThread) { break; } } var message = new NetMQMessage(); message.Append("A"); message.Append("hello: " + index); _server.Publish(message); Log("publishing: " + message); Thread.Sleep(100); index++; } Log("close server."); _server.Dispose(); }); _server_thread.Start(); var _client_thread = new Thread(() => { Log("Start client."); var _client = new ReliableClient("tcp://localhost:6669"); _client.Subscribe("A"); while (true) { lock (_threadLock) { if (_endThread) { break; } } NetMQMessage msg = null; msg = _client.ReceiveMessage(); if (msg != null) { var topic = msg.Pop().ConvertToString(); var message = msg.Pop().ConvertToString(); var output = String.Format("reviced topic: {0}, message: {1}", topic, message); Log(output); } Thread.Sleep(10); } Log("close client."); _client.Dispose(); }); _client_thread.Start(); }
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(); }
// Client thread which does not block Update() void NetMQClient() { string IPHeader = ">tcp://" + IP + ":"; var timeout = new System.TimeSpan(0, 0, 1); //1sec // Necessary to handle this NetMQ issue on Unity editor // https://github.com/zeromq/netmq/issues/526 AsyncIO.ForceDotNet.Force(); NetMQConfig.ManualTerminationTakeOver(); NetMQConfig.ContextCreate(true); string subport = ""; Debug.Log("Connect to the server: " + IPHeader + PORT + "."); var requestSocket = new RequestSocket(IPHeader + PORT); double t = 0; const int N = 1000; bool is_connected = false; for (int k = 0; k < N; k++) { var sw = new System.Diagnostics.Stopwatch(); sw.Start(); requestSocket.SendFrame("SUB_PORT"); is_connected = requestSocket.TryReceiveFrameString(timeout, out subport); sw.Stop(); t = t + sw.Elapsed.Milliseconds; //Debug.Log("Round trip time:" + sw.Elapsed + "[sec]."); if (is_connected == false) { break; } } Debug.Log("Round trip average time:" + t / N + "[msec]."); requestSocket.Close(); if (is_connected) { // var subscriberSocket = new SubscriberSocket(IPHeader + subport); subscriberSocket.Subscribe(ID); var msg = new NetMQMessage(); while (is_connected && stop_thread_ == false) { Debug.Log("Receive a multipart message."); is_connected = subscriberSocket.TryReceiveMultipartMessage(timeout, ref (msg)); if (is_connected) { Debug.Log("Unpack a received multipart message."); try { //Debug.Log(msg[0].ConvertToString()); var message = MsgPack.Unpacking.UnpackObject(msg[1].ToByteArray()); MsgPack.MessagePackObject mmap = message.Value; lock (thisLock_) { data_ = JsonUtility.FromJson <Pupil.PupilData3D>(mmap.ToString()); } //Debug.Log(message); } catch { Debug.Log("Failed to unpack."); } } else { Debug.Log("Failed to receive a message."); Thread.Sleep(1000); } } subscriberSocket.Close(); } else { Debug.Log("Failed to connect the server."); } // Necessary to handle this NetMQ issue on Unity editor // https://github.com/zeromq/netmq/issues/526 Debug.Log("ContextTerminate."); NetMQConfig.ContextTerminate(); }
// Client thread which does not block Update() void NetMQClient() { AsyncIO.ForceDotNet.Force(); NetMQConfig.ManualTerminationTakeOver(); NetMQConfig.ContextCreate(true); string msg; var timeout = new System.TimeSpan(0, 0, 1); //1sec Thread.Sleep(250); Log("Connect."); var socket = new RequestSocket(">tcp://localhost:5557"); bool trySendPhase = true; while (true) { lock (thisLock_) { if (stop_thread_) { break; } } // Log("Send Request."); try { if (trySendPhase) { if (socket.TrySendFrame(timeout, "hello")) { trySendPhase = !trySendPhase; } else { Log("TrySend timeout, sleep"); Thread.Sleep(1000); } } else { if (socket.TryReceiveFrameString(timeout, out msg)) { Log("recived: " + msg); trySendPhase = !trySendPhase; } else { Log("TryRecieve timeout, sleep"); Thread.Sleep(1000); trySendPhase = !trySendPhase; } } } catch (System.Exception ex) { Log(ex.Message); trySendPhase = !trySendPhase; //throw ex; } } socket.Close(); Log("ContextTerminate."); NetMQConfig.ContextTerminate(); }