publish events to all clients connected over unix domain socket v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Thu, 14 Mar 2019 20:39:34 +0100
branchv_0
changeset 5 980a27d138f7
parent 4 a874deb6a536
child 6 f81d43b74209
publish events to all clients connected over unix domain socket
nbproject/configurations.xml
src/CMakeLists.txt
src/SimulatorSocketServer.cpp
src/SimulatorSocketServer.h
--- a/nbproject/configurations.xml	Thu Mar 14 17:41:43 2019 +0100
+++ b/nbproject/configurations.xml	Thu Mar 14 20:39:34 2019 +0100
@@ -113,10 +113,12 @@
               <pElem>/usr/include/x86_64-linux-gnu/qt5/QtGui</pElem>
               <pElem>/usr/include/x86_64-linux-gnu/qt5/QtCore</pElem>
               <pElem>/usr/lib/x86_64-linux-gnu/qt5/mkspecs/linux-g++</pElem>
+              <pElem>/usr/include/x86_64-linux-gnu/qt5/QtNetwork</pElem>
             </incDir>
             <preprocessorList>
               <Elem>QT_CORE_LIB</Elem>
               <Elem>QT_GUI_LIB</Elem>
+              <Elem>QT_NETWORK_LIB</Elem>
               <Elem>QT_WIDGETS_LIB</Elem>
             </preprocessorList>
           </ccTool>
@@ -146,9 +148,9 @@
           </incDir>
         </ccTool>
       </folder>
-      <item path="src/SimulatorSocketServer.cpp" ex="false" tool="1" flavor2="0">
-      </item>
-      <item path="src/SimulatorSocketServer.h" ex="false" tool="3" flavor2="0">
+      <item path="src/SimulatorSocketServer.cpp" ex="false" tool="1" flavor2="8">
+        <ccTool flags="0">
+        </ccTool>
       </item>
       <item path="src/SimulatorWindow.cpp" ex="false" tool="1" flavor2="8">
         <ccTool flags="0">
--- a/src/CMakeLists.txt	Thu Mar 14 17:41:43 2019 +0100
+++ b/src/CMakeLists.txt	Thu Mar 14 20:39:34 2019 +0100
@@ -21,7 +21,7 @@
 set(CMAKE_AUTOMOC ON) # Instruct CMake to run moc automatically when needed
 set(CMAKE_AUTOUIC ON) # Create code from a list of Qt designer ui files
 find_package(Qt5Widgets CONFIG REQUIRED) # Find the QtWidgets library
-# find_package(Qt5Charts CONFIG REQUIRED)
+find_package(Qt5Network CONFIG REQUIRED)
 
 # Add ASan AddressSanitizer
 set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -fno-omit-frame-pointer -fsanitize=address")
@@ -41,7 +41,7 @@
 # Link libraries:
 target_link_libraries(${EXECUTABLE_FILE} -lX11 -lm -lspnav)
 target_link_libraries(${EXECUTABLE_FILE} Qt5::Widgets)
-# target_link_libraries(${EXECUTABLE_FILE} Qt5::Charts)
+target_link_libraries(${EXECUTABLE_FILE} Qt5::Network)
 set_property(TARGET ${EXECUTABLE_FILE} PROPERTY INSTALL_RPATH_USE_LINK_PATH TRUE)
 
 install(TARGETS ${EXECUTABLE_FILE} DESTINATION bin)
--- a/src/SimulatorSocketServer.cpp	Thu Mar 14 17:41:43 2019 +0100
+++ b/src/SimulatorSocketServer.cpp	Thu Mar 14 20:39:34 2019 +0100
@@ -22,7 +22,28 @@
 #include "ButtonEvent.h"
 #include "MotionEvent.h"
 
+enum SocketEventType : int32_t {
+	Motion,
+	ButtonPress,
+	ButtonRelease
+};
+
 SimulatorSocketServer::SimulatorSocketServer() {
+	QString path = "/tmp/spacenav-simulator.sock";
+
+	bool listnening = server->listen(path);
+
+	if (listnening) {
+		std::wcout << L"Listening on: " << path.toStdWString() << std::endl;
+
+		connect(server, &QLocalServer::newConnection, [path, this]() {
+			std::wcout << L"New connection on: " << path.toStdWString() << std::endl;
+			connections.push_back(server->nextPendingConnection());
+		});
+
+	} else {
+		std::wcout << L"Unable to listen on: " << path.toStdWString() << std::endl; // TODO: throw exception
+	}
 }
 
 SimulatorSocketServer::SimulatorSocketServer(const SimulatorSocketServer& orig) {
@@ -32,19 +53,36 @@
 }
 
 void SimulatorSocketServer::publishButtonEvent(const ButtonEvent e) {
-	std::wcout << L"buttonEvent(" << e.number << L", " << (e.pressed ? L"pressed" : L"released") << L")" << std::endl;
+	std::array<int32_t, 8> data = {0};
+	data[0] = e.pressed ? SocketEventType::ButtonPress : SocketEventType::ButtonRelease;
+	data[1] = e.number;
+	publish(data);
+
+
 }
 
 void SimulatorSocketServer::publishMotionEvent(const MotionEvent e) {
-	std::wcout << L"motionEvent( m = ["
-			<< e.x << L", "
-			<< e.y << L", "
-			<< e.z << L"], "
-			<< L"r = ["
-			<< e.rx << L", "
-			<< e.ry << L", "
-			<< e.rz << L"], "
-			<< L"period = " << e.period
-			<< L")" << std::endl;
+	std::array<int32_t, 8> data = {0};
+	int i = 0;
+	data[i++] = SocketEventType::Motion;
+	data[i++] = e.x;
+	data[i++] = e.y;
+	data[i++] = e.z;
+	data[i++] = e.rx;
+	data[i++] = e.ry;
+	data[i++] = e.rz;
+	data[i++] = e.period;
+	publish(data);
+	// TODO: custom sensitivity for each connection
+}
 
+void SimulatorSocketServer::publish(const std::array<int32_t, 8> &data) {
+	for (QLocalSocket* socket : connections) {
+		if (socket->isValid()) {
+			socket->write((char*) &data, sizeof data);
+			socket->flush();
+		} else {
+			// TODO: remove invalid socket
+		}
+	}
 }
--- a/src/SimulatorSocketServer.h	Thu Mar 14 17:41:43 2019 +0100
+++ b/src/SimulatorSocketServer.h	Thu Mar 14 20:39:34 2019 +0100
@@ -17,7 +17,13 @@
  */
 #pragma once
 
+#include <vector>
+#include <array>
+#include <ctype.h>
+
 #include <QObject>
+#include <QLocalServer>
+#include <QLocalSocket>
 
 #include "MotionEvent.h"
 #include "ButtonEvent.h"
@@ -32,5 +38,7 @@
 	void publishMotionEvent(const MotionEvent e);
 	void publishButtonEvent(const ButtonEvent e);
 private:
-
+	QLocalServer* server = new QLocalServer(this);
+	std::vector<QLocalSocket*> connections;
+	void publish(const std::array<int32_t, 8> &data);
 };