summaryrefslogtreecommitdiffstats
path: root/kopete/protocols/oscar/liboscar/rateclassmanager.cpp
blob: 65f3764803298d6b002021b7012445f30b6e5c3b (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
/*
    Kopete Oscar Protocol
    rateclassmanager.cpp - Manages the rates we get from the OSCAR server

    Copyright (c) 2004 Matt Rogers <[email protected]>

    Kopete (c) 2002-2004 by the Kopete developers <[email protected]>

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

#include <tqvaluelist.h>
#include <kdebug.h>


#include "rateclassmanager.h"
#include "transfer.h"
#include "connection.h"
#include "rateclass.h"


class RateClassManagerPrivate
{
public:
	//! The list of rate classes owned by this manager
	TQValueList<RateClass*> classList;
	Connection* client;
};

RateClassManager::RateClassManager( Connection* parent, const char* name )
: TQObject( parent, name )
{
	d = new RateClassManagerPrivate();
	d->client = parent;
}

RateClassManager::~RateClassManager()
{
	reset();
	delete d;
}

void RateClassManager::reset()
{
	TQValueList<RateClass*>::iterator it = d->classList.begin();
	while ( it != d->classList.end() && d->classList.count() > 0)
	{
		RateClass* rc = ( *it );
		it = d->classList.remove( it );
		delete rc;
	}
}

void RateClassManager::registerClass( RateClass* rc )
{
	TQObject::connect( rc, TQ_SIGNAL( dataReady( Transfer* ) ), this, TQ_SLOT( transferReady( Transfer* ) ) );
	d->classList.append( rc );
}

bool RateClassManager::canSend( Transfer* t ) const
{
	SnacTransfer* st = dynamic_cast<SnacTransfer*>( t );

	if ( !st ) //no snac transfer, no rate limiting
	{ kdDebug(OSCAR_RAW_DEBUG) << k_funcinfo << "Not sending a snac" << endl;
		return true;
	}

	RateClass* rc = findRateClass( st );
	if ( rc )
	{ 
		if ( rc->timeToNextSend() == 0 )
		{
			kdDebug(OSCAR_RAW_DEBUG) << k_funcinfo << "rate class " << rc->id() << " said it's okay to send" << endl;
			return true;
		}
		else
		{
			kdDebug(OSCAR_RAW_DEBUG) << k_funcinfo << "rate class " << rc->id() << " said it's not okay to send yet" << endl;
			return false;
		}
	}
	else // no rate class
	{
		kdDebug(OSCAR_RAW_DEBUG) << k_funcinfo << "no rate class. doing no rate limiting" << endl;
		return true;
	}
}

void RateClassManager::queue( Transfer* t )
{
	SnacTransfer* st = dynamic_cast<SnacTransfer*>( t );
	if ( !st )
	{ //we're not sending a snac
		transferReady( t );
		return;
	}

	RateClass* rc = findRateClass( st );
	if ( rc )
		rc->enqueue( st );
	else
		transferReady( t );
}

TQValueList<RateClass*> RateClassManager::classList() const
{
	return d->classList;
}

void RateClassManager::transferReady( Transfer* t )
{
	//tell the client to send it again. We should be
	//able to send it now
	FlapTransfer* ft = dynamic_cast<FlapTransfer*>( t );
	
	if ( ft )
		ft->setFlapSequence( d->client->flapSequence() );
	
	d->client->forcedSend( t );
}


RateClass* RateClassManager::findRateClass( SnacTransfer* st ) const
{
	SNAC s = st->snac();
	//kdDebug(OSCAR_RAW_DEBUG) << k_funcinfo << "Looking for SNAC " << s.family << ", " << s.subtype << endl;
	RateClass* rc = 0L;
	TQValueList<RateClass*>::const_iterator it;
	TQValueList<RateClass*>::const_iterator rcEnd = d->classList.constEnd();

	for ( it = d->classList.constBegin(); it != rcEnd; ++it )
	{
		if ( ( *it )->isMember( s.family, s.subtype ) )
		{
			//kdDebug(OSCAR_RAW_DEBUG) << k_funcinfo << "Found SNAC(" << s.family << ", " << s.subtype << ") in class" << endl;
			rc = ( *it );
			break;
		}
	}

	return rc;
}

void RateClassManager::recalcRateLevels()
{
	TQValueList<RateClass*>::iterator it;
	TQValueList<RateClass*>::iterator rcEnd = d->classList.end();
	for ( it = d->classList.begin(); it != rcEnd; ++it )
		( *it )->updateRateInfo();
}

int RateClassManager::timeToInitialLevel( SNAC s )
{
	TQValueList<RateClass*>::const_iterator it;
	TQValueList<RateClass*>::const_iterator rcEnd = d->classList.constEnd();
	
	for ( it = d->classList.constBegin(); it != rcEnd; ++it )
	{
		if ( ( *it )->isMember( s.family, s.subtype ) )
		{
			return ( *it )->timeToInitialLevel();
		}
	}
	return 0;
}

#include "rateclassmanager.moc"