KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > rero > dcc > DataDCC


1 package rero.dcc;
2
3 import java.util.*;
4
5 public class DataDCC
6 {
7    protected static List dccList;
8    protected static HashMap resumeData;
9
10    public DataDCC()
11    {
12       if (dccList == null)
13       {
14          dccList = Collections.synchronizedList(new LinkedList());
15       }
16
17       if (resumeData == null)
18       {
19          resumeData = new HashMap();
20       }
21    }
22  
23    /** used by the resume stuff */
24    public void addConnection(String JavaDoc port, GenericDCC dcc)
25    {
26       dccList.add(dcc);
27       resumeData.put(port, dcc);
28    }
29
30    public LinkedList getConnections(int type, int state)
31    {
32       return getConnections(dccList, type, state);
33    }
34
35    public LinkedList getConnections(Collection dccCollection, int type, int state)
36    {
37       LinkedList rv = new LinkedList();
38
39       Iterator iter = dccCollection.iterator();
40       while (iter.hasNext())
41       {
42           GenericDCC temp = (GenericDCC)iter.next();
43           if ((type == -1 || temp.getTypeOfDCC() == type) && (state == -1 || temp.getState() == state))
44               rv.add(temp);
45       }
46
47       return rv;
48    }
49
50    public List getAllConnections()
51    {
52       return dccList;
53    }
54
55    public GenericDCC getConnectionFromHash(String JavaDoc hash)
56    {
57       Iterator i = getAllConnections().iterator();
58       while (i.hasNext())
59       {
60          GenericDCC temp = (GenericDCC)i.next();
61          if (temp.getImplementation().toString().equals(hash))
62            return temp;
63       }
64
65       return null;
66    }
67
68    public Iterator getActiveConnections()
69    {
70       return getConnections(-1, ProtocolDCC.STATE_OPEN).iterator();
71    }
72
73    public Iterator getWaitingConnections()
74    {
75       return getConnections(-1, ProtocolDCC.STATE_WAIT).iterator();
76    }
77
78    public Iterator getClosedConnections()
79    {
80       return getConnections(-1, ProtocolDCC.STATE_CLOSED).iterator();
81    }
82
83    public GenericDCC getUserConnection(Collection dccCollection, String JavaDoc nickname)
84    {
85       Iterator i = dccCollection.iterator();
86       while (i.hasNext())
87       {
88          GenericDCC temp = (GenericDCC)i.next();
89          if (temp.getNickname().equals(nickname))
90          {
91             return temp;
92          }
93       }
94
95       return null;
96    }
97  
98    public GenericDCC getConnection(String JavaDoc port)
99    {
100       return (GenericDCC)resumeData.get(port);
101    }
102
103    public GenericDCC getConnectionToAccept(String JavaDoc nickname)
104    {
105       return getUserConnection(getConnections(-1, ProtocolDCC.STATE_WAIT), nickname);
106    }
107
108    public void closeConnection(String JavaDoc nickname)
109    {
110       closeConnection(nickname, -1);
111    }
112
113    public void closeConnection(String JavaDoc nickname, int type)
114    {
115       GenericDCC temp = getUserConnection(getConnections(type, ProtocolDCC.STATE_OPEN), nickname);
116       if (temp != null)
117          temp.getImplementation().close();
118    }
119
120    public void closeChat(String JavaDoc nickname)
121    {
122       ProtocolDCC temp = getSpecificConnection(nickname, ProtocolDCC.DCC_CHAT);
123       if (temp != null)
124          temp.close();
125    }
126
127    public void removeConnection(GenericDCC dcc)
128    {
129       dccList.remove(dcc.getImplementation());
130       connectionCache.remove(dcc.getNickname());
131    }
132
133    protected HashMap connectionCache = new HashMap();
134
135    public ProtocolDCC getSpecificConnection(String JavaDoc nickname, int type)
136    {
137       if (connectionCache.containsKey(nickname))
138       {
139          ProtocolDCC connection = (ProtocolDCC)connectionCache.get(nickname);
140
141          if (connection.getState() == ProtocolDCC.STATE_OPEN && connection.getTypeOfDCC() == type)
142          {
143             return connection;
144          }
145
146          connectionCache.remove(nickname);
147       }
148
149       Iterator i = getActiveConnections();
150       while (i.hasNext())
151       {
152          ProtocolDCC connection = ((GenericDCC)i.next()).getImplementation();
153          if (connection.getTypeOfDCC() == type && connection.getNickname().equals(nickname))
154          {
155              connectionCache.put(nickname, connection);
156              return connection;
157          }
158       }
159
160       return null;
161    }
162 }
163
Popular Tags