ipc client connect test with working unit tests.

This commit is contained in:
Nick Bolton 2012-06-29 11:33:21 +00:00
parent 7d8fd5927d
commit 218e98398f
15 changed files with 86 additions and 40 deletions

View File

@ -54,6 +54,18 @@ CEventQueue::~CEventQueue()
setInstance(NULL);
}
void
CEventQueue::loop()
{
CEvent event;
getEvent(event);
while (event.getType() != CEvent::kQuit) {
dispatchEvent(event);
CEvent::deleteData(event);
getEvent(event);
}
}
CEvent::Type
CEventQueue::registerType(const char* name)
{

View File

@ -37,6 +37,7 @@ public:
virtual ~CEventQueue();
// IEventQueue overrides
virtual void loop();
virtual void adoptBuffer(IEventQueueBuffer*);
virtual bool getEvent(CEvent& event, double timeout = -1.0);
virtual bool dispatchEvent(const CEvent& event);

View File

@ -49,6 +49,12 @@ public:
//! @name manipulators
//@{
//! Loop the event queue until quit
/*!
Dequeues and dispatches events until the kQuit event is found.
*/
virtual void loop() = 0;
//! Set the buffer
/*!
Replace the current event queue buffer. Any queued events are

View File

@ -16,9 +16,12 @@
*/
#include "CIpcClient.h"
#include "Ipc.h"
CIpcClient::CIpcClient()
CIpcClient::CIpcClient() :
m_serverAddress(CNetworkAddress(IPC_HOST, IPC_PORT))
{
m_serverAddress.resolve();
}
CIpcClient::~CIpcClient()
@ -28,4 +31,5 @@ CIpcClient::~CIpcClient()
void
CIpcClient::connect()
{
m_socket.connect(m_serverAddress);
}

View File

@ -17,6 +17,9 @@
#pragma once
#include "CNetworkAddress.h"
#include "CTCPSocket.h"
//! IPC client for communication between daemon and GUI.
/*!
* See \ref CIpcServer description.
@ -28,4 +31,8 @@ public:
//! Connects to the IPC server at localhost.
void connect();
private:
CNetworkAddress m_serverAddress;
CTCPSocket m_socket;
};

View File

@ -16,9 +16,12 @@
*/
#include "CIpcServer.h"
#include "Ipc.h"
CIpcServer::CIpcServer()
CIpcServer::CIpcServer() :
m_address(CNetworkAddress(IPC_HOST, IPC_PORT))
{
m_address.resolve();
}
CIpcServer::~CIpcServer()
@ -28,4 +31,5 @@ CIpcServer::~CIpcServer()
void
CIpcServer::listen()
{
m_socket.bind(m_address);
}

View File

@ -17,6 +17,9 @@
#pragma once
#include "CTCPListenSocket.h"
#include "CNetworkAddress.h"
//! IPC server for communication between daemon and GUI.
/*!
* The IPC server listens on localhost. The IPC client runs on both the
@ -31,4 +34,8 @@ public:
//! Opens a TCP socket only allowing local connections
void listen();
private:
CTCPListenSocket m_socket;
CNetworkAddress m_address;
};

View File

@ -14,6 +14,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
set(inc
Ipc.h
CIpcServer.h
CIpcClient.h
CIpcServerProxy.h
@ -32,6 +33,11 @@ if (WIN32)
endif()
set(inc
../arch
../base
../common
../io
../mt
../net
)
@ -45,5 +51,5 @@ include_directories(${inc})
add_library(ipc STATIC ${src})
if (UNIX)
target_link_libraries(net)
target_link_libraries(arch base common mt io net)
endif()

21
src/lib/ipc/Ipc.h Normal file
View File

@ -0,0 +1,21 @@
/*
* synergy -- mouse and keyboard sharing utility
* Copyright (C) 2012 Nick Bolton
*
* This package is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* found in the file COPYING that should have accompanied this file.
*
* This package is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#define IPC_HOST "127.0.0.1"
#define IPC_PORT 24801

View File

@ -82,6 +82,7 @@ CSocketMultiplexer::~CSocketMultiplexer()
CSocketMultiplexer*
CSocketMultiplexer::getInstance()
{
assert(s_instance != NULL);
return s_instance;
}

View File

@ -538,14 +538,8 @@ CClientApp::mainLoop()
// run event loop. if startClient() failed we're supposed to retry
// later. the timer installed by startClient() will take care of
// that.
CEvent event;
DAEMON_RUNNING(true);
EVENTQUEUE->getEvent(event);
while (event.getType() != CEvent::kQuit) {
EVENTQUEUE->dispatchEvent(event);
CEvent::deleteData(event);
EVENTQUEUE->getEvent(event);
}
EVENTQUEUE->loop();
DAEMON_RUNNING(false);
// close down

View File

@ -190,13 +190,7 @@ CDaemonApp::mainLoop(bool logToFile)
m_relauncher.startAsync();
#endif
CEvent event;
EVENTQUEUE->getEvent(event);
while (event.getType() != CEvent::kQuit) {
EVENTQUEUE->dispatchEvent(event);
CEvent::deleteData(event);
EVENTQUEUE->getEvent(event);
}
EVENTQUEUE->loop();
#if SYSAPI_WIN32
m_relauncher.stop();

View File

@ -356,13 +356,9 @@ CServerApp::closeServer(CServer* server)
new TMethodEventJob<CServerApp>(this, &CServerApp::handleClientsDisconnected));
EVENTQUEUE->adoptHandler(CServer::getDisconnectedEvent(), server,
new TMethodEventJob<CServerApp>(this, &CServerApp::handleClientsDisconnected));
CEvent event;
EVENTQUEUE->getEvent(event);
while (event.getType() != CEvent::kQuit) {
EVENTQUEUE->dispatchEvent(event);
CEvent::deleteData(event);
EVENTQUEUE->getEvent(event);
}
EVENTQUEUE->loop();
EVENTQUEUE->removeHandler(CEvent::kTimer, timer);
EVENTQUEUE->deleteTimer(timer);
EVENTQUEUE->removeHandler(CServer::getDisconnectedEvent(), server);
@ -815,14 +811,8 @@ CServerApp::mainLoop()
// run event loop. if startServer() failed we're supposed to retry
// later. the timer installed by startServer() will take care of
// that.
CEvent event;
DAEMON_RUNNING(true);
EVENTQUEUE->getEvent(event);
while (event.getType() != CEvent::kQuit) {
EVENTQUEUE->dispatchEvent(event);
CEvent::deleteData(event);
EVENTQUEUE->getEvent(event);
}
EVENTQUEUE->loop();
DAEMON_RUNNING(false);
// close down

View File

@ -18,21 +18,19 @@
#include <gtest/gtest.h>
#include "CIpcServer.h"
#include "CIpcClient.h"
#include "CSocketMultiplexer.h"
#include "CEventQueue.h"
TEST(CIpcTests, serverSend)
TEST(CIpcTests, connectToServer)
{
CSocketMultiplexer multiplexer;
CEventQueue eventQueue;
CIpcServer server;
server.listen();
CIpcClient client;
client.connect();
}
TEST(CIpcTests, clientSend)
{
CIpcServer server;
server.listen();
CIpcClient client;
client.connect();
eventQueue.loop();
}

View File

@ -24,6 +24,7 @@
class CMockEventQueue : public IEventQueue
{
public:
MOCK_METHOD0(loop, void());
MOCK_METHOD2(newOneShotTimer, CEventQueueTimer*(double, void*));
MOCK_METHOD2(newTimer, CEventQueueTimer*(double, void*));
MOCK_METHOD2(getEvent, bool(CEvent&, double));