| 1 | /* |
| 2 | * libMaia - maiaXmlRpcServer.cpp |
| 3 | * Copyright (c) 2007 Sebastian Wiedenroth <[email protected]> |
| 4 | * and Karl Glatz |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions |
| 8 | * are met: |
| 9 | * |
| 10 | * 1. Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * 2. Redistributions in binary form must reproduce the above copyright |
| 13 | * notice, this list of conditions and the following disclaimer in the |
| 14 | * documentation and/or other materials provided with the distribution. |
| 15 | * |
| 16 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 17 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 18 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 19 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 21 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 22 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 23 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 25 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | */ |
| 27 | |
| 28 | #include "maiaXmlRpcServer.h" |
| 29 | #include "maiaFault.h" |
| 30 | |
| 31 | MaiaXmlRpcServer::MaiaXmlRpcServer(const QHostAddress &address, quint16 port, QObject* parent) : QObject(parent) { |
| 32 | allowedAddresses = NULL; |
| 33 | connect(&server, SIGNAL(newConnection()), this, SLOT(newConnection())); |
| 34 | server.listen(address, port); |
| 35 | } |
| 36 | |
| 37 | MaiaXmlRpcServer::MaiaXmlRpcServer(quint16 port, QObject* parent) : QObject(parent) { |
| 38 | allowedAddresses = NULL; |
| 39 | connect(&server, SIGNAL(newConnection()), this, SLOT(newConnection())); |
| 40 | server.listen(QHostAddress::Any, port); |
| 41 | } |
| 42 | |
| 43 | MaiaXmlRpcServer::MaiaXmlRpcServer(const QHostAddress &address, quint16 port, QList<QHostAddress> *allowedAddresses, QObject *parent) : QObject(parent) { |
| 44 | this->allowedAddresses = allowedAddresses; |
| 45 | connect(&server, SIGNAL(newConnection()), this, SLOT(newConnection())); |
| 46 | server.listen(address, port); |
| 47 | } |
| 48 | |
| 49 | void MaiaXmlRpcServer::addMethod(QString method, |
| 50 | QObject* responseObject, const char* responseSlot) { |
| 51 | objectMap[method] = responseObject; |
| 52 | slotMap[method] = responseSlot; |
| 53 | } |
| 54 | |
| 55 | void MaiaXmlRpcServer::removeMethod(QString method) { |
| 56 | objectMap.remove(method); |
| 57 | slotMap.remove(method); |
| 58 | } |
| 59 | |
| 60 | void MaiaXmlRpcServer::getMethod(QString method, QObject **responseObject, const char **responseSlot) { |
| 61 | if(!objectMap.contains(method)) { |
| 62 | *responseObject = NULL; |
| 63 | *responseSlot = NULL; |
| 64 | return; |
| 65 | } |
| 66 | *responseObject = objectMap[method]; |
| 67 | *responseSlot = slotMap[method]; |
| 68 | } |
| 69 | |
| 70 | void MaiaXmlRpcServer::newConnection() { |
| 71 | QTcpSocket *connection = server.nextPendingConnection(); |
| 72 | if (!this->allowedAddresses || this->allowedAddresses->isEmpty() || this->allowedAddresses->contains(connection->peerAddress())) { |
| 73 | MaiaXmlRpcServerConnection *client = new MaiaXmlRpcServerConnection(connection, this); |
| 74 | connect(client, SIGNAL(getMethod(QString, QObject **, const char**)), |
| 75 | this, SLOT(getMethod(QString, QObject **, const char**))); |
| 76 | } else { |
| 77 | qWarning() << "Rejected connection attempt from" << connection->peerAddress().toString(); |
| 78 | connection->disconnectFromHost(); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | QHostAddress MaiaXmlRpcServer::getServerAddress() { |
| 83 | return server.serverAddress(); |
| 84 | } |
| 85 |