aboutsummaryrefslogtreecommitdiffstats
path: root/src/net/java/sip/communicator/service/protocol/ActiveCallsRepository.java
blob: 11a1faf5f8da0b421d158e14d7326f646a5bcf9c (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
/*
 * SIP Communicator, the OpenSource Java VoIP and Instant Messaging client.
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */
package net.java.sip.communicator.service.protocol;

import java.util.*;

import net.java.sip.communicator.service.protocol.event.*;
import net.java.sip.communicator.util.*;

/**
 * Keeps a list of all calls currently active and maintained by this protocol
 * provider. Offers methods for finding a call by its ID, peer session
 * and others. This class is meant for use by protocol implementations and
 * cannot be accessed from other bundles.
 *
 * @author Emil Ivov
 */
public class ActiveCallsRepository<T extends Call,
                                   U extends AbstractOperationSetBasicTelephony>
    extends CallChangeAdapter
{
    /**
     * The <tt>Logger</tt> used by the <tt>ActiveCallsRepository</tt>
     * class and its instances for logging output.
     */
    private static final Logger logger = Logger
                    .getLogger(ActiveCallsRepository.class.getName());

    /**
     * A table mapping call ids against call instances.
     */
    private Hashtable<String, T> activeCalls
                                    = new Hashtable<String, T>();

    /**
     * The operation set that created us. Instance is mainly used for firing
     * events when necessary.
     */
    private final U parentOperationSet;

    /**
     * Creates a new instance of this repository.
     *
     * @param opSet a reference to the
     * <tt>AbstractOperationSetBasicTelephony</tt> extension that created us.
     */
    public ActiveCallsRepository(U opSet)
    {
        this.parentOperationSet = opSet;
    }

    /**
     * Adds the specified call to the list of calls tracked by this repository.
     * @param call CallSipImpl
     */
    public void addCall(T call)
    {
        activeCalls.put(call.getCallID(), call);
        call.addCallChangeListener(this);
    }

    /**
     * If <tt>evt</tt> indicates that the call has been ended we remove it from
     * the repository.
     * @param evt the <tt>CallChangeEvent</tt> instance containing the source
     * calls and its old and new state.
     */
    public void callStateChanged(CallChangeEvent evt)
    {
        if(evt.getEventType().equals(CallChangeEvent.CALL_STATE_CHANGE)
        && evt.getNewValue().equals(CallState.CALL_ENDED))
        {
            T sourceCall =
                this.activeCalls.remove(evt.getSourceCall().getCallID());

            if (logger.isTraceEnabled())
                logger.trace("Removing call " + sourceCall + " from the list of "
                        + "active calls because it entered an ENDED state");

            this.parentOperationSet.fireCallEvent(
                CallEvent.CALL_ENDED, sourceCall);
        }
    }

    /**
     * Returns an iterator over all currently active (non-ended) calls.
     *
     * @return an iterator over all currently active (non-ended) calls.
     */
    public Iterator<T> getActiveCalls()
    {
        synchronized(activeCalls)
        {
            return new LinkedList<T>(activeCalls.values()).iterator();
        }
    }

    /**
     * Returns the number of calls currently tracked by this repository.
     *
     * @return the number of calls currently tracked by this repository.
     */
    public int getActiveCallCount()
    {
        synchronized (activeCalls)
        {
            return this.activeCalls.size();
        }
    }

}