summaryrefslogtreecommitdiffstats
path: root/kopete/protocols/jabber/jingle/jinglesessionmanager.cpp
blob: c997941168e2094a9351ce5240a325eabee5686e (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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
    jinglesessionmanager.cpp - Manage Jingle sessions.

    Copyright (c) 2006      by Michaƫl Larouche     <michael.larouche@kdemail.net>

    Kopete    (c) 2001-2006 by the Kopete developers <kopete-devel@kde.org>

    *************************************************************************
    *                                                                       *
    * This program 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 2 of the License, or     *
    * (at your option) any later version.                                   *
    *                                                                       *
    *************************************************************************
*/
// libjingle before everything else to not clash with TQt
#define POSIX
#include "talk/xmpp/constants.h"
#include "talk/base/sigslot.h"
#include "talk/xmpp/jid.h"
#include "talk/xmllite/xmlelement.h"
#include "talk/xmllite/xmlprinter.h"
#include "talk/base/network.h"
#include "talk/p2p/base/session.h"
#include "talk/p2p/base/sessionmanager.h"
#include "talk/p2p/base/helpers.h"
#include "talk/p2p/client/basicportallocator.h"
#include "talk/p2p/client/sessionclient.h"
#include "talk/base/physicalsocketserver.h"
#include "talk/base/thread.h"
#include "talk/base/socketaddress.h"
#include "talk/session/phone/call.h"
#include "talk/session/phone/phonesessionclient.h"
#include "talk/session/sessionsendtask.h"


#include "jinglesessionmanager.h"

//#include "jinglesession.h"
#include "jinglevoicesession.h"

#include "jinglewatchsessiontask.h"

#include "jabberaccount.h"
#include "jabberprotocol.h"

#include <kdebug.h>

#define JINGLE_NS "http://www.google.com/session"
#define JINGLE_VOICE_SESSION_NS "http://www.google.com/session/phone"

//BEGIN JingleSessionManager::SlotsProxy
class JingleSessionManager;
class JingleSessionManager::SlotsProxy : public sigslot::has_slots<>
{
public:
	SlotsProxy(JingleSessionManager *parent)
	 : sessionManager(parent)
	{}
	
	void OnSignalingRequest()
	{
		kdDebug(JABBER_DEBUG_GLOBAL) << k_funcinfo << "Requesting Jingle signaling." << endl;
		sessionManager->cricketSessionManager()->OnSignalingReady();
	}
	
	
private:
	JingleSessionManager *sessionManager;
};

//END JingleSessionManager::SlotsProxy

//BEGIN JingleSessionManager::Private
class JingeSession;
class JingleSessionManager::Private
{
public:
	Private(JabberAccount *t_account)
	 : account(t_account), watchSessionTask(0L)
	{}

	~Private()
	{
		delete networkManager;
		delete portAllocator;
		delete sessionThread;
		delete cricketSessionManager;
	}
	
	JabberAccount *account;
	TQValueList<JingleSession*> sessionList;
	JingleWatchSessionTask *watchSessionTask;

	cricket::NetworkManager *networkManager;
	cricket::BasicPortAllocator *portAllocator;
	cricket::Thread *sessionThread;
	cricket::SessionManager *cricketSessionManager;
};
//END JingleSessionManager::Private

JingleSessionManager::JingleSessionManager(JabberAccount *account)
 : TQObject(account, 0), d(new Private(account))
{
	// Create slots proxy for libjingle
	slotsProxy = new SlotsProxy(this);

	// Create watch incoming session task.
	d->watchSessionTask = new JingleWatchSessionTask(account->client()->rootTask());
	connect(d->watchSessionTask, TQT_SIGNAL(watchSession(const TQString &, const TQString &)), this, TQT_SLOT(slotIncomingSession(const TQString &, const TQString &)));

	// Create global cricket variables common to all sessions.
	// Seed random generation with the JID of the account.
	TQString accountJid = account->client()->jid().full();
	cricket::InitRandom( accountJid.ascii(), accountJid.length() );

	// Create the libjingle NetworkManager that manager local network connections
	d->networkManager = new cricket::NetworkManager();
	
	// Init the port allocator(select best ports) with the Google STUN server to help.
	cricket::SocketAddress *googleStunAddress = new cricket::SocketAddress("64.233.167.126", 19302);
	// TODO: Define a relay server.
  	d->portAllocator = new cricket::BasicPortAllocator(d->networkManager, googleStunAddress, 0L);

	// Create the Session manager that manager peer-to-peer sessions.
	d->sessionThread = new cricket::Thread();
	d->cricketSessionManager = new cricket::SessionManager(d->portAllocator, d->sessionThread);
	d->cricketSessionManager->SignalRequestSignaling.connect(slotsProxy, &JingleSessionManager::SlotsProxy::OnSignalingRequest);
	d->cricketSessionManager->OnSignalingReady();

	d->sessionThread->Start();
}

JingleSessionManager::~JingleSessionManager()
{
	kdDebug(JABBER_DEBUG_GLOBAL) << k_funcinfo << endl;

	kdDebug(JABBER_DEBUG_GLOBAL) << k_funcinfo << "Cleaning up Jingle sessions." << endl;
	TQValueList<JingleSession*>::Iterator it, itEnd = d->sessionList.end();
	for(it = d->sessionList.begin(); it != itEnd; ++it)
	{
		JingleSession *deletedSession = *it;
		if( deletedSession )
		{
			kdDebug(JABBER_DEBUG_GLOBAL) << k_funcinfo << "deleting a session." << endl;
			delete deletedSession;
		}
	}
	kdDebug(JABBER_DEBUG_GLOBAL) << k_funcinfo << "Done Cleaning up Jingle sessions." << endl;

	delete d;
}

cricket::SessionManager *JingleSessionManager::cricketSessionManager()
{
	return d->cricketSessionManager;
}

JabberAccount *JingleSessionManager::account()
{
	return d->account;
}

JingleSession *JingleSessionManager::createSession(const TQString &sessionType, const JidList &peers)
{	
	JingleSession *newSession = 0L;

	if(sessionType == JINGLE_VOICE_SESSION_NS)
	{
		kdDebug(JABBER_DEBUG_GLOBAL) << k_funcinfo << "Creating a voice session" << endl;
		newSession = new JingleVoiceSession(account(), peers);
	}
	
	if(newSession)
		d->sessionList.append(newSession);

	return newSession;
}

JingleSession *JingleSessionManager::createSession(const TQString &sessionType, const XMPP::Jid &user)
{
	JingleSessionManager::JidList jidList;
	jidList.append(user);
	
	return createSession(sessionType, jidList);
}

void JingleSessionManager::removeSession(JingleSession *session)
{
	kdDebug(JABBER_DEBUG_GLOBAL) << k_funcinfo << "Removing a jingle session." << endl;

	d->sessionList.remove(session);
	delete session;	
}

void JingleSessionManager::slotIncomingSession(const TQString &sessionType, const TQString &initiator)
{
	kdDebug(JABBER_DEBUG_GLOBAL) << k_funcinfo << "Incoming session: " << sessionType << ". Initiator: " << initiator << endl;

	JingleSession *newSession = createSession(sessionType, XMPP::Jid(initiator));
	emit incomingSession(sessionType, newSession);
}

#include "jinglesessionmanager.moc"