summaryrefslogtreecommitdiffstats
path: root/networkstatus/networkstatus.cpp
blob: 8e73e4e687bd0e921cea90eeb91e572950d9bcc0 (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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/*
    This file is part of kdepim.

    Copyright (c) 2005 Will Stephenson <lists@stevello.free-online.co.uk>

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library 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
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.
*/

#include "networkstatus.h"

#include <tqdict.h>
#include <tqtimer.h>
#include <tqvaluelist.h>

#include <dcopclient.h>
#include <kapplication.h>
#include <kdebug.h>

#include "clientifaceimpl.h"
#include "serviceifaceimpl.h"
#include "network.h"

#include <kdeversion.h>
#include <kdemacros.h>

#if KDE_IS_VERSION( 3,3,90 )
/* life is great */
#else
/* workaround typo that breaks compilation with newer gcc */
#undef KDE_EXPORT
#define KDE_EXPORT
#undef KDE_NO_EXPORT
#define KDE_NO_EXPORT
#endif

extern "C" {
	KDE_EXPORT KDEDModule* create_networkstatus( const TQCString& obj )
	{
		return new NetworkStatusModule( obj );
	}
}

// INTERNALLY USED STRUCTS AND TYPEDEFS

//typedef TQDict< Network > NetworkList;
typedef TQValueList< Network * > NetworkList;

class NetworkStatusModule::Private
{
public:
	NetworkList networks;
/*	ClientIface * clientIface;
	ServiceIface * serviceIface;*/
};

// CTORS/DTORS

NetworkStatusModule::NetworkStatusModule( const TQCString & obj ) : KDEDModule( obj )
{
	d = new Private;
/*	d->clientIface = new ClientIfaceImpl( this );
	d->serviceIface = new ServiceIfaceImpl( this );*/
	connect( kapp->dcopClient(), TQT_SIGNAL( applicationRemoved( const TQCString& ) ) , this, TQT_SLOT( unregisteredFromDCOP( const TQCString& ) ) );
	connect( kapp->dcopClient(), TQT_SIGNAL( applicationRegistered( const TQCString& ) ) , this, TQT_SLOT( registeredToDCOP( const TQCString& ) ) );
}

NetworkStatusModule::~NetworkStatusModule()
{
/*	delete d->clientIface;
	delete d->serviceIface;*/
	delete d;
}

// CLIENT INTERFACE

TQStringList NetworkStatusModule::networks()
{
	kdDebug() << k_funcinfo << " contains " << d->networks.count() << " networks" << endl;
	TQStringList networks;
	NetworkList::iterator end = d->networks.end();
	NetworkList::iterator it = d->networks.begin();
	for ( ; it != end; ++it )
		networks.append( (*it)->name() );
	return networks;
}

int NetworkStatusModule::status( const TQString & host )
{
	if ( host == "127.0.0.1" || host == "localhost" )
		return NetworkStatus::Online;
	Network * p = networkForHost( host );
	if ( !p )
	{
		//kdDebug() << k_funcinfo << " no networks have status for host '" << host << "'" << endl;
		return (int)NetworkStatus::NoNetworks;
	}
	else
	{	
		kdDebug() << k_funcinfo << " got status for host '" << host << "' : " << (int)(p->status()) << endl;
		return (int)(p->status());
	}
}

int NetworkStatusModule::request( const TQString & host, bool userInitiated )
{
	// identify most suitable network for host
	Network * p = networkForHost( host );
	if ( !p )
		return NetworkStatus::Unavailable;
	
	NetworkStatus::EnumStatus status = p->status();
	TQCString appId = kapp->dcopClient()->senderId();
	if ( status == NetworkStatus::Online )
	{
		p->registerUsage( appId, host );
		return NetworkStatus::Connected;
	}
	// if online
	//   register usage
	//   return Available
	else if ( status == NetworkStatus::Establishing )
	{
		p->registerUsage( appId, host );
		return NetworkStatus::RequestAccepted;
	}
	// if establishing
	//   register usage
	//   return Accepted
	else if ( status == NetworkStatus::Offline || status == NetworkStatus::ShuttingDown )
	{
		// TODO: check on demand policy
		
		p->registerUsage( appId, host );
		return NetworkStatus::RequestAccepted;
	}
	// if offline or ShuttingDown
	//   check ODP::
	//   always or Permanent: register, return accepted
	//   user: check userInitiated, register, return Accepted or UserRefused
	//   never: return UserRefused
	else if ( status == NetworkStatus::OfflineFailed )
	{
		// TODO: check user's preference for dealing with failed networks
		p->registerUsage( appId, host );
		return NetworkStatus::RequestAccepted;
	}
	// if OfflineFailed
	//   check user's preference
	else if ( status == NetworkStatus::OfflineDisconnected )
	{
		return NetworkStatus::Unavailable;
	}
	else
		return NetworkStatus::Unavailable;
	// if OfflineDisconnected or NoNetworks
	//   return Unavailable
}

void NetworkStatusModule::relinquish( const TQString & host )
{
	TQCString appId = kapp->dcopClient()->senderId();
	// find network currently used by app for host...
	NetworkList::iterator end = d->networks.end();
	NetworkList::iterator it = d->networks.begin();
	for ( ; it != end; ++it )
	{
		Network * net = *it;
		NetworkUsageList usage = net->usage();
		NetworkUsageList::iterator end2 = usage.end();
		for ( NetworkUsageList::iterator usageIt = usage.begin(); usageIt != end2; ++usageIt )
		{
			if ( (*usageIt).appId == appId && (*usageIt).host == host )
			{
				// remove host usage record
				usage.remove( usageIt );
				// if requested shutdown flagged for network
				//  check if all hosts have relinquished
				//   call confirmShutDown on Service
				//checkShutdownOk();
			}
		}
	}
}

bool NetworkStatusModule::reportFailure( const TQString & host )
{
	// find network for host
	// check IP record.  remove IP usage record.  if other IP exists, return true.
	Q_UNUSED( host );
	kdDebug() << k_funcinfo << "NOT IMPLEMENTED" << endl;
	return false;
}

// PROTECTED UTILITY FUNCTIONS
/*
 * Determine the network to use for the supplied host
 */
Network * NetworkStatusModule::networkForHost( const TQString & host ) const
{
	// return a null pointer if no networks are registered
	if ( d->networks.isEmpty() )
		return 0;
	
	NetworkList::const_iterator it = d->networks.begin();
	Network * bestNetwork = *(it++);
	NetworkList::const_iterator end = d->networks.end();
 	for ( ; it != end; ++it )
	{
		if ( (*it)->reachabilityFor( host ) > bestNetwork->reachabilityFor( host ) )
		{
			bestNetwork = (*it);
		}
	}
	return bestNetwork;
}


void NetworkStatusModule::registeredToDCOP( const TQCString & appId )
{
}

void NetworkStatusModule::unregisteredFromDCOP( const TQCString & appId )
{
	// unregister any networks owned by a service that has just unregistered
	NetworkList::iterator it = d->networks.begin();
	NetworkList::iterator end = d->networks.end();
	for ( ; it != end; ++it )
	{
		if ( (*it)->service() == appId)
		{
			kdDebug() << k_funcinfo << "removing '" << (*it)->name() << "', registered by " << appId << endl;
			d->networks.remove( it );
			break;
		}
	}
}

// SERVICE INTERFACE //
void NetworkStatusModule::setNetworkStatus( const TQString & networkName, int st )
{
	kdDebug() << k_funcinfo << endl;
	NetworkStatus::EnumStatus status = (NetworkStatus::EnumStatus)st;
	Network * net = 0;
	NetworkList::iterator it = d->networks.begin();
	NetworkList::iterator end = d->networks.end();
	for ( ; it != end; ++it )
	{
		if ( (*it)->name() == networkName )
		{
			net = (*it);
			break;
		}
	}
	if ( net )
	{
		if ( net->status() == status )
			return;

		// update the status of the network
		net->setStatus( status );

		// notify for each host in use on that network
		NetworkUsageList usage = net->usage();
		NetworkUsageList::iterator end = usage.end();
		TQStringList notified;
		for ( NetworkUsageList::iterator it = usage.begin(); it != end; ++it )
		{
			// only notify once per host
			if ( !notified.tqcontains( (*it).host ) )
			{
				kdDebug() << "notifying statusChange of " << networkName << " to " << (int)status << 
						" because " << (*it).appId << " is using " << (*it).host << endl;
				/*d->clientIface->*/statusChange( (*it).host, (int)status );
				notified.append( (*it).host );
			}
		}

		// if we are now anything but Establishing or Online, reset the usage records for that network
		if ( !( net->status() == NetworkStatus::Establishing || net->status() == NetworkStatus::Establishing ) )
			net->removeAllUsage();
	}
	else
		kdDebug() << k_funcinfo << "No network found by this name" << endl;
}

void NetworkStatusModule::registerNetwork( const TQString & networkName, const NetworkStatus::Properties properties )
{
	kdDebug() << k_funcinfo << "registering '" << networkName << "', with status " << properties.status << endl;
	// TODO: check for re-registration, checking appid matches
	
	d->networks.append( new Network( networkName, properties ) );
}

void NetworkStatusModule::unregisterNetwork( const TQString & networkName )
{
	// TODO: check appid
	//d->networks.remove( networkName );
}

void NetworkStatusModule::requestShutdown( const TQString & networkName )
{
	Q_UNUSED( networkName );
	kdDebug() << k_funcinfo << "NOT IMPLEMENTED" << endl;
}

#include "networkstatus.moc"