source: SMSSender/gateways/Smartphone/business/smartphoneconnector.cpp @ 364:32145f6c3e53

separation-frontend-backend
Last change on this file since 364:32145f6c3e53 was 364:32145f6c3e53, checked in by Sämy Zehnder <saemy.zehnder@…>, 5 years ago
  • Changes version to 3.1
  • Removes static -base libraries and instead merges them into the frontend projects. This is done in a way that they are still shared between the different frontends. However, the sources need to be recompiled for each frontend to ensure compatibility. The benefit is, that from now on, multiple frontends can be compiled at the same time with only one configuration. In particular, DEFINES+=FRONTEND_xxx changed to CONFIG+=build-frontend-xxx.
  • Creates gateway-common-gui.
File size: 4.9 KB
Line 
1/*
2  Smartphone plugin - The smssender plugin for connecting to smartphones.
3  Copyright (C) 2012-2014, gorrión. See http://smssender.gorrion.ch
4
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  GNU General Public License for more details.
14
15  You should have received a copy of the GNU General Public License
16  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17*/
18#include "smartphoneconnector.h"
19
20#include <QNetworkInterface>
21
22#include "common/exceptions/eexception.h"
23#include "gateways/Smartphone/business/connectionhandler.h"
24#include "gateways/Smartphone/business/packages/packagewizard.h"
25#include "gateways/Smartphone/business/smartphonemanager.h"
26
27namespace Smartphone {
28
29SmartphoneConnector::SmartphoneConnector(SmartphoneManager *smartphoneManager)
30    : QObject()
31    , m_smartphoneManager(smartphoneManager)
32{
33    m_server = new QTcpServer(this);
34    bool open = m_server->listen(QHostAddress::Any, PORT_SMSSENDER_APP);
35    if (!open) {
36        EException("Unable to open server socket")
37                .raise();
38    }
39
40    connect(m_server, SIGNAL(newConnection()), this, SLOT(acceptConnection()));
41
42
43    // Client polling setup
44    startClientPoll();
45
46    m_pollerTimer.setInterval(5000);
47    connect(&m_pollerTimer, SIGNAL(timeout()), this, SLOT(startClientPoll()));
48    m_pollerTimer.start();
49}
50SmartphoneConnector::~SmartphoneConnector() {
51    qDeleteAll(m_connections.values());
52    m_deviceConnections.clear();
53}
54
55
56/**
57 * Returns the XML-String with the connection-request data that a smartphone needs to be able
58 * to connect to us.
59 */
60QString SmartphoneConnector::createConnectionRequestPackage(const Smartphone &device) const {
61    // Get the pcIdent
62    QString pcIdent = m_smartphoneManager->pcIdent();
63
64    // Get the readable name
65    QString name = m_smartphoneManager->pcName();
66
67    return PackageWizard::createConnectionRequestPackage(pcIdent, name, device);
68}
69
70
71void SmartphoneConnector::acceptConnection() {
72    QTcpSocket *cSocket = m_server->nextPendingConnection();
73    handleSocket(cSocket);
74}
75
76void SmartphoneConnector::handleSocket(QTcpSocket *cSocket) {
77    handleSocket(cSocket, NULL);
78}
79void SmartphoneConnector::handleSocket(QTcpSocket *cSocket, const Smartphone &peerDevice) {
80    QHostAddress peerAddress = cSocket->peerAddress();
81    if (m_connections.contains(peerAddress)) {
82        cSocket->close(); // Do not open multiple sockets to the same device
83        delete cSocket;
84        return;
85    }
86
87    ConnectionHandler *handler = new ConnectionHandler(m_smartphoneManager, cSocket, peerDevice);
88    connect(handler, SIGNAL(peerIdentified(ConnectionHandler*)), this, SLOT(onPeerIdentified(ConnectionHandler*)));
89    connect(handler, SIGNAL(aboutToFinish(ConnectionHandler*)),
90            this, SLOT(onSocketHandlerFinished(ConnectionHandler*)), Qt::DirectConnection);
91    m_connections.insert(peerAddress, handler);
92}
93
94
95void SmartphoneConnector::onPeerIdentified(ConnectionHandler *connectionHandler) {
96    Q_ASSERT(connectionHandler->isPeerIdentified());
97    registerDeviceConnection(connectionHandler->peerDevice(), connectionHandler);
98}
99void SmartphoneConnector::onSocketHandlerFinished(ConnectionHandler *connectionHandler) {
100    unregisterDeviceConnection(connectionHandler->peerDevice());
101
102    for (QHash<QHostAddress, ConnectionHandler *>::iterator i = m_connections.begin(); i != m_connections.end(); ++i) {
103        if (i.value() == connectionHandler) {
104            m_connections.remove(i.key());
105            break;
106        }
107    }
108}
109
110void SmartphoneConnector::registerDeviceConnection(const Smartphone &device, ConnectionHandler *handler) {
111    if (m_deviceConnections.contains(device)) {
112        if (m_deviceConnections[device] != handler) {
113            handler->cancelWork();
114            return;
115        }
116    } else {
117        m_deviceConnections.insert(device, handler);
118    }
119
120    emit deviceConnected(device);
121}
122void SmartphoneConnector::unregisterDeviceConnection(const Smartphone &device) {
123    if (!m_deviceConnections.contains(device)) {
124        return;
125    }
126
127    m_deviceConnections.take(device);
128    emit deviceDisconnected(device);
129}
130
131QSet<Smartphone> SmartphoneConnector::connectedDevices() const {
132    return m_deviceConnections.keys().toSet();
133}
134bool SmartphoneConnector::isDeviceConnected(const Smartphone &device) const {
135    return m_deviceConnections.contains(device);
136}
137
138const ConnectionHandler * SmartphoneConnector::connectionHandler(const QHostAddress &address) const {
139    return m_connections.value(address);
140}
141const ConnectionHandler * SmartphoneConnector::connectionHandler(const Smartphone &device) const {
142    return m_deviceConnections.value(device);
143}
144
145
146}
Note: See TracBrowser for help on using the repository browser.