summaryrefslogtreecommitdiff
path: root/sappeer.h
blob: 1c477c49046d547894eda44282a6bf46179d924b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#ifndef SAPPEER_H
#define SAPPEER_H

#include <QtCore/QObject>
#include <QtCore/QQueue>

#include "saprotocol.h"
#include "sapserviceinfo.h"

class WMSPeer;
class CapabilityPeer;
class SAPConnection;
class SAPConnectionRequest;
class SAPSocket;
class SAPAgent;

class SAPPeer : public QObject
{
	Q_OBJECT
public:
	explicit SAPPeer(SAProtocol::Role role, const QString &localName, const QString &peerName, QObject *parent = 0);
	~SAPPeer();

	SAPConnection* createServiceConnection(const QString &profile, const QString &requesterProfile, SAPServiceInfo::Role requesterRole);
	bool terminateServiceConnection(const QString &profile, const QString &requesterProfile, SAPServiceInfo::Role requesterRole);

	SAProtocol::Role role() const;
	QString localName() const;
	QString peerName() const;

signals:
	void connected();
	void disconnected();

protected:
	/** Transmits a frame to the underlying transport. */
	virtual void sendFrame(const QByteArray &data) = 0;

	CapabilityPeer *capabilityPeer();

	/** Writes data to the remote. */
	void writeDataToSession(int session, const QByteArray &data);
	/** Writes a control frame to the remote. */
	void writeControlToSession(int session, const QByteArray &control);

	void acceptServiceConnection(SAPConnectionRequest *connReq, int statusCode);

	/** Distributes data to the appropiate socket. */
	void handleSessionData(int session, const QByteArray &data);

	/** Distributes a control message to the apropiate socket. */
	void handleSessionControl(int session, const QByteArray &data);

	/** Perform service discovery once connected. */
	void handleConnected();
	void handleDisconnected();

private:
	int findUnusedSessionId() const;
	void handleDefaultSessionMessage(const QByteArray &message);

protected:
	WMSPeer *_wms;

private:
	const SAProtocol::Role _role;
	const QString _localName;
	const QString _peerName;

	QMap<QString, SAPConnection*> _conns;
	QMap<int, SAPSocket*> _sessions;

	friend class SAPSocket;
	friend class SAPConnectionRequest;
};

#endif // SAPPEER_H