summaryrefslogtreecommitdiffstats
path: root/ccallman.cpp
blob: d6ab9574e73e044c03fd6cf15309e498c1dbf0c9 (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
/*  Copyright 2008,2009,2010 Edwin Eefting (edwin@datux.nl) 

    This file is part of Synapse.

    Synapse 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 3 of the License, or
    (at your option) any later version.

    Synapse 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 General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Synapse.  If not, see <http://www.gnu.org/licenses/>. */













#include "ccallman.h"
#include "clog.h"
using namespace boost;
namespace synapse
{

CcallMan::CcallMan()
{
	statCallsTotal=0;
	statCallsQueuedMax=0;
	statCallsQueued=0;
}


CcallMan::~CcallMan()
{
}




/*!
    \fn CcallMan::addCall(CmsgPtr msg, CsessionPtr dst)
 */
void CcallMan::addCall(const CmsgPtr & msg, const CsessionPtr & dst, FsoHandler soHandler)
{
	callList.push_back(Ccall(msg,dst,soHandler));
	statCallsTotal++;
	statCallsQueued++;
	if (statCallsQueued>statCallsQueuedMax)
		statCallsQueuedMax=statCallsQueued;
//	DEB( "(" << callList.size() << ")" << " " << msg->event << " FROM " << msg->src << " TO " << msg->dst << " CALLDST " << dst->id);
}






/*!
    \fn CcallMan::popCall()
 */
CcallList::iterator CcallMan::startCall(const CthreadPtr & threadPtr)
{
	//find a call thats ready to go and return it
	//TODO:optimize 
	for (CcallList::iterator callI=callList.begin(); callI!=callList.end(); callI++)
	{
		if (!callI->started && callI->dst->startThread())
		{
			callI->threadPtr=threadPtr;
			callI->started=1;
			statCallsQueued--;
//			DEB( callI->msg->event << " FROM " << callI->msg->src << " TO " << callI->msg->dst << " CALLDST " << callI->dst->id);
		
			return callI;
		}
	}
	return CcallList::iterator();
}

void CcallMan::endCall(CcallList::iterator callI)
{
	callI->dst->endThread();
//	DEB( callI->msg->event << " FROM " << callI->msg->src << " TO " << callI->msg->dst << " CALLDST " << callI->dst->id);
	
	callList.erase(callI);
//	DEB("calls left: " << callList.size());
}


void CcallMan::getStatus(Cvar & var)
{
	for (CcallList::iterator callI=callList.begin(); callI!=callList.end(); callI++)
	{
		Cvar c;
        if (callI->started)
            c["started"]=true;
        else
            c["started"]=false;

        c["data"]=*callI->msg;
        c["event"]=callI->msg->event;
        c["src"]=callI->msg->src;
        c["dst"]=callI->dst->id;
        c["dstUserName"]=callI->dst->user->getName();
        c["dstModule"]=callI->dst->module->name;
        var["queue"].list().push_back(c);
	}

	var["statCallsQueued"]=statCallsQueued;
	var["statCallsQueuedMax"]=statCallsQueuedMax;
	var["statCallsTotal"]=statCallsTotal;

}

bool CcallMan::interruptCall(string event, int src, int dst)
{
	for (CcallList::iterator callI=callList.begin(); callI!=callList.end(); callI++)
	{
		if (callI->msg->event==event)
		{
			if (callI->msg->src==src)
			{
				if (dst==0 || dst==callI->dst->id)
				{	
					//send interrupt
					if (callI->started && callI->threadPtr)
					{
						DEB("Interrupting call: " << event << " FROM " << src << " TO " << dst);
						callI->threadPtr->interrupt();
						return (true);
					}
					//not started yet, we can just delete it from the queue
					{
						DEB("Cancelling call: " << event << " FROM " << src << " TO " << dst);
						callList.erase(callI);
						statCallsQueued--;
						return (true);

					}
				}
			}
		}
	}
	return false;
}

//interrupt all running calls (used for shutdown)
bool CcallMan::interruptAll()
{
	for (CcallList::iterator callI=callList.begin(); callI!=callList.end(); callI++)
	{
		//send interrupt
		if (callI->started && callI->threadPtr)
		{
			DEB("Interrupting call: " << callI->msg->event << " FROM " << callI->msg->src << " TO " <<
			callI->dst->id << ":" << callI->dst->user->getName() << "@" << callI->dst->module->name 
			<< callI->msg->getPrint("  |"));

			callI->threadPtr->interrupt();
		}
	}
	return false;
}
}