diff options
author | Добрый Ээх <bukharaev@gmail.com> | 2016-10-07 17:17:03 +0300 |
---|---|---|
committer | Добрый Ээх <bukharaev@gmail.com> | 2016-10-13 21:25:22 +0300 |
commit | a65d86ebcf216bd0480c7f576d0eff60df44a160 (patch) | |
tree | 0ba4b8802b4d60179190523d327b2246966fc1b8 /platform | |
parent | a8067e8826da51278fa78ce652b80cec2848536b (diff) |
gps tracking reporter
Diffstat (limited to 'platform')
-rw-r--r-- | platform/platform.pro | 1 | ||||
-rw-r--r-- | platform/socket.cpp | 122 | ||||
-rw-r--r-- | platform/socket.hpp | 18 |
3 files changed, 140 insertions, 1 deletions
diff --git a/platform/platform.pro b/platform/platform.pro index d05cc1f1e4..c6fb2fd10e 100644 --- a/platform/platform.pro +++ b/platform/platform.pro @@ -105,3 +105,4 @@ SOURCES += \ preferred_languages.cpp \ servers_list.cpp \ settings.cpp \ + socket.cpp \ diff --git a/platform/socket.cpp b/platform/socket.cpp new file mode 100644 index 0000000000..59eb1985a8 --- /dev/null +++ b/platform/socket.cpp @@ -0,0 +1,122 @@ +#include "socket.hpp" + +#include "base/assert.hpp" + +#include "std/algorithm.hpp" +#include "std/deque.hpp" +#include "std/mutex.hpp" + +namespace +{ +class MockSocket final : public platform::Socket +{ +public: + // Socket overrides + bool Open(string const & host, uint16_t port) override { return false; } + void Close() override {} + bool Read(uint8_t * data, uint32_t count) override { return false; } + bool Write(uint8_t const * data, uint32_t count) override { return false; } + void SetTimeout(uint32_t milliseconds) override {} +}; + +class TestSocketImpl final : public platform::TestSocket +{ +public: + // Socket overrides + ~TestSocketImpl(); + bool Open(string const & host, uint16_t port) override; + void Close() override; + bool Read(uint8_t * data, uint32_t count) override; + bool Write(uint8_t const * data, uint32_t count) override; + void SetTimeout(uint32_t milliseconds) override; + + // TestSocket overrides + bool HasInput() const override; + bool HasOutput() const override; + void WriteServer(uint8_t const * data, uint32_t count) override; + size_t ReadServer(vector<uint8_t> & destination) override; + +private: + bool m_isConnected = false; + + deque<uint8_t> m_input; + mutable mutex m_inputMutex; + + vector<uint8_t> m_output; + mutable mutex m_outputMutex; +}; + +TestSocketImpl::~TestSocketImpl() { m_isConnected = false; } + +bool TestSocketImpl::Open(string const & host, uint16_t port) +{ + m_isConnected = true; + return true; +} + +void TestSocketImpl::Close() { m_isConnected = false; } +bool TestSocketImpl::Read(uint8_t * data, uint32_t count) +{ + if (!m_isConnected) + return false; + + lock_guard<mutex> lg(m_inputMutex); + + if (m_input.size() < count) + return false; + + copy(m_input.begin(), m_input.end(), data); + m_input.erase(m_input.begin(), m_input.begin() + count); + return true; +} + +bool TestSocketImpl::Write(uint8_t const * data, uint32_t count) +{ + if (!m_isConnected) + return false; + + lock_guard<mutex> lg(m_outputMutex); + m_output.insert(m_output.end(), data, data + count); + return true; +} + +void TestSocketImpl::SetTimeout(uint32_t milliseconds) {} +bool TestSocketImpl::HasInput() const +{ + lock_guard<mutex> lg(m_inputMutex); + return !m_input.empty(); +} + +bool TestSocketImpl::HasOutput() const +{ + lock_guard<mutex> lg(m_outputMutex); + return !m_output.empty(); +} + +void TestSocketImpl::WriteServer(uint8_t const * data, uint32_t count) +{ + ASSERT(m_isConnected, ()); + + lock_guard<mutex> lg(m_inputMutex); + m_input.insert(m_input.end(), data, data + count); +} + +size_t TestSocketImpl::ReadServer(vector<uint8_t> & destination) +{ + lock_guard<mutex> lg(m_outputMutex); + + size_t const outputSize = m_output.size(); + if (outputSize == 0) + return 0; + + destination.insert(destination.end(), m_output.begin(), m_output.end()); + m_output.clear(); + return outputSize; +} +} // namespace + +namespace platform +{ +unique_ptr<Socket> createMockSocket() { return make_unique<MockSocket>(); } +unique_ptr<TestSocket> createTestSocket() { return make_unique<TestSocketImpl>(); } +} // namespace platform diff --git a/platform/socket.hpp b/platform/socket.hpp index 01f51eb133..41a0d7711e 100644 --- a/platform/socket.hpp +++ b/platform/socket.hpp @@ -1,8 +1,8 @@ #pragma once #include "std/string.hpp" -#include "std/target_os.hpp" #include "std/unique_ptr.hpp" +#include "std/vector.hpp" namespace platform { @@ -26,5 +26,21 @@ public: virtual void SetTimeout(uint32_t milliseconds) = 0; }; +class TestSocket : public Socket +{ +public: + virtual bool HasInput() const = 0; + virtual bool HasOutput() const = 0; + + // Simulate server writing + virtual void WriteServer(uint8_t const * data, uint32_t count) = 0; + + // Simulate server reading + // returns size of read data + virtual size_t ReadServer(vector<uint8_t> & destination) = 0; +}; + unique_ptr<Socket> createSocket(); +unique_ptr<Socket> createMockSocket(); +unique_ptr<TestSocket> createTestSocket(); } // namespace platform |