2002-09-09 14:18:21 +00:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Name: server.cpp
|
|
|
|
// Purpose: Simple Soap Server
|
|
|
|
// Author: Guy
|
|
|
|
// Modified by:
|
|
|
|
// Created: 21/08/2002
|
|
|
|
// Copyright: (c) 2002 Guy
|
|
|
|
// Licence: wxWindows licence
|
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// ==========================================================================
|
|
|
|
// declarations
|
|
|
|
// ==========================================================================
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
// headers
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
2002-09-13 14:56:23 +00:00
|
|
|
#include <wx/wx.h>
|
|
|
|
#include <wx/socket.h>
|
2002-10-24 10:47:49 +00:00
|
|
|
#include "wx/wfstream.h"
|
2002-09-13 14:56:23 +00:00
|
|
|
|
|
|
|
#include <wx/config.h>
|
|
|
|
#include <wx/msw/iniconf.h>
|
|
|
|
|
2002-10-24 10:47:49 +00:00
|
|
|
#include "http.h"
|
2002-09-09 14:18:21 +00:00
|
|
|
#include "soap.h"
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
// classes
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// Define a new application type
|
|
|
|
class SoapServerApp : public wxApp
|
|
|
|
{
|
2002-10-24 10:47:49 +00:00
|
|
|
wxEvtHandler* m_server;
|
|
|
|
|
2002-09-09 14:18:21 +00:00
|
|
|
public:
|
|
|
|
virtual bool OnInit();
|
2002-10-24 10:47:49 +00:00
|
|
|
virtual int OnExit();
|
2002-09-09 14:18:21 +00:00
|
|
|
};
|
|
|
|
|
2002-10-24 10:47:49 +00:00
|
|
|
enum
|
2002-09-09 14:18:21 +00:00
|
|
|
{
|
2002-10-24 10:47:49 +00:00
|
|
|
SERVER_ID = 1001,
|
|
|
|
SOCKET_ID = 1002
|
|
|
|
};
|
2002-09-13 14:56:23 +00:00
|
|
|
|
2002-10-24 10:47:49 +00:00
|
|
|
class SoapServer : public wxEvtHandler
|
|
|
|
{
|
|
|
|
wxSocketServer* m_server;
|
|
|
|
wxFileOutputStream* m_log;
|
2002-09-09 14:18:21 +00:00
|
|
|
|
2002-10-24 10:47:49 +00:00
|
|
|
protected:
|
|
|
|
int GetDefaultPort() const;
|
|
|
|
void AppendText(const char* str);
|
2002-09-09 14:18:21 +00:00
|
|
|
void ProcessSoapCommand(wxSocketBase *sock);
|
|
|
|
|
2002-10-24 10:47:49 +00:00
|
|
|
public:
|
|
|
|
bool Ok() const { return m_server->Ok(); }
|
|
|
|
SoapServer();
|
|
|
|
~SoapServer();
|
2002-09-09 14:18:21 +00:00
|
|
|
|
2002-10-24 10:47:49 +00:00
|
|
|
void OnServerEvent(wxSocketEvent& event);
|
|
|
|
void OnSocketEvent(wxSocketEvent& event);
|
|
|
|
DECLARE_EVENT_TABLE();
|
2002-09-09 14:18:21 +00:00
|
|
|
};
|
|
|
|
|
2002-10-24 10:47:49 +00:00
|
|
|
BEGIN_EVENT_TABLE(SoapServer, wxEvtHandler)
|
|
|
|
EVT_SOCKET(SERVER_ID, SoapServer::OnServerEvent)
|
|
|
|
EVT_SOCKET(SOCKET_ID, SoapServer::OnSocketEvent)
|
2002-09-09 14:18:21 +00:00
|
|
|
END_EVENT_TABLE()
|
|
|
|
|
2002-10-24 10:47:49 +00:00
|
|
|
void SoapServer::AppendText(const char* str)
|
2002-09-13 14:56:23 +00:00
|
|
|
{
|
2002-10-24 10:47:49 +00:00
|
|
|
*m_log << str << "\r\n";
|
2002-09-13 14:56:23 +00:00
|
|
|
}
|
|
|
|
|
2002-10-24 10:47:49 +00:00
|
|
|
void SoapServer::ProcessSoapCommand(wxSocketBase *sock)
|
2002-09-09 14:18:21 +00:00
|
|
|
{
|
|
|
|
sock->SetFlags(wxSOCKET_NOWAIT);
|
|
|
|
|
|
|
|
// Read the data
|
2002-09-13 14:56:23 +00:00
|
|
|
wxChar buf[4096]; memset(buf, 0, sizeof(buf));
|
2002-09-09 14:18:21 +00:00
|
|
|
unsigned int len = sock->Read(buf, sizeof(buf)).LastCount();
|
2002-09-13 14:56:23 +00:00
|
|
|
AppendText(buf);
|
2002-09-09 14:18:21 +00:00
|
|
|
|
2002-10-24 10:47:49 +00:00
|
|
|
wxString str;
|
|
|
|
if (strncmp(buf, "POST", 4) == 0)
|
|
|
|
{
|
|
|
|
wxString strAnswer = SoapProcessMessage(buf, len);
|
|
|
|
sock->Write(strAnswer, strAnswer.Length());
|
|
|
|
AppendText(strAnswer);
|
|
|
|
} else
|
|
|
|
if (strncmp(buf, "GET ", 4) == 0)
|
|
|
|
{
|
|
|
|
str = buf;
|
|
|
|
HttpProcessMessage(str, sock, *m_log);
|
|
|
|
}
|
2002-09-09 14:18:21 +00:00
|
|
|
}
|
|
|
|
|
2002-10-24 10:47:49 +00:00
|
|
|
|
|
|
|
void SoapServer::OnServerEvent(wxSocketEvent& event)
|
2002-09-09 14:18:21 +00:00
|
|
|
{
|
2002-09-13 14:56:23 +00:00
|
|
|
wxString s = "OnServerEvent: ";
|
2002-09-09 14:18:21 +00:00
|
|
|
|
|
|
|
switch(event.GetSocketEvent())
|
|
|
|
{
|
2002-10-24 10:47:49 +00:00
|
|
|
case wxSOCKET_CONNECTION : s.Append("wxSOCKET_CONNECTION"); break;
|
|
|
|
default : s.Append("Unexpected event!"); break;
|
2002-09-09 14:18:21 +00:00
|
|
|
}
|
|
|
|
|
2002-09-13 14:56:23 +00:00
|
|
|
AppendText(s);
|
2002-09-09 14:18:21 +00:00
|
|
|
|
|
|
|
// Accept new connection if there is one in the pending
|
|
|
|
// connections queue, else exit. We use Accept(FALSE) for
|
|
|
|
// non-blocking accept (although if we got here, there
|
|
|
|
// should ALWAYS be a pending connection).
|
|
|
|
|
2002-10-24 10:47:49 +00:00
|
|
|
wxSocketBase* sock = m_server->Accept(FALSE);
|
2002-09-09 14:18:21 +00:00
|
|
|
if (sock)
|
|
|
|
{
|
2002-10-24 10:47:49 +00:00
|
|
|
AppendText("New client connection accepted");
|
2002-09-09 14:18:21 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2002-10-24 10:47:49 +00:00
|
|
|
AppendText("Error: couldn't accept a new connection");
|
2002-09-09 14:18:21 +00:00
|
|
|
sock->Destroy();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
sock->SetEventHandler(*this, SOCKET_ID);
|
|
|
|
sock->SetNotify(wxSOCKET_INPUT_FLAG | wxSOCKET_LOST_FLAG);
|
|
|
|
sock->Notify(TRUE);
|
|
|
|
}
|
|
|
|
|
2002-10-24 10:47:49 +00:00
|
|
|
void SoapServer::OnSocketEvent(wxSocketEvent& event)
|
2002-09-09 14:18:21 +00:00
|
|
|
{
|
2002-09-13 14:56:23 +00:00
|
|
|
wxString s = "OnSocketEvent: ";
|
2002-09-09 14:18:21 +00:00
|
|
|
wxSocketBase *sock = event.GetSocket();
|
|
|
|
|
|
|
|
// First, print a message
|
|
|
|
switch(event.GetSocketEvent())
|
|
|
|
{
|
2002-09-13 14:56:23 +00:00
|
|
|
case wxSOCKET_INPUT : s.Append("wxSOCKET_INPUT\n"); break;
|
|
|
|
case wxSOCKET_LOST : s.Append("wxSOCKET_LOST\n"); break;
|
|
|
|
default : s.Append("Unexpected event !\n"); break;
|
2002-09-09 14:18:21 +00:00
|
|
|
}
|
|
|
|
|
2002-09-13 14:56:23 +00:00
|
|
|
AppendText(s);
|
2002-09-09 14:18:21 +00:00
|
|
|
|
|
|
|
// Now we process the event
|
|
|
|
switch(event.GetSocketEvent())
|
|
|
|
{
|
|
|
|
case wxSOCKET_INPUT:
|
|
|
|
{
|
|
|
|
// We disable input events, so that the test doesn't trigger
|
|
|
|
// wxSocketEvent again.
|
|
|
|
sock->SetNotify(wxSOCKET_LOST_FLAG);
|
|
|
|
|
|
|
|
ProcessSoapCommand(sock);
|
|
|
|
|
|
|
|
// Enable input events again.
|
|
|
|
sock->SetNotify(wxSOCKET_LOST_FLAG | wxSOCKET_INPUT_FLAG);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case wxSOCKET_LOST:
|
|
|
|
{
|
|
|
|
// Destroy() should be used instead of delete wherever possible,
|
|
|
|
// due to the fact that wxSocket uses 'delayed events' (see the
|
|
|
|
// documentation for wxPostEvent) and we don't want an event to
|
|
|
|
// arrive to the event handler (the frame, here) after the socket
|
|
|
|
// has been deleted. Also, we might be doing some other thing with
|
|
|
|
// the socket at the same time; for example, we might be in the
|
|
|
|
// middle of a test or something. Destroy() takes care of all
|
|
|
|
// this for us.
|
|
|
|
|
2002-10-24 10:47:49 +00:00
|
|
|
AppendText("Deleting socket.");
|
2002-09-09 14:18:21 +00:00
|
|
|
sock->Destroy();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: ;
|
|
|
|
}
|
2002-10-24 10:47:49 +00:00
|
|
|
}
|
2002-09-09 14:18:21 +00:00
|
|
|
|
2002-10-24 10:47:49 +00:00
|
|
|
int SoapServer::GetDefaultPort() const
|
|
|
|
{
|
|
|
|
int nPort = 3883;
|
|
|
|
wxIniConfig ini("", "", "./campo.ini");
|
|
|
|
ini.SetPath("/Server");
|
|
|
|
const wxString str = ini.Read("Dictionary", "");
|
|
|
|
const int colon = str.Find(':');
|
|
|
|
if (colon > 0)
|
|
|
|
nPort = atoi(str.Mid(colon+1));
|
|
|
|
return nPort;
|
2002-09-09 14:18:21 +00:00
|
|
|
}
|
|
|
|
|
2002-10-24 10:47:49 +00:00
|
|
|
SoapServer::SoapServer()
|
|
|
|
{
|
|
|
|
// Create the address - defaults to localhost:0 initially
|
|
|
|
wxIPV4address addr;
|
|
|
|
addr.Service(GetDefaultPort());
|
|
|
|
|
|
|
|
// Create the socket
|
|
|
|
m_server = new wxSocketServer(addr);
|
|
|
|
|
|
|
|
m_log = new wxFileOutputStream("soaplog.txt");
|
|
|
|
|
|
|
|
wxString str;
|
|
|
|
|
|
|
|
// We use Ok() here to see if the server is really listening
|
|
|
|
if (m_server->Ok())
|
|
|
|
{
|
|
|
|
// Setup the event handler and subscribe to connection events
|
|
|
|
m_server->SetEventHandler(*this, SERVER_ID);
|
|
|
|
m_server->SetNotify(wxSOCKET_CONNECTION_FLAG);
|
|
|
|
m_server->Notify(TRUE);
|
|
|
|
|
|
|
|
str << "Server listening on port " << addr.Service();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
str << "Could not listen to port " << addr.Service();
|
|
|
|
}
|
|
|
|
AppendText(str);
|
|
|
|
}
|
|
|
|
|
|
|
|
SoapServer::~SoapServer()
|
|
|
|
{
|
|
|
|
delete m_server;
|
|
|
|
delete m_log;
|
|
|
|
}
|
|
|
|
|
|
|
|
IMPLEMENT_APP(SoapServerApp)
|
|
|
|
|
|
|
|
bool SoapServerApp::OnInit()
|
|
|
|
{
|
|
|
|
m_server = new SoapServer;
|
|
|
|
return TRUE;
|
|
|
|
}
|
2002-09-09 14:18:21 +00:00
|
|
|
|
2002-10-24 10:47:49 +00:00
|
|
|
int SoapServerApp::OnExit()
|
2002-09-09 14:18:21 +00:00
|
|
|
{
|
2002-10-24 10:47:49 +00:00
|
|
|
delete m_server;
|
|
|
|
return wxApp::OnExit();
|
2002-09-09 14:18:21 +00:00
|
|
|
}
|