From 1529d2e5d4ceab8f04f33f5f8ccb45f9643b9ca3 Mon Sep 17 00:00:00 2001 From: "Uwe L. Korn" Date: Fri, 14 Jun 2013 20:19:05 +0200 Subject: [PATCH] Add API 2.0 / Control API skeleton --- src/tomahawk/CMakeLists.txt | 6 ++- src/tomahawk/TomahawkApp.cpp | 84 +++++++++++++++++++++++++++-------- src/tomahawk/TomahawkApp.h | 2 + src/tomahawk/web/Api_v2.cpp | 55 +++++++++++++++++++++++ src/tomahawk/web/Api_v2.h | 46 +++++++++++++++++++ src/tomahawk/web/Api_v2_0.cpp | 36 +++++++++++++++ src/tomahawk/web/Api_v2_0.h | 46 +++++++++++++++++++ 7 files changed, 255 insertions(+), 20 deletions(-) create mode 100644 src/tomahawk/web/Api_v2.cpp create mode 100644 src/tomahawk/web/Api_v2.h create mode 100644 src/tomahawk/web/Api_v2_0.cpp create mode 100644 src/tomahawk/web/Api_v2_0.h diff --git a/src/tomahawk/CMakeLists.txt b/src/tomahawk/CMakeLists.txt index 7ffb3dead..155ade442 100644 --- a/src/tomahawk/CMakeLists.txt +++ b/src/tomahawk/CMakeLists.txt @@ -107,7 +107,11 @@ INCLUDE_DIRECTORIES( ) IF( QXTWEB_FOUND ) - LIST(APPEND tomahawkSources web/Api_v1.cpp) + LIST(APPEND tomahawkSources + web/Api_v1.cpp + web/Api_v2.cpp + web/Api_v2_0.cpp + ) LIST(APPEND LINK_LIBRARIES ${QXTWEB_LIBRARIES}) INCLUDE_DIRECTORIES(${QXTWEB_INCLUDE_DIRS}) ENDIF() diff --git a/src/tomahawk/TomahawkApp.cpp b/src/tomahawk/TomahawkApp.cpp index dbb07c2e8..60766fdf8 100644 --- a/src/tomahawk/TomahawkApp.cpp +++ b/src/tomahawk/TomahawkApp.cpp @@ -4,6 +4,7 @@ * Copyright 2010-2011, Leo Franchi * Copyright 2010-2012, Jeff Mitchell * Copyright 2013, Teo Mrnjavac + * Copyright 2013, Uwe L. Korn * * Tomahawk is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -38,6 +39,7 @@ #include "network/Servent.h" #include "network/DbSyncConnection.h" #include "web/Api_v1.h" +#include "web/Api_v2.h" #include "SourceList.h" #include "ViewManager.h" #include "ShortcutHandler.h" @@ -490,36 +492,80 @@ TomahawkApp::initHTTP() delete m_httpv1_session.data(); if ( !m_httpv1_connector.isNull() ) delete m_httpv1_connector.data(); + if ( !m_httpv2_session.isNull() ) + delete m_httpv2_session.data(); + if ( !m_httpv2_connector.isNull() ) + delete m_httpv2_connector.data(); return; } if ( m_httpv1_session ) { - tLog() << "HTTPd session already exists, returning"; - return; + tLog() << "HTTPd session for API v1.0 already exists, not starting again"; } - - m_httpv1_session = QPointer< QxtHttpSessionManager >( new QxtHttpSessionManager() ); - m_httpv1_connector = QPointer< QxtHttpServerConnector >( new QxtHttpServerConnector ); - if ( m_httpv1_session.isNull() || m_httpv1_connector.isNull() ) + else { - if ( !m_httpv1_session.isNull() ) - delete m_httpv1_session.data(); - if ( !m_httpv1_connector.isNull() ) - delete m_httpv1_connector.data(); - tLog() << "Failed to start HTTPd, could not create object"; - return; + m_httpv1_session = QPointer< QxtHttpSessionManager >( new QxtHttpSessionManager() ); + m_httpv1_connector = QPointer< QxtHttpServerConnector >( new QxtHttpServerConnector ); + if ( m_httpv1_session.isNull() || m_httpv1_connector.isNull() ) + { + if ( !m_httpv1_session.isNull() ) + delete m_httpv1_session.data(); + if ( !m_httpv1_connector.isNull() ) + delete m_httpv1_connector.data(); + tLog() << "Failed to start HTTPd for API v1.0, could not create object"; + } + else + { + m_httpv1_session.data()->setPort( 60210 ); //TODO config + m_httpv1_session.data()->setListenInterface( QHostAddress::LocalHost ); + m_httpv1_session.data()->setConnector( m_httpv1_connector.data() ); + + Api_v1* api = new Api_v1( m_httpv1_session.data() ); + m_httpv1_session.data()->setStaticContentService( api ); + + tLog() << "Starting HTTPd for API v1.0 on" << m_httpv1_session.data()->listenInterface().toString() << m_httpv1_session.data()->port(); + m_httpv1_session.data()->start(); + } } - m_httpv1_session.data()->setPort( 60210 ); //TODO config - m_httpv1_session.data()->setListenInterface( QHostAddress::LocalHost ); - m_httpv1_session.data()->setConnector( m_httpv1_connector.data() ); + if ( !m_httpv2_session.isNull() ) + { + tLog() << "HTTPd session for API v2.0 already exists, not starting again"; + } + else + { + m_httpv2_session = QPointer< QxtHttpSessionManager >( new QxtHttpSessionManager() ); + m_httpv2_connector = QPointer< QxtHttpServerConnector >( new QxtHttpServerConnector ); + if ( m_httpv2_session.isNull() || m_httpv2_connector.isNull() ) + { + if ( !m_httpv2_session.isNull() ) + delete m_httpv2_session.data(); + if ( !m_httpv2_connector.isNull() ) + delete m_httpv2_connector.data(); + tLog() << "Failed to start HTTPd for API v2.0, could not create object"; + } + else + { + m_httpv2_session->setPort( 60211 ); //TODO config +#if QT_VERSION >= QT_VERSION_CHECK( 5, 0, 0 ) + m_httpv2_session.data()->setListenInterface( QHostAddress::Any ); +#else + m_httpv2_session.data()->setListenInterface( QHostAddress::AnyIPv6 ); +#endif - Api_v1* api = new Api_v1( m_httpv1_session.data() ); - m_httpv1_session.data()->setStaticContentService( api ); + m_httpv2_session->setConnector( m_httpv2_connector.data() ); - tLog() << "Starting HTTPd on" << m_httpv1_session.data()->listenInterface().toString() << m_httpv1_session.data()->port(); - m_httpv1_session.data()->start(); + Api_v2* api = new Api_v2( m_httpv2_session.data() ); + m_httpv2_session->setStaticContentService( api ); + + tLog() << "Starting HTTPd for API v2.0 on" << m_httpv2_session->listenInterface().toString() << m_httpv2_session->port(); + if ( !m_httpv2_session->start() ) + { + tLog() << "Failed to start HTTPd for API v2.0, could not bind to interface/port"; + } + } + } } diff --git a/src/tomahawk/TomahawkApp.h b/src/tomahawk/TomahawkApp.h index 2d9ab6f2d..2bfc5f38b 100644 --- a/src/tomahawk/TomahawkApp.h +++ b/src/tomahawk/TomahawkApp.h @@ -150,6 +150,8 @@ private: QPointer< QxtHttpServerConnector > m_httpv1_connector; QPointer< QxtHttpSessionManager > m_httpv1_session; + QPointer< QxtHttpServerConnector > m_httpv2_connector; + QPointer< QxtHttpSessionManager > m_httpv2_session; }; Q_DECLARE_METATYPE( PairList ) diff --git a/src/tomahawk/web/Api_v2.cpp b/src/tomahawk/web/Api_v2.cpp new file mode 100644 index 000000000..497faf49f --- /dev/null +++ b/src/tomahawk/web/Api_v2.cpp @@ -0,0 +1,55 @@ +/* === This file is part of Tomahawk Player - === + * + * Copyright 2013, Uwe L. Korn + * + * Tomahawk is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Tomahawk is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Tomahawk. If not, see . + */ + +#include "Api_v2.h" + +#include + +Api_v2::Api_v2( QxtAbstractWebSessionManager* sm, QObject* parent ) + : QxtWebSlotService( sm, parent ) + , m_apiv2_0( this ) +{ +} + +void +Api_v2::api( QxtWebRequestEvent *event, const QString &version, const QString &method ) +{ + if ( version == "2.0" ) + { + if ( !QMetaObject::invokeMethod( &m_apiv2_0, method.toAscii().constData(), Q_ARG( QxtWebRequestEvent*, event ) ) ) + { + // Method call failed, report failure + sendPlain404( event, QString( "Method \"%1\" for API 2.0 not found" ).arg( method ), "Method in API 2.0 not found" ); + } + + } + else + { + sendPlain404( event, QString( "Unknown API version %1" ).arg( version ), "API version not found" ); + } +} + +void +Api_v2::sendPlain404( QxtWebRequestEvent *event, const QString &message, const QString &statusmessage ) +{ + QxtWebPageEvent * e = new QxtWebPageEvent( event->sessionID, event->requestID, message.toUtf8() ); + e->contentType = "text/plain"; + e->status = 404; + e->statusMessage = statusmessage.toAscii().constData(); + postEvent( e ); +} diff --git a/src/tomahawk/web/Api_v2.h b/src/tomahawk/web/Api_v2.h new file mode 100644 index 000000000..55bf06949 --- /dev/null +++ b/src/tomahawk/web/Api_v2.h @@ -0,0 +1,46 @@ +/* === This file is part of Tomahawk Player - === + * + * Copyright 2013, Uwe L. Korn + * + * Tomahawk is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Tomahawk is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Tomahawk. If not, see . + */ + +#ifndef API_V2_H +#define API_V2_H + +#include "Api_v2_0.h" + +#include + +#include + +class Api_v2 : public QxtWebSlotService +{ + Q_OBJECT +public: + Api_v2( QxtAbstractWebSessionManager* sm, QObject* parent = 0 ); + +signals: + +public slots: + /** + * All api (non-UI) calls with go to /api//method + */ + void api( QxtWebRequestEvent* event, const QString& version, const QString& method ); +private: + void sendPlain404( QxtWebRequestEvent* event, const QString& message, const QString& statusmessage ); + Api_v2_0 m_apiv2_0; +}; + +#endif // API_V2_H diff --git a/src/tomahawk/web/Api_v2_0.cpp b/src/tomahawk/web/Api_v2_0.cpp new file mode 100644 index 000000000..222bd1dd9 --- /dev/null +++ b/src/tomahawk/web/Api_v2_0.cpp @@ -0,0 +1,36 @@ +/* === This file is part of Tomahawk Player - === + * + * Copyright 2013, Uwe L. Korn + * + * Tomahawk is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Tomahawk is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Tomahawk. If not, see . + */ + +#include "Api_v2_0.h" + +#include +#include + +Api_v2_0::Api_v2_0( QxtWebSlotService *parent ) + : QObject( parent ) + , m_service( parent ) +{ +} + +void +Api_v2_0::ping( QxtWebRequestEvent *event ) +{ + QxtWebPageEvent * e = new QxtWebPageEvent( event->sessionID, event->requestID, "pong" ); + e->contentType = "text/plain"; + m_service->postEvent( e ); +} diff --git a/src/tomahawk/web/Api_v2_0.h b/src/tomahawk/web/Api_v2_0.h new file mode 100644 index 000000000..e649cf212 --- /dev/null +++ b/src/tomahawk/web/Api_v2_0.h @@ -0,0 +1,46 @@ +/* === This file is part of Tomahawk Player - === + * + * Copyright 2013, Uwe L. Korn + * + * Tomahawk is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Tomahawk is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Tomahawk. If not, see . + */ + +#ifndef API_V2_0_H +#define API_V2_0_H + +#include + +class QxtWebRequestEvent; +class QxtWebSlotService; + +class Api_v2_0 : public QObject +{ + Q_OBJECT +public: + Api_v2_0( QxtWebSlotService *parent = 0 ); + +signals: + +public slots: + /** + * Simple test to check for API 2.0 support. + * + * This call needs no authentication. + */ + void ping( QxtWebRequestEvent* event ); +private: + QxtWebSlotService* m_service; +}; + +#endif // API_V2_0_H