no real changes, just come cleanup (use constants instead of hardcoded values; remove _T()s; don't allocate things on the heap unnecessarily; don't make members public or protected

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@56305 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
2008-10-14 13:29:26 +00:00
parent 012b4a0790
commit ea84f255b1

View File

@@ -42,21 +42,6 @@
// wxWin macros // wxWin macros
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// Define a new application
class MyServer;
class MyApp : public wxApp
{
public:
virtual bool OnInit();
virtual int OnExit();
protected:
MyServer *m_server;
};
DECLARE_APP(MyApp)
class MyConnection : public MyConnectionBase, public wxTimer class MyConnection : public MyConnectionBase, public wxTimer
{ {
public: public:
@@ -70,27 +55,39 @@ public:
virtual bool OnDisconnect(); virtual bool OnDisconnect();
virtual void Notify(); virtual void Notify();
private:
wxString m_sAdvise; wxString m_sAdvise;
protected:
wxString m_sRequestDate; wxString m_sRequestDate;
char m_achRequestBytes[3]; char m_achRequestBytes[3];
}; };
class MyServer: public wxServer class MyServer : public wxServer
{ {
public: public:
MyServer(); MyServer();
virtual ~MyServer(); virtual ~MyServer();
void Disconnect(); void Disconnect();
bool IsConnected() { return m_connection != NULL; }; bool IsConnected() { return m_connection != NULL; };
MyConnection *GetConnection() { return m_connection; };
wxConnectionBase *OnAcceptConnection(const wxString& topic); virtual wxConnectionBase *OnAcceptConnection(const wxString& topic);
private:
wxConnection *m_connection;
};
// Define a new application
class MyApp : public wxApp
{
public:
virtual bool OnInit();
protected: protected:
MyConnection *m_connection; MyServer m_server;
}; };
DECLARE_APP(MyApp)
// ============================================================================ // ============================================================================
// implementation // implementation
// ============================================================================ // ============================================================================
@@ -108,29 +105,23 @@ bool MyApp::OnInit()
delete wxLog::SetActiveTarget(new wxLogStderr); delete wxLog::SetActiveTarget(new wxLogStderr);
// Create a new server const char * const kind =
m_server = new MyServer;
if (m_server->Create("4242"))
{
wxLogMessage(_T("Server 4242 started"));
#if wxUSE_DDE_FOR_IPC #if wxUSE_DDE_FOR_IPC
wxLogMessage(_T("Server uses DDE")); "DDE"
#else // !wxUSE_DDE_FOR_IPC #else
wxLogMessage(_T("Server uses TCP")); "TCP"
#endif // wxUSE_DDE_FOR_IPC/!wxUSE_DDE_FOR_IPC #endif
return true; ;
}
else // Create a new server
if ( !m_server.Create(IPC_SERVICE) )
{ {
wxLogMessage(_T("Server 4242 failed to start")); wxLogMessage("%s server failed to start on %s", kind, IPC_SERVICE);
delete m_server;
return false; return false;
} }
}
int MyApp::OnExit() wxLogMessage("%s server started on %s", kind, IPC_SERVICE);
{ return true;
return 0;
} }
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
@@ -149,14 +140,15 @@ MyServer::~MyServer()
wxConnectionBase *MyServer::OnAcceptConnection(const wxString& topic) wxConnectionBase *MyServer::OnAcceptConnection(const wxString& topic)
{ {
wxLogMessage(_T("OnAcceptConnection(\"%s\")"), topic.c_str()); wxLogMessage("OnAcceptConnection(\"%s\")", topic.c_str());
if ( topic == IPC_TOPIC ) if ( topic == IPC_TOPIC )
{ {
m_connection = new MyConnection; m_connection = new MyConnection;
wxLogMessage(_T("Connection accepted")); wxLogMessage("Connection accepted");
return m_connection; return m_connection;
} }
// unknown topic // unknown topic
return NULL; return NULL;
} }
@@ -168,7 +160,7 @@ void MyServer::Disconnect()
m_connection->Disconnect(); m_connection->Disconnect();
delete m_connection; delete m_connection;
m_connection = NULL; m_connection = NULL;
wxLogMessage(_T("Disconnected client")); wxLogMessage("Disconnected client");
} }
} }
@@ -176,37 +168,47 @@ void MyServer::Disconnect()
// MyConnection // MyConnection
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
bool MyConnection::OnExecute(const wxString& topic, bool
const void *data, size_t size, wxIPCFormat format) MyConnection::OnExecute(const wxString& topic,
const void *data,
size_t size,
wxIPCFormat format)
{ {
Log(_T("OnExecute"), topic, _T(""), data, size, format); Log("OnExecute", topic, "", data, size, format);
return true; return true;
} }
bool MyConnection::OnPoke(const wxString& topic, bool
const wxString& item, const void *data, size_t size, wxIPCFormat format) MyConnection::OnPoke(const wxString& topic,
const wxString& item,
const void *data,
size_t size,
wxIPCFormat format)
{ {
Log(_T("OnPoke"), topic, item, data, size, format); Log("OnPoke", topic, item, data, size, format);
return wxConnection::OnPoke(topic, item, data, size, format); return wxConnection::OnPoke(topic, item, data, size, format);
} }
const void *MyConnection::OnRequest(const wxString& topic, const void *
const wxString& item, size_t *size, wxIPCFormat format) MyConnection::OnRequest(const wxString& topic,
const wxString& item,
size_t *size,
wxIPCFormat format)
{ {
const void *data; const void *data;
if (item == _T("Date")) if (item == "Date")
{ {
m_sRequestDate = wxDateTime::Now().Format(); m_sRequestDate = wxDateTime::Now().Format();
data = m_sRequestDate.c_str(); data = m_sRequestDate.c_str();
*size = wxNO_LEN; *size = wxNO_LEN;
} }
else if (item == _T("Date+len")) else if (item == "Date+len")
{ {
m_sRequestDate = wxDateTime::Now().FormatTime() + _T(" ") + wxDateTime::Now().FormatDate(); m_sRequestDate = wxDateTime::Now().FormatTime() + " " + wxDateTime::Now().FormatDate();
data = m_sRequestDate.c_str(); data = m_sRequestDate.c_str();
*size = m_sRequestDate.Length() + 1; *size = m_sRequestDate.Length() + 1;
} }
else if (item == _T("bytes[3]")) else if (item == "bytes[3]")
{ {
data = m_achRequestBytes; data = m_achRequestBytes;
m_achRequestBytes[0] = '1'; m_achRequestBytes[1] = '2'; m_achRequestBytes[2] = '3'; m_achRequestBytes[0] = '1'; m_achRequestBytes[1] = '2'; m_achRequestBytes[2] = '3';
@@ -217,42 +219,44 @@ const void *MyConnection::OnRequest(const wxString& topic,
data = NULL; data = NULL;
*size = 0; *size = 0;
} }
Log(_T("OnRequest"), topic, item, data, *size, format); Log("OnRequest", topic, item, data, *size, format);
return data; return data;
} }
bool MyConnection::OnStartAdvise(const wxString& topic, bool MyConnection::OnStartAdvise(const wxString& topic, const wxString& item)
const wxString& item)
{ {
wxLogMessage(_T("OnStartAdvise(\"%s\",\"%s\")"), topic.c_str(), item.c_str()); wxLogMessage("OnStartAdvise(\"%s\",\"%s\")", topic.c_str(), item.c_str());
wxLogMessage(_T("Returning true")); wxLogMessage("Returning true");
m_sAdvise = item; m_sAdvise = item;
Start(3000, false); Start(3000); // schedule our Notify() to be called in 3 seconds
return true; return true;
} }
bool MyConnection::OnStopAdvise(const wxString& topic, bool MyConnection::OnStopAdvise(const wxString& topic, const wxString& item)
const wxString& item)
{ {
wxLogMessage(_T("OnStopAdvise(\"%s\",\"%s\")"), topic.c_str(), item.c_str()); wxLogMessage("OnStopAdvise(\"%s\",\"%s\")", topic.c_str(), item.c_str());
wxLogMessage(_T("Returning true")); wxLogMessage("Returning true");
m_sAdvise.Empty(); m_sAdvise.clear();
Stop(); Stop();
return true; return true;
} }
void MyConnection::Notify() void MyConnection::Notify()
{ {
if (!m_sAdvise.IsEmpty()) if (!m_sAdvise.empty())
{ {
wxString s = wxDateTime::Now().Format(); wxString s = wxDateTime::Now().Format();
Advise(m_sAdvise, s); Advise(m_sAdvise, s);
s = wxDateTime::Now().FormatTime() + _T(" ") + wxDateTime::Now().FormatDate(); s = wxDateTime::Now().FormatTime() + " " + wxDateTime::Now().FormatDate();
Advise(m_sAdvise, (const char *)s.c_str(), s.Length() + 1); Advise(m_sAdvise, s.mb_str(), s.length() + 1);
#if wxUSE_DDE_FOR_IPC #if wxUSE_DDE_FOR_IPC
wxLogMessage(_T("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.")); wxLogMessage("DDE Advise type argument cannot be wxIPC_PRIVATE. "
#endif "The client will receive it as wxIPC_TEXT, "
"and receive the correct no of bytes, "
"but not print a correct log entry.");
#endif // DDE
char bytes[3]; char bytes[3];
bytes[0] = '1'; bytes[1] = '2'; bytes[2] = '3'; bytes[0] = '1'; bytes[1] = '2'; bytes[2] = '3';
Advise(m_sAdvise, bytes, 3, wxIPC_PRIVATE); Advise(m_sAdvise, bytes, 3, wxIPC_PRIVATE);
@@ -263,12 +267,12 @@ void MyConnection::Notify()
bool MyConnection::DoAdvise(const wxString& item, const void *data, size_t size, wxIPCFormat format) bool MyConnection::DoAdvise(const wxString& item, const void *data, size_t size, wxIPCFormat format)
{ {
Log(_T("Advise"), _T(""), item, data, size, format); Log("Advise", "", item, data, size, format);
return wxConnection::DoAdvise(item, data, size, format); return wxConnection::DoAdvise(item, data, size, format);
} }
bool MyConnection::OnDisconnect() bool MyConnection::OnDisconnect()
{ {
wxLogMessage(_T("OnDisconnect()")); wxLogMessage("OnDisconnect()");
return true; return true;
} }