summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAaron Seigo <aseigo@kde.org>2014-11-30 13:05:19 +0100
committerAaron Seigo <aseigo@kde.org>2014-11-30 13:05:19 +0100
commit97b79eeb86eedee57630b8d29f6eeab08ccb02b0 (patch)
treef1944c54ac2ab4650221974e81dc14809550cc7c
parent0bcfc57f24adf8ce8dfb2fad33b294b5f0110a89 (diff)
downloadsink-97b79eeb86eedee57630b8d29f6eeab08ccb02b0.tar.gz
sink-97b79eeb86eedee57630b8d29f6eeab08ccb02b0.zip
add flatbuffer support and use that for the handshake
-rw-r--r--CMakeLists.txt11
-rw-r--r--client/resourceaccess.cpp10
-rw-r--r--cmake/modules/FindFlatBuffers.cmake56
-rw-r--r--common/CMakeLists.txt1
-rw-r--r--common/commands/CMakeLists.txt1
-rw-r--r--common/commands/handshake.fbs9
-rw-r--r--resource/listener.cpp8
7 files changed, 90 insertions, 6 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index e0e3796..91c4568 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -1,5 +1,6 @@
1cmake_minimum_required(VERSION 2.8) 1cmake_minimum_required(VERSION 2.8)
2 2
3
3# ECM setup 4# ECM setup
4find_package(ECM 0.0.10 REQUIRED NO_MODULE) 5find_package(ECM 0.0.10 REQUIRED NO_MODULE)
5set(CMAKE_MODULE_PATH 6set(CMAKE_MODULE_PATH
@@ -18,11 +19,21 @@ include(ECMPackageConfigHelpers)
18# include(KDECMakeSettings) 19# include(KDECMakeSettings)
19 20
20find_package(Qt5Core REQUIRED) 21find_package(Qt5Core REQUIRED)
22find_package(FlatBuffers REQUIRED)
23function(generate_flatbuffers dest)
24 # TODO: move the file from ${fbs}_generated.h to just ${fbs}?
25 foreach(fbs ${ARGN})
26 execute_process(COMMAND ${FLATBUFFERS_FLATC_EXECUTABLE} -c -b --gen-includes -o ${dest} ${CMAKE_CURRENT_SOURCE_DIR}/${fbs})
27 endforeach(fbs)
28endfunction(generate_flatbuffers)
21 29
22set(CMAKE_AUTOMOC ON) 30set(CMAKE_AUTOMOC ON)
23add_definitions("-Wall -std=c++0x") 31add_definitions("-Wall -std=c++0x")
24include_directories(${CMAKE_SOURCE_DIR} ${CMAKE_BINARY_DIR}) 32include_directories(${CMAKE_SOURCE_DIR} ${CMAKE_BINARY_DIR})
25 33
34# common, eventually a lib but right now just the command buffers
35add_subdirectory(common)
36
26# the client 37# the client
27add_subdirectory(client) 38add_subdirectory(client)
28 39
diff --git a/client/resourceaccess.cpp b/client/resourceaccess.cpp
index 2972316..b0a26e7 100644
--- a/client/resourceaccess.cpp
+++ b/client/resourceaccess.cpp
@@ -2,6 +2,7 @@
2 2
3#include "common/console.h" 3#include "common/console.h"
4#include "common/commands.h" 4#include "common/commands.h"
5#include "common/commands/handshake_generated.h"
5 6
6#include <QDebug> 7#include <QDebug>
7#include <QProcess> 8#include <QProcess>
@@ -67,12 +68,15 @@ void ResourceAccess::connected()
67 Console::main()->log(QString("Connected: %1").arg(m_socket->fullServerName())); 68 Console::main()->log(QString("Connected: %1").arg(m_socket->fullServerName()));
68 69
69 { 70 {
70 const QByteArray name = QString::number((long long)this).toLatin1(); 71 flatbuffers::FlatBufferBuilder fbb;
72 auto name = fbb.CreateString("Client PID: " + QString::number((long long)this).toLatin1() + "!");
73 auto command = Toynadi::CreateHandshake(fbb, name);
74 Toynadi::FinishHandshakeBuffer(fbb, command);
71 const int commandId = Commands::HandshakeCommand; 75 const int commandId = Commands::HandshakeCommand;
72 const int dataSize = name.size(); 76 const int dataSize = fbb.GetSize();
73 m_socket->write((const char*)&commandId, sizeof(int)); 77 m_socket->write((const char*)&commandId, sizeof(int));
74 m_socket->write((const char*)&dataSize, sizeof(int)); 78 m_socket->write((const char*)&dataSize, sizeof(int));
75 m_socket->write(name.data(), name.size()); 79 m_socket->write((const char*)fbb.GetBufferPointer(), dataSize);
76 } 80 }
77 81
78 emit ready(true); 82 emit ready(true);
diff --git a/cmake/modules/FindFlatBuffers.cmake b/cmake/modules/FindFlatBuffers.cmake
new file mode 100644
index 0000000..0a54e2a
--- /dev/null
+++ b/cmake/modules/FindFlatBuffers.cmake
@@ -0,0 +1,56 @@
1# Copyright 2014 Stefan.Eilemann@epfl.ch
2# Copyright 2014 Google Inc. All rights reserved.
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16# Find the flatbuffers schema compiler
17#
18# Output Variables:
19# * FLATBUFFERS_FLATC_EXECUTABLE the flatc compiler executable
20# * FLATBUFFERS_FOUND
21#
22# Provides:
23# * FLATBUFFERS_GENERATE_C_HEADERS(Name <files>) creates the C++ headers
24# for the given flatbuffer schema files.
25# Returns the header files in ${Name}_OUTPUTS
26
27find_program(FLATBUFFERS_FLATC_EXECUTABLE NAMES flatc)
28find_path(FLATBUFFERS_INCLUDE_DIR NAMES flatbuffers/flatbuffers.h)
29
30include(FindPackageHandleStandardArgs)
31find_package_handle_standard_args(flatbuffers
32 DEFAULT_MSG FLATBUFFERS_FLATC_EXECUTABLE FLATBUFFERS_INCLUDE_DIR)
33
34if(FLATBUFFERS_FOUND)
35 function(FLATBUFFERS_GENERATE_C_HEADERS Name)
36 set(FLATC_OUTPUTS)
37 foreach(FILE ${ARGN})
38 get_filename_component(FLATC_OUTPUT ${FILE} NAME_WE)
39 set(FLATC_OUTPUT
40 "${CMAKE_CURRENT_BINARY_DIR}/${FLATC_OUTPUT}_generated.h")
41 list(APPEND FLATC_OUTPUTS ${FLATC_OUTPUT})
42
43 add_custom_command(OUTPUT ${FLATC_OUTPUT}
44 COMMAND ${FLATBUFFERS_FLATC_EXECUTABLE}
45 ARGS -c -o "${CMAKE_CURRENT_BINARY_DIR}/" ${FILE}
46 COMMENT "Building C++ header for ${FILE}"
47 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
48 endforeach()
49 set(${Name}_OUTPUTS ${FLATC_OUTPUTS} PARENT_SCOPE)
50 endfunction()
51
52 set(FLATBUFFERS_INCLUDE_DIRS ${FLATBUFFERS_INCLUDE_DIR})
53 include_directories(${CMAKE_BINARY_DIR})
54else()
55 set(FLATBUFFERS_INCLUDE_DIR)
56endif()
diff --git a/common/CMakeLists.txt b/common/CMakeLists.txt
new file mode 100644
index 0000000..2ceacf8
--- /dev/null
+++ b/common/CMakeLists.txt
@@ -0,0 +1 @@
add_subdirectory(commands)
diff --git a/common/commands/CMakeLists.txt b/common/commands/CMakeLists.txt
new file mode 100644
index 0000000..2a9ba64
--- /dev/null
+++ b/common/commands/CMakeLists.txt
@@ -0,0 +1 @@
generate_flatbuffers(${CMAKE_CURRENT_BINARY_DIR} handshake.fbs)
diff --git a/common/commands/handshake.fbs b/common/commands/handshake.fbs
new file mode 100644
index 0000000..4492ca9
--- /dev/null
+++ b/common/commands/handshake.fbs
@@ -0,0 +1,9 @@
1
2namespace Toynadi;
3
4table Handshake {
5 name: string;
6}
7
8root_type Handshake;
9
diff --git a/resource/listener.cpp b/resource/listener.cpp
index da1edc3..645cb8c 100644
--- a/resource/listener.cpp
+++ b/resource/listener.cpp
@@ -2,6 +2,7 @@
2 2
3#include "common/console.h" 3#include "common/console.h"
4#include "common/commands.h" 4#include "common/commands.h"
5#include "common/commands/handshake_generated.h"
5 6
6#include <QLocalSocket> 7#include <QLocalSocket>
7#include <QTimer> 8#include <QTimer>
@@ -130,11 +131,12 @@ void Listener::processClientBuffer(Client &client)
130 client.commandBuffer.remove(0, headerSize + size); 131 client.commandBuffer.remove(0, headerSize + size);
131 132
132 switch (commandId) { 133 switch (commandId) {
133 case Commands::HandshakeCommand: 134 case Commands::HandshakeCommand: {
134 client.name = data; 135 auto buffer = Toynadi::GetHandshake(data.constData());
135 Console::main()->log(QString(" Handshake from %1").arg(client.name)); 136 Console::main()->log(QString(" Handshake from %1").arg(buffer->name()->c_str()));
136 //TODO: reply? 137 //TODO: reply?
137 break; 138 break;
139 }
138 default: 140 default:
139 // client.hasSentCommand = true; 141 // client.hasSentCommand = true;
140 break; 142 break;