2008-02-03 19:16:33 -05:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Name: samples/ipc/baseclient.cpp
|
|
|
|
// Purpose: IPC sample: console client
|
|
|
|
// Author: Anders Larsen
|
|
|
|
// Most of the code was stolen from: samples/ipc/client.cpp
|
|
|
|
// (c) Julian Smart, Jurgen Doornik
|
|
|
|
// Created: 2007-11-08
|
|
|
|
// RCS-ID: $Id$
|
|
|
|
// Copyright: (c) 2007 Anders Larsen
|
|
|
|
// License: wxWindows licence
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// ============================================================================
|
|
|
|
// declarations
|
|
|
|
// ============================================================================
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// headers
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// For compilers that support precompilation, includes "wx.h".
|
|
|
|
#include "wx/wxprec.h"
|
|
|
|
|
|
|
|
#ifdef __BORLANDC__
|
|
|
|
#pragma hdrstop
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef WX_PRECOMP
|
|
|
|
#include "wx/wx.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Settings common to both executables: determines whether
|
|
|
|
// we're using TCP/IP or real DDE.
|
|
|
|
#include "ipcsetup.h"
|
|
|
|
|
2008-02-10 18:43:39 -05:00
|
|
|
#include "connection.h"
|
|
|
|
|
2008-02-03 19:16:33 -05:00
|
|
|
#include "wx/timer.h"
|
|
|
|
#include "wx/datetime.h"
|
2009-01-02 21:21:12 -05:00
|
|
|
#include "wx/vector.h"
|
|
|
|
|
|
|
|
class MyClient;
|
2008-02-03 19:16:33 -05:00
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
2009-01-02 21:21:12 -05:00
|
|
|
// classes
|
2008-02-03 19:16:33 -05:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2009-01-02 21:21:12 -05:00
|
|
|
class MyApp : public wxApp
|
2008-02-03 19:16:33 -05:00
|
|
|
{
|
|
|
|
public:
|
2009-01-02 21:21:12 -05:00
|
|
|
MyApp() { Connect(wxEVT_IDLE, wxIdleEventHandler(MyApp::OnIdle)); }
|
|
|
|
|
2008-02-03 19:16:33 -05:00
|
|
|
virtual bool OnInit();
|
|
|
|
virtual int OnExit();
|
|
|
|
|
2009-01-02 21:21:12 -05:00
|
|
|
private:
|
|
|
|
void OnIdle(wxIdleEvent& event);
|
|
|
|
|
|
|
|
MyClient *m_client;
|
2008-02-03 19:16:33 -05:00
|
|
|
};
|
|
|
|
|
2008-02-10 18:43:39 -05:00
|
|
|
class MyConnection : public MyConnectionBase
|
2008-02-03 19:16:33 -05:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
virtual bool DoExecute(const void *data, size_t size, wxIPCFormat format);
|
|
|
|
virtual const void *Request(const wxString& item, size_t *size = NULL, wxIPCFormat format = wxIPC_TEXT);
|
|
|
|
virtual bool DoPoke(const wxString& item, const void* data, size_t size, wxIPCFormat format);
|
|
|
|
virtual bool OnAdvise(const wxString& topic, const wxString& item, const void *data, size_t size, wxIPCFormat format);
|
|
|
|
virtual bool OnDisconnect();
|
|
|
|
};
|
|
|
|
|
2009-01-02 21:21:12 -05:00
|
|
|
class MyClient : public wxClient,
|
|
|
|
private wxTimer
|
2008-02-03 19:16:33 -05:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
MyClient();
|
|
|
|
virtual ~MyClient();
|
2009-01-02 21:21:12 -05:00
|
|
|
|
2008-02-03 19:16:33 -05:00
|
|
|
bool Connect(const wxString& sHost, const wxString& sService, const wxString& sTopic);
|
|
|
|
void Disconnect();
|
|
|
|
wxConnectionBase *OnMakeConnection();
|
|
|
|
bool IsConnected() { return m_connection != NULL; };
|
2009-01-02 21:21:12 -05:00
|
|
|
|
2008-02-03 19:16:33 -05:00
|
|
|
virtual void Notify();
|
|
|
|
|
2009-01-02 21:21:12 -05:00
|
|
|
void StartNextTestIfNecessary();
|
|
|
|
|
|
|
|
private:
|
|
|
|
void TestRequest();
|
|
|
|
void TestPoke();
|
|
|
|
void TestExecute();
|
|
|
|
void TestStartAdvise();
|
|
|
|
void TestStopAdvise();
|
|
|
|
void TestDisconnect();
|
|
|
|
|
|
|
|
|
|
|
|
MyConnection *m_connection;
|
|
|
|
|
|
|
|
// the test functions to be executed by StartNextTestIfNecessary()
|
|
|
|
typedef void (MyClient::*MyClientTestFunc)();
|
|
|
|
wxVector<MyClientTestFunc> m_tests;
|
|
|
|
|
|
|
|
// number of seconds since the start of the test
|
|
|
|
int m_step;
|
2008-02-03 19:16:33 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
// ============================================================================
|
|
|
|
// implementation
|
|
|
|
// ============================================================================
|
|
|
|
|
2008-06-01 14:52:22 -04:00
|
|
|
IMPLEMENT_APP_CONSOLE(MyApp)
|
2008-02-03 19:16:33 -05:00
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// MyApp
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// The `main program' equivalent, creating the windows and returning the
|
|
|
|
// main frame
|
|
|
|
bool MyApp::OnInit()
|
|
|
|
{
|
|
|
|
if ( !wxApp::OnInit() )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Create a new client
|
|
|
|
m_client = new MyClient;
|
|
|
|
bool retval = m_client->Connect("localhost", "4242", "IPC TEST");
|
|
|
|
|
2009-01-02 21:21:12 -05:00
|
|
|
wxLogMessage("Client host=\"localhost\" port=\"4242\" topic=\"IPC TEST\" %s",
|
|
|
|
retval ? "connected" : "failed to connect");
|
2008-02-03 19:16:33 -05:00
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
int MyApp::OnExit()
|
|
|
|
{
|
|
|
|
delete m_client;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-01-02 21:21:12 -05:00
|
|
|
void MyApp::OnIdle(wxIdleEvent& event)
|
|
|
|
{
|
|
|
|
if ( m_client )
|
|
|
|
m_client->StartNextTestIfNecessary();
|
|
|
|
|
|
|
|
event.Skip();
|
|
|
|
}
|
|
|
|
|
2008-02-03 19:16:33 -05:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// MyClient
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2009-01-02 21:21:12 -05:00
|
|
|
MyClient::MyClient()
|
|
|
|
: wxClient()
|
2008-02-03 19:16:33 -05:00
|
|
|
{
|
|
|
|
m_connection = NULL;
|
|
|
|
m_step = 0;
|
|
|
|
}
|
|
|
|
|
2009-01-02 21:21:12 -05:00
|
|
|
bool
|
|
|
|
MyClient::Connect(const wxString& sHost,
|
|
|
|
const wxString& sService,
|
|
|
|
const wxString& sTopic)
|
2008-02-03 19:16:33 -05:00
|
|
|
{
|
|
|
|
// suppress the log messages from MakeConnection()
|
|
|
|
wxLogNull nolog;
|
|
|
|
|
|
|
|
m_connection = (MyConnection *)MakeConnection(sHost, sService, sTopic);
|
2009-01-02 21:21:12 -05:00
|
|
|
if ( !m_connection )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
Start(1000);
|
|
|
|
|
|
|
|
return true;
|
2008-02-03 19:16:33 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
wxConnectionBase *MyClient::OnMakeConnection()
|
|
|
|
{
|
|
|
|
return new MyConnection;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MyClient::Disconnect()
|
|
|
|
{
|
|
|
|
if (m_connection)
|
|
|
|
{
|
|
|
|
m_connection->Disconnect();
|
|
|
|
delete m_connection;
|
|
|
|
m_connection = NULL;
|
2009-01-02 21:21:12 -05:00
|
|
|
wxLogMessage("Client disconnected from server");
|
2008-02-03 19:16:33 -05:00
|
|
|
}
|
|
|
|
wxGetApp().ExitMainLoop();
|
|
|
|
}
|
|
|
|
|
|
|
|
MyClient::~MyClient()
|
|
|
|
{
|
|
|
|
Disconnect();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MyClient::Notify()
|
|
|
|
{
|
2009-01-02 21:21:12 -05:00
|
|
|
// we shouldn't call wxIPC methods from here directly as we may be called
|
|
|
|
// from inside an IPC call when using TCP/IP as the sockets are used in
|
|
|
|
// non-blocking code and so can dispatch events, including the timer ones,
|
|
|
|
// while waiting for IO and so starting another IPC call would result in
|
|
|
|
// fatal reentrancies -- instead, just set a flag and perform the test
|
|
|
|
// indicated by it later from our idle event handler
|
|
|
|
MyClientTestFunc testfunc = NULL;
|
|
|
|
switch ( m_step++ )
|
2008-02-03 19:16:33 -05:00
|
|
|
{
|
|
|
|
case 0:
|
2009-01-02 21:21:12 -05:00
|
|
|
testfunc = &MyClient::TestRequest;
|
2008-02-03 19:16:33 -05:00
|
|
|
break;
|
2009-01-02 21:21:12 -05:00
|
|
|
|
2008-02-03 19:16:33 -05:00
|
|
|
case 1:
|
2009-01-02 21:21:12 -05:00
|
|
|
testfunc = &MyClient::TestPoke;
|
2008-02-03 19:16:33 -05:00
|
|
|
break;
|
2009-01-02 21:21:12 -05:00
|
|
|
|
2008-02-03 19:16:33 -05:00
|
|
|
case 2:
|
2009-01-02 21:21:12 -05:00
|
|
|
testfunc = &MyClient::TestExecute;
|
2008-02-03 19:16:33 -05:00
|
|
|
break;
|
2009-01-02 21:21:12 -05:00
|
|
|
|
2008-02-03 19:16:33 -05:00
|
|
|
case 3:
|
2009-01-02 21:21:12 -05:00
|
|
|
testfunc = &MyClient::TestStartAdvise;
|
2008-02-03 19:16:33 -05:00
|
|
|
break;
|
2009-01-02 21:21:12 -05:00
|
|
|
|
2008-02-03 19:16:33 -05:00
|
|
|
case 10:
|
2009-01-02 21:21:12 -05:00
|
|
|
testfunc = &MyClient::TestStopAdvise;
|
2008-02-03 19:16:33 -05:00
|
|
|
break;
|
2009-01-02 21:21:12 -05:00
|
|
|
|
2008-02-03 19:16:33 -05:00
|
|
|
case 15:
|
2009-01-02 21:21:12 -05:00
|
|
|
testfunc = &MyClient::TestDisconnect;
|
2010-06-12 07:28:20 -04:00
|
|
|
// We don't need the timer any more, we're going to exit soon.
|
|
|
|
Stop();
|
2008-02-03 19:16:33 -05:00
|
|
|
break;
|
2010-06-12 07:28:20 -04:00
|
|
|
|
|
|
|
default:
|
|
|
|
// No need to wake up idle handling.
|
|
|
|
return;
|
2008-02-03 19:16:33 -05:00
|
|
|
}
|
2009-01-02 21:21:12 -05:00
|
|
|
|
2010-06-12 07:28:20 -04:00
|
|
|
m_tests.push_back(testfunc);
|
2009-01-02 21:21:12 -05:00
|
|
|
|
|
|
|
wxWakeUpIdle();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MyClient::StartNextTestIfNecessary()
|
|
|
|
{
|
2010-06-12 07:28:20 -04:00
|
|
|
while ( !m_tests.empty() )
|
2009-01-02 21:21:12 -05:00
|
|
|
{
|
|
|
|
MyClientTestFunc testfunc = m_tests.front();
|
|
|
|
m_tests.erase(m_tests.begin());
|
|
|
|
(this->*testfunc)();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MyClient::TestRequest()
|
|
|
|
{
|
|
|
|
size_t size;
|
|
|
|
m_connection->Request("Date");
|
|
|
|
m_connection->Request("Date+len", &size);
|
|
|
|
m_connection->Request("bytes[3]", &size, wxIPC_PRIVATE);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MyClient::TestPoke()
|
|
|
|
{
|
|
|
|
wxString s = wxDateTime::Now().Format();
|
|
|
|
m_connection->Poke("Date", s);
|
|
|
|
s = wxDateTime::Now().FormatTime() + " " + wxDateTime::Now().FormatDate();
|
|
|
|
m_connection->Poke("Date", (const char *)s.c_str(), s.length() + 1);
|
|
|
|
char bytes[3];
|
|
|
|
bytes[0] = '1'; bytes[1] = '2'; bytes[2] = '3';
|
|
|
|
m_connection->Poke("bytes[3]", bytes, 3, wxIPC_PRIVATE);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MyClient::TestExecute()
|
|
|
|
{
|
|
|
|
wxString s = "Date";
|
|
|
|
m_connection->Execute(s);
|
|
|
|
m_connection->Execute((const char *)s.c_str(), s.length() + 1);
|
|
|
|
char bytes[3];
|
|
|
|
bytes[0] = '1';
|
|
|
|
bytes[1] = '2';
|
|
|
|
bytes[2] = '3';
|
|
|
|
m_connection->Execute(bytes, WXSIZEOF(bytes));
|
|
|
|
}
|
|
|
|
|
|
|
|
void MyClient::TestStartAdvise()
|
|
|
|
{
|
|
|
|
wxLogMessage("StartAdvise(\"something\")");
|
|
|
|
m_connection->StartAdvise("something");
|
|
|
|
}
|
|
|
|
|
|
|
|
void MyClient::TestStopAdvise()
|
|
|
|
{
|
|
|
|
wxLogMessage("StopAdvise(\"something\")");
|
|
|
|
m_connection->StopAdvise("something");
|
|
|
|
}
|
|
|
|
|
|
|
|
void MyClient::TestDisconnect()
|
|
|
|
{
|
|
|
|
Disconnect();
|
2008-02-03 19:16:33 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// MyConnection
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
bool MyConnection::OnAdvise(const wxString& topic, const wxString& item, const void *data,
|
|
|
|
size_t size, wxIPCFormat format)
|
|
|
|
{
|
2009-01-02 21:21:12 -05:00
|
|
|
Log("OnAdvise", topic, item, data, size, format);
|
2008-02-03 19:16:33 -05:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MyConnection::OnDisconnect()
|
|
|
|
{
|
2009-01-02 21:21:12 -05:00
|
|
|
wxLogMessage("OnDisconnect()");
|
2008-02-03 19:16:33 -05:00
|
|
|
wxGetApp().ExitMainLoop();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MyConnection::DoExecute(const void *data, size_t size, wxIPCFormat format)
|
|
|
|
{
|
2009-01-02 21:21:12 -05:00
|
|
|
Log("Execute", wxEmptyString, wxEmptyString, data, size, format);
|
2008-02-03 19:16:33 -05:00
|
|
|
bool retval = wxConnection::DoExecute(data, size, format);
|
|
|
|
if (!retval)
|
2009-07-20 12:47:54 -04:00
|
|
|
{
|
2009-01-02 21:21:12 -05:00
|
|
|
wxLogMessage("Execute failed!");
|
2009-07-20 12:47:54 -04:00
|
|
|
}
|
2008-02-03 19:16:33 -05:00
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
const void *MyConnection::Request(const wxString& item, size_t *size, wxIPCFormat format)
|
|
|
|
{
|
|
|
|
const void *data = wxConnection::Request(item, size, format);
|
2009-01-02 21:21:12 -05:00
|
|
|
Log("Request", wxEmptyString, item, data, size ? *size : wxNO_LEN, format);
|
2008-02-03 19:16:33 -05:00
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MyConnection::DoPoke(const wxString& item, const void *data, size_t size, wxIPCFormat format)
|
|
|
|
{
|
2009-01-02 21:21:12 -05:00
|
|
|
Log("Poke", wxEmptyString, item, data, size, format);
|
2008-02-03 19:16:33 -05:00
|
|
|
return wxConnection::DoPoke(item, data, size, format);
|
|
|
|
}
|