2008-02-03 19:16:33 -05:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Name: samples/ipc/baseserver.cpp
|
|
|
|
// Purpose: IPC sample: console server
|
|
|
|
// Author: Anders Larsen
|
|
|
|
// Most of the code was stolen from: samples/ipc/server.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"
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
2008-10-16 08:35:03 -04:00
|
|
|
// local classes
|
2008-02-03 19:16:33 -05:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2008-10-16 08:35:03 -04:00
|
|
|
// a simple connection class testing and logging various operations
|
2008-02-10 18:43:39 -05:00
|
|
|
class MyConnection : public MyConnectionBase, public wxTimer
|
2008-02-03 19:16:33 -05:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
virtual bool Disconnect() { return wxConnection::Disconnect(); }
|
2008-10-16 08:35:03 -04:00
|
|
|
virtual bool OnExecute(const wxString& topic,
|
|
|
|
const void *data,
|
|
|
|
size_t size,
|
|
|
|
wxIPCFormat format);
|
|
|
|
virtual const void *OnRequest(const wxString& topic,
|
|
|
|
const wxString& item,
|
|
|
|
size_t *size,
|
|
|
|
wxIPCFormat format);
|
|
|
|
virtual bool OnPoke(const wxString& topic,
|
|
|
|
const wxString& item,
|
|
|
|
const void *data,
|
|
|
|
size_t size,
|
|
|
|
wxIPCFormat format);
|
2008-02-03 19:16:33 -05:00
|
|
|
virtual bool OnStartAdvise(const wxString& topic, const wxString& item);
|
|
|
|
virtual bool OnStopAdvise(const wxString& topic, const wxString& item);
|
2008-10-16 08:35:03 -04:00
|
|
|
virtual bool DoAdvise(const wxString& item,
|
|
|
|
const void *data,
|
|
|
|
size_t size,
|
|
|
|
wxIPCFormat format);
|
2008-02-03 19:16:33 -05:00
|
|
|
virtual bool OnDisconnect();
|
|
|
|
virtual void Notify();
|
|
|
|
|
2008-10-14 09:29:26 -04:00
|
|
|
private:
|
2008-02-03 19:16:33 -05:00
|
|
|
wxString m_sAdvise;
|
|
|
|
|
|
|
|
wxString m_sRequestDate;
|
|
|
|
char m_achRequestBytes[3];
|
|
|
|
};
|
|
|
|
|
2008-10-16 08:35:03 -04:00
|
|
|
// a connection used for benchmarking some IPC operations by
|
|
|
|
// tests/benchmarks/ipcclient.cpp
|
|
|
|
class BenchConnection : public wxConnection
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
BenchConnection() { m_advise = false; }
|
|
|
|
|
|
|
|
virtual bool OnPoke(const wxString& topic,
|
|
|
|
const wxString& item,
|
|
|
|
const void *data,
|
|
|
|
size_t size,
|
|
|
|
wxIPCFormat format);
|
|
|
|
virtual bool OnStartAdvise(const wxString& topic, const wxString& item);
|
|
|
|
virtual bool OnStopAdvise(const wxString& topic, const wxString& item);
|
|
|
|
|
|
|
|
private:
|
|
|
|
// return true if this is the supported topic+item combination, log an
|
|
|
|
// error message otherwise
|
|
|
|
bool IsSupportedTopicAndItem(const wxString& operation,
|
|
|
|
const wxString& topic,
|
|
|
|
const wxString& item) const;
|
|
|
|
|
|
|
|
// the item which can be manipulated by the client via Poke() calls
|
|
|
|
wxString m_item;
|
|
|
|
|
|
|
|
// should we notify the client about changes to m_item?
|
|
|
|
bool m_advise;
|
|
|
|
|
|
|
|
DECLARE_NO_COPY_CLASS(BenchConnection)
|
|
|
|
};
|
|
|
|
|
|
|
|
// a simple server accepting connections to IPC_TOPIC and IPC_BENCHMARK_TOPIC
|
2008-10-14 09:29:26 -04:00
|
|
|
class MyServer : public wxServer
|
2008-02-03 19:16:33 -05:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
MyServer();
|
|
|
|
virtual ~MyServer();
|
|
|
|
void Disconnect();
|
|
|
|
bool IsConnected() { return m_connection != NULL; };
|
2008-10-14 09:29:26 -04:00
|
|
|
|
|
|
|
virtual wxConnectionBase *OnAcceptConnection(const wxString& topic);
|
|
|
|
|
|
|
|
private:
|
|
|
|
wxConnection *m_connection;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Define a new application
|
|
|
|
class MyApp : public wxApp
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
virtual bool OnInit();
|
2008-02-03 19:16:33 -05:00
|
|
|
|
|
|
|
protected:
|
2008-10-14 09:29:26 -04:00
|
|
|
MyServer m_server;
|
2008-02-03 19:16:33 -05:00
|
|
|
};
|
|
|
|
|
2008-10-14 09:29:26 -04:00
|
|
|
DECLARE_APP(MyApp)
|
|
|
|
|
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
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
bool MyApp::OnInit()
|
|
|
|
{
|
|
|
|
if ( !wxApp::OnInit() )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
delete wxLog::SetActiveTarget(new wxLogStderr);
|
|
|
|
|
2008-10-14 09:29:26 -04:00
|
|
|
const char * const kind =
|
2008-02-03 19:16:33 -05:00
|
|
|
#if wxUSE_DDE_FOR_IPC
|
2008-10-14 09:29:26 -04:00
|
|
|
"DDE"
|
|
|
|
#else
|
|
|
|
"TCP"
|
|
|
|
#endif
|
|
|
|
;
|
|
|
|
|
|
|
|
// Create a new server
|
|
|
|
if ( !m_server.Create(IPC_SERVICE) )
|
2008-02-03 19:16:33 -05:00
|
|
|
{
|
2008-10-14 09:29:26 -04:00
|
|
|
wxLogMessage("%s server failed to start on %s", kind, IPC_SERVICE);
|
2008-02-03 19:16:33 -05:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-10-14 09:29:26 -04:00
|
|
|
wxLogMessage("%s server started on %s", kind, IPC_SERVICE);
|
|
|
|
return true;
|
2008-02-03 19:16:33 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// MyServer
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
MyServer::MyServer()
|
|
|
|
{
|
|
|
|
m_connection = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
MyServer::~MyServer()
|
|
|
|
{
|
|
|
|
Disconnect();
|
|
|
|
}
|
|
|
|
|
|
|
|
wxConnectionBase *MyServer::OnAcceptConnection(const wxString& topic)
|
|
|
|
{
|
2008-10-16 08:35:03 -04:00
|
|
|
wxLogMessage("OnAcceptConnection(\"%s\")", topic);
|
2008-02-03 19:16:33 -05:00
|
|
|
|
|
|
|
if ( topic == IPC_TOPIC )
|
|
|
|
{
|
|
|
|
m_connection = new MyConnection;
|
2008-10-16 08:35:03 -04:00
|
|
|
}
|
|
|
|
else if ( topic == IPC_BENCHMARK_TOPIC )
|
|
|
|
{
|
|
|
|
m_connection = new BenchConnection;
|
|
|
|
}
|
|
|
|
else // unknown topic
|
|
|
|
{
|
|
|
|
wxLogMessage("Unknown topic");
|
|
|
|
return NULL;
|
2008-02-03 19:16:33 -05:00
|
|
|
}
|
2008-10-14 09:29:26 -04:00
|
|
|
|
2008-10-16 08:35:03 -04:00
|
|
|
wxLogMessage("Connection accepted");
|
|
|
|
return m_connection;
|
2008-02-03 19:16:33 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void MyServer::Disconnect()
|
|
|
|
{
|
2008-10-16 08:35:03 -04:00
|
|
|
if ( m_connection )
|
2008-02-03 19:16:33 -05:00
|
|
|
{
|
|
|
|
m_connection->Disconnect();
|
|
|
|
delete m_connection;
|
|
|
|
m_connection = NULL;
|
2008-10-14 09:29:26 -04:00
|
|
|
wxLogMessage("Disconnected client");
|
2008-02-03 19:16:33 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// MyConnection
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
2008-10-14 09:29:26 -04:00
|
|
|
bool
|
|
|
|
MyConnection::OnExecute(const wxString& topic,
|
|
|
|
const void *data,
|
|
|
|
size_t size,
|
|
|
|
wxIPCFormat format)
|
2008-02-03 19:16:33 -05:00
|
|
|
{
|
2008-10-14 09:29:26 -04:00
|
|
|
Log("OnExecute", topic, "", data, size, format);
|
2008-02-03 19:16:33 -05:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-10-14 09:29:26 -04:00
|
|
|
bool
|
|
|
|
MyConnection::OnPoke(const wxString& topic,
|
|
|
|
const wxString& item,
|
|
|
|
const void *data,
|
|
|
|
size_t size,
|
|
|
|
wxIPCFormat format)
|
2008-02-03 19:16:33 -05:00
|
|
|
{
|
2008-10-14 09:29:26 -04:00
|
|
|
Log("OnPoke", topic, item, data, size, format);
|
2008-02-03 19:16:33 -05:00
|
|
|
return wxConnection::OnPoke(topic, item, data, size, format);
|
|
|
|
}
|
|
|
|
|
2008-10-14 09:29:26 -04:00
|
|
|
const void *
|
|
|
|
MyConnection::OnRequest(const wxString& topic,
|
|
|
|
const wxString& item,
|
|
|
|
size_t *size,
|
|
|
|
wxIPCFormat format)
|
2008-02-03 19:16:33 -05:00
|
|
|
{
|
|
|
|
const void *data;
|
2008-10-14 09:29:26 -04:00
|
|
|
if (item == "Date")
|
2008-02-03 19:16:33 -05:00
|
|
|
{
|
|
|
|
m_sRequestDate = wxDateTime::Now().Format();
|
|
|
|
data = m_sRequestDate.c_str();
|
|
|
|
*size = wxNO_LEN;
|
|
|
|
}
|
2008-10-14 09:29:26 -04:00
|
|
|
else if (item == "Date+len")
|
2008-02-03 19:16:33 -05:00
|
|
|
{
|
2008-10-16 08:35:03 -04:00
|
|
|
m_sRequestDate = wxDateTime::Now().FormatTime() +
|
|
|
|
" " + wxDateTime::Now().FormatDate();
|
2008-02-03 19:16:33 -05:00
|
|
|
data = m_sRequestDate.c_str();
|
|
|
|
*size = m_sRequestDate.Length() + 1;
|
|
|
|
}
|
2008-10-14 09:29:26 -04:00
|
|
|
else if (item == "bytes[3]")
|
2008-02-03 19:16:33 -05:00
|
|
|
{
|
|
|
|
data = m_achRequestBytes;
|
2008-10-16 08:35:03 -04:00
|
|
|
m_achRequestBytes[0] = '1';
|
|
|
|
m_achRequestBytes[1] = '2';
|
|
|
|
m_achRequestBytes[2] = '3';
|
2008-02-03 19:16:33 -05:00
|
|
|
*size = 3;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
data = NULL;
|
|
|
|
*size = 0;
|
|
|
|
}
|
2008-10-14 09:29:26 -04:00
|
|
|
Log("OnRequest", topic, item, data, *size, format);
|
2008-02-03 19:16:33 -05:00
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2008-10-14 09:29:26 -04:00
|
|
|
bool MyConnection::OnStartAdvise(const wxString& topic, const wxString& item)
|
2008-02-03 19:16:33 -05:00
|
|
|
{
|
2008-10-16 08:35:03 -04:00
|
|
|
wxLogMessage("OnStartAdvise(\"%s\",\"%s\")", topic, item);
|
2008-10-14 09:29:26 -04:00
|
|
|
wxLogMessage("Returning true");
|
2008-02-03 19:16:33 -05:00
|
|
|
m_sAdvise = item;
|
2008-10-14 09:29:26 -04:00
|
|
|
Start(3000); // schedule our Notify() to be called in 3 seconds
|
2008-02-03 19:16:33 -05:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-10-14 09:29:26 -04:00
|
|
|
bool MyConnection::OnStopAdvise(const wxString& topic, const wxString& item)
|
2008-02-03 19:16:33 -05:00
|
|
|
{
|
2008-10-16 08:35:03 -04:00
|
|
|
wxLogMessage("OnStopAdvise(\"%s\",\"%s\")", topic, item);
|
2008-10-14 09:29:26 -04:00
|
|
|
wxLogMessage("Returning true");
|
|
|
|
m_sAdvise.clear();
|
2008-02-03 19:16:33 -05:00
|
|
|
Stop();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MyConnection::Notify()
|
|
|
|
{
|
2008-10-14 09:29:26 -04:00
|
|
|
if (!m_sAdvise.empty())
|
2008-02-03 19:16:33 -05:00
|
|
|
{
|
|
|
|
wxString s = wxDateTime::Now().Format();
|
|
|
|
Advise(m_sAdvise, s);
|
2008-10-16 08:35:03 -04:00
|
|
|
s = wxDateTime::Now().FormatTime() + " "
|
|
|
|
+ wxDateTime::Now().FormatDate();
|
2008-10-14 09:29:26 -04:00
|
|
|
Advise(m_sAdvise, s.mb_str(), s.length() + 1);
|
2008-02-03 19:16:33 -05:00
|
|
|
|
|
|
|
#if wxUSE_DDE_FOR_IPC
|
2008-10-14 09:29:26 -04:00
|
|
|
wxLogMessage("DDE Advise type argument cannot be wxIPC_PRIVATE. "
|
|
|
|
"The client will receive it as wxIPC_TEXT, "
|
|
|
|
"and receive the correct no of bytes, "
|
|
|
|
"but not print a correct log entry.");
|
|
|
|
#endif // DDE
|
|
|
|
|
2008-02-03 19:16:33 -05:00
|
|
|
char bytes[3];
|
|
|
|
bytes[0] = '1'; bytes[1] = '2'; bytes[2] = '3';
|
|
|
|
Advise(m_sAdvise, bytes, 3, wxIPC_PRIVATE);
|
|
|
|
// this works, but the log treats it as a string now
|
|
|
|
// m_connection->Advise(m_connection->m_sAdvise, bytes, 3, wxIPC_TEXT );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-16 08:35:03 -04:00
|
|
|
bool MyConnection::DoAdvise(const wxString& item,
|
|
|
|
const void *data,
|
|
|
|
size_t size,
|
|
|
|
wxIPCFormat format)
|
2008-02-03 19:16:33 -05:00
|
|
|
{
|
2008-10-14 09:29:26 -04:00
|
|
|
Log("Advise", "", item, data, size, format);
|
2008-02-03 19:16:33 -05:00
|
|
|
return wxConnection::DoAdvise(item, data, size, format);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MyConnection::OnDisconnect()
|
|
|
|
{
|
2008-10-14 09:29:26 -04:00
|
|
|
wxLogMessage("OnDisconnect()");
|
2008-02-03 19:16:33 -05:00
|
|
|
return true;
|
|
|
|
}
|
2008-10-16 08:35:03 -04:00
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// BenchConnection
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
bool BenchConnection::IsSupportedTopicAndItem(const wxString& operation,
|
|
|
|
const wxString& topic,
|
|
|
|
const wxString& item) const
|
|
|
|
{
|
|
|
|
if ( topic != IPC_BENCHMARK_TOPIC ||
|
|
|
|
item != IPC_BENCHMARK_ITEM )
|
|
|
|
{
|
|
|
|
wxLogMessage("Unexpected %s(\"%s\", \"%s\") call.",
|
|
|
|
operation, topic, item);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BenchConnection::OnPoke(const wxString& topic,
|
|
|
|
const wxString& item,
|
|
|
|
const void *data,
|
|
|
|
size_t size,
|
|
|
|
wxIPCFormat format)
|
|
|
|
{
|
|
|
|
if ( !IsSupportedTopicAndItem("OnPoke", topic, item) )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if ( !IsTextFormat(format) )
|
|
|
|
{
|
|
|
|
wxLogMessage("Unexpected format %d in OnPoke().", format);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_item = GetTextFromData(data, size, format);
|
|
|
|
|
|
|
|
if ( m_advise )
|
|
|
|
{
|
|
|
|
if ( !Advise(item, m_item) )
|
|
|
|
wxLogMessage("Failed to advise client about the change.");
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BenchConnection::OnStartAdvise(const wxString& topic, const wxString& item)
|
|
|
|
{
|
|
|
|
if ( !IsSupportedTopicAndItem("OnStartAdvise", topic, item) )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
m_advise = true;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BenchConnection::OnStopAdvise(const wxString& topic, const wxString& item)
|
|
|
|
{
|
|
|
|
if ( !IsSupportedTopicAndItem("OnStopAdvise", topic, item) )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
m_advise = false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|