View Javadoc

1   /********************************************************************************
2    * InternetCafe is a software solution that helps the management of Cybercafes 
3    * according with the ITALIAN DECREE LAW ON ANTI-TERROR MEASURES, 27 JULY 2005.
4    * Copyright (C) 2006  Guido Angelo Ingenito
5   
6    * This program is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU General Public License
8    * as published by the Free Software Foundation; either version 2
9    * of the License, or (at your option) any later version.
10  
11   * This program is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.
15   * 
16   * You should have received a copy of the GNU General Public License
17   * along with this program; if not, write to the Free Software
18   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19   *******************************************************************************/
20  package base.network;
21  
22  import java.util.Hashtable;
23  import java.util.Iterator;
24  import java.util.Vector;
25  
26  public class Network {
27  
28  	private String name = "Network's Name";
29  
30  	private Hashtable<String, Workstation> client = new Hashtable<String, Workstation>();
31  
32  	private Hashtable<String, Workstation> server = new Hashtable<String, Workstation>();
33  
34  	public Network() {
35  
36  	}
37  
38  	public Network(String name, Workstation[] server, Workstation[] client) {
39  		this.name = name;
40  		this.addServerSet(server);
41  		this.addClientSet(client);
42  	}
43  
44  	public Workstation[] getAllWorkstation() {
45  		Vector<Workstation> result = new Vector<Workstation>();
46  		result.addAll(client.values());
47  		result.addAll(server.values());
48  		return result.toArray(new Workstation[0]);
49  	}
50  
51  	/***
52  	 * @return Returns the server.
53  	 */
54  	public Workstation[] getServer() {
55  		return server.values().toArray(new Workstation[0]);
56  	}
57  
58  	/***
59  	 * @return Returns the client.
60  	 */
61  	public Workstation[] getClient() {
62  		return client.values().toArray(new Workstation[0]);
63  	}
64  
65  	/***
66  	 * This method simply adds a client to the network.
67  	 * 
68  	 * @param client
69  	 *            The client to add to the network.
70  	 */
71  	protected void addClient(Workstation client) {
72  		this.client.put(NetworkManager.workstationKey(client), client);
73  	}
74  
75  	/***
76  	 * This method simply adds a server to the network.
77  	 * 
78  	 * @param server
79  	 *            The server to add to the network.
80  	 */
81  	protected void addServer(Workstation server) {
82  		this.server.put(NetworkManager.workstationKey(server), server);
83  	}
84  
85  	/***
86  	 * This method simply adds a workstation's client set to the network.
87  	 * 
88  	 * @param client
89  	 *            A set of client workstations to add to the network.
90  	 */
91  	protected void addClientSet(Workstation[] client) {
92  		for (int i = 0; i < client.length; i++)
93  			this.addClient(client[i]);
94  	}
95  
96  	/***
97  	 * This method simply adds a workstation's server set to the network.
98  	 * 
99  	 * @param server
100 	 *            A set of server workstations to add to the network.
101 	 */
102 	protected void addServerSet(Workstation[] server) {
103 		for (int i = 0; i < server.length; i++)
104 			this.addServer(server[i]);
105 	}
106 
107 	/***
108 	 * @return Returns the name.
109 	 */
110 	public String getName() {
111 		return name;
112 	}
113 
114 	/*
115 	 * (non-Javadoc)
116 	 * 
117 	 * @see java.lang.Object#toString()
118 	 */
119 	@Override
120 	public String toString() {
121 		StringBuffer sb = new StringBuffer();
122 		sb.append("NETWORK");
123 		sb.append("\n");
124 		sb.append("name: " + name);
125 		sb.append("\n");
126 		sb.append("clients: ");
127 		sb.append("\n");
128 		Iterator<Workstation> iterator = server.values().iterator();
129 		while (iterator.hasNext()) {
130 			sb.append(iterator.next());
131 			sb.append("\n");
132 		}
133 		sb.append("clients: ");
134 		sb.append("\n");
135 		iterator = client.values().iterator();
136 		while (iterator.hasNext()) {
137 			sb.append(iterator.next());
138 			sb.append("\n");
139 		}
140 
141 		return sb.toString();
142 	}
143 
144 	/***
145 	 * This method deletes a client workstation.
146 	 * 
147 	 * @param workstationKey
148 	 *            The client workstation to delete in form of key string.
149 	 */
150 	protected void deleteClient(String workstationKey) {
151 		this.client.remove(workstationKey);
152 	}
153 
154 	/***
155 	 * This method deletes a server workstation.
156 	 * 
157 	 * @param workstationKey
158 	 *            The server workstation to delete in form of key string.
159 	 */
160 	protected void deleteServer(String workstationKey) {
161 		this.server.remove(workstationKey);
162 	}
163 
164 	/***
165 	 * This method retrieves a client workstation specifying his id.
166 	 * 
167 	 * @param workstationId
168 	 *            An id associated to the client to retrieve.
169 	 * @return A client workstation with id equals to workstationId, null if the
170 	 *         specified workstationId is not a valid client workstation's id.
171 	 */
172 	public Workstation getClientById(int workstationId) {
173 		Workstation[] workstation = getClient();
174 		for (int i = 0; i < workstation.length; i++)
175 			if (workstation[i].getId() == workstationId)
176 				return workstation[i];
177 		return null;
178 	}
179 
180 	/***
181 	 * This method retrieves a server workstation specifying his id.
182 	 * 
183 	 * @param workstationId
184 	 *            An id associated to the server to retrieve.
185 	 * @return A server workstation with id equals to workstationId, null if the
186 	 *         specified workstationId is not a valid server workstation's id.
187 	 */
188 	public Workstation getServerById(int workstationId) {
189 		Workstation[] workstation = getServer();
190 		for (int i = 0; i < workstation.length; i++)
191 			if (workstation[i].getId() == workstationId)
192 				return workstation[i];
193 		return null;
194 	}
195 }