View Javadoc

1   /*
2    *  XNap - A P2P framework and client.
3    *
4    *  See the file AUTHORS for copyright information.
5    *
6    *  This program is free software; you can redistribute it and/or modify
7    *  it under the terms of the GNU General Public License as published by
8    *  the Free Software Foundation.
9    *
10   *  This program is distributed in the hope that it will be useful,
11   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   *  GNU General Public License for more details.
14   *
15   *  You should have received a copy of the GNU General Public License
16   *  along with this program; if not, write to the Free Software
17   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   */
19  
20  package org.xnap.plugin.opennap.net;
21  
22  import java.beans.PropertyChangeListener;
23  import java.beans.PropertyChangeSupport;
24  
25  import org.xnap.XNap;
26  import org.xnap.plugin.opennap.OpenNapPlugin;
27  
28  public class OpenNapNetwork {
29  
30      //--- Constant(s) ---
31  
32      //--- Data field(s) ---
33  
34  	private static int totalConnecting = 0;
35  
36      private String status;
37  
38      private int servers;
39  
40      /***
41       * Number of servers in this network that are connected.
42       */
43      private int connected;
44  
45      /***
46       * Number of servers in this network that are connecting.
47       */
48      private int connecting;
49  
50      private PropertyChangeSupport pcs = new PropertyChangeSupport(this);
51      private String name;
52  
53      private int fileCount = -1;
54      private int fileSize = -1;
55      private int userCount = -1;
56  
57      public OpenNapNetwork(String name)
58      {
59  		this.name = name;
60      }
61  
62      //--- Method(s) ---
63  
64  	/***
65  	 * Returns the total number of networks that are currently connecting.
66  	 */
67  	public static int getTotalConnecting()
68  	{
69  		synchronized (OpenNapNetwork.class) {
70  			return totalConnecting;
71  		}
72  	}
73  
74      public void add(OpenNapServer s)
75      {
76  		synchronized (this) {
77  			servers++;
78  		}
79  		updateStatus();
80      }
81  
82      public void addPropertyChangeListener(PropertyChangeListener l)
83      {
84  		pcs.addPropertyChangeListener(l);
85      }
86  
87      /***
88       * Returns true if <code>obj</code> has the same hostname and port.
89       */
90      public boolean equals(Object obj)
91      {
92  		if (obj instanceof OpenNapNetwork) {
93  			return getName().equals(((OpenNapNetwork)obj).getName());
94  		}
95  	
96  		return false;
97      }
98  
99      public int getFileCount() 
100     {
101 		return fileCount;
102     }
103 
104     public int getFileSize() 
105     {
106 		return fileSize;
107     }
108 
109     public String getName()
110     {
111 		return name;
112     }
113 
114     public int getServers()
115     {
116 		return servers;
117     }
118 
119     public String getStatus()
120     {
121 		return status;
122     }
123 
124     public int getUserCount()
125     {
126 		return userCount;
127     }
128 
129     public boolean isBusy()
130     {
131 		return (connecting > 0 || connected > 0);
132     }
133 
134     public void remove(OpenNapServer s)
135     {
136 		synchronized (this) {
137 			servers--;
138 		}
139 		updateStatus();
140     }
141 
142     public void removePropertyChangeListener(PropertyChangeListener l) 
143     {
144         pcs.removePropertyChangeListener(l);
145     }
146 
147     public void setConnected(OpenNapServer s, boolean isConnected)
148     {
149 		synchronized (this) {
150 			connected += (isConnected) ? 1 : -1;
151 		}
152 		updateStatus();
153     }
154 
155     public void setConnecting(OpenNapServer s, boolean isConnecting)
156     {
157 		synchronized (this) {
158 			connecting += (isConnecting) ? 1 : -1;
159 		}
160 		synchronized (OpenNapNetwork.class) {
161 			totalConnecting += (isConnecting) ? 1 : -1;
162 		}
163 
164 		if (!isBusy()) {
165 			OpenNapPlugin.getServerManager().getConnector().wakeup();
166 		}
167 
168 		updateStatus();
169     }
170 
171     public void setStats(OpenNapServer s, int userCount, int fileCount, 
172 						 int fileSize)
173     {
174 		if (userCount > this.userCount) {
175 			this.userCount = userCount;
176 		}
177 		if (fileCount > this.fileCount) {
178 			this.fileCount = fileCount;
179 		}
180 		if (fileSize > this.fileSize) {
181 			this.fileSize = fileSize;
182 		}
183 		pcs.firePropertyChange("stats", null, null);
184     }
185 
186     public void setStatus(String status)
187     {
188 		String oldValue = getStatus();
189 		this.status = status;
190 		pcs.firePropertyChange("status", oldValue, status);
191     }
192 
193     public void updateStatus()
194     {
195 		setStatus((connected > 0) 
196 				  ? XNap.tr("connected ({0})", new Integer(connected))
197 				  : (connecting > 0) 
198 				  ? XNap.tr("connecting ({0})", new Integer(connecting)) + "..."
199 				  : "");
200     }
201 
202     public String toString()
203     {
204 		return getName() + " (" + getServers() + ")";
205     }
206 
207 }