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.net.InetAddress;
23  import java.net.UnknownHostException;
24  
25  import org.w3c.dom.Document;
26  import org.w3c.dom.Element;
27  import org.w3c.dom.Node;
28  
29  import base.ICXmlTags;
30  import base.IXMLSaveable;
31  
32  public class Workstation implements IXMLSaveable {
33  
34  	private final int id;
35  
36  	private String name = "";
37  
38  	private String location = "";
39  
40  	private String description = "";
41  
42  	private String type = WorkstationType.WORKSTATION_TYPE[0];
43  
44  	private InetAddress address;
45  
46  	private int port = -1;
47  
48  	private boolean inUse = false;
49  
50  	// private final String role;
51  
52  	protected Workstation(final int id) {
53  		this.id = id;
54  		// this.role = role;
55  		try {
56  			this.address = InetAddress.getLocalHost();
57  			this.port = 9000;
58  		} catch (UnknownHostException e) {
59  			// TODO Auto-generated catch block
60  			e.printStackTrace();
61  		}
62  	}
63  
64  	protected Workstation(final int id, String name, String type,
65  			String description, InetAddress address, boolean inUse) {
66  		this.id = id;
67  		this.name = name;
68  		this.type = type;
69  		this.description = description;
70  		this.address = address;
71  		this.port = 9000;
72  		this.inUse = inUse;
73  	}
74  
75  	protected Workstation(final int id, String name, String type,
76  			String description, InetAddress address, int port, boolean inUse) {
77  		this.id = id;
78  		this.name = name;
79  		this.type = type;
80  		this.description = description;
81  		this.address = address;
82  		this.port = port;
83  		this.inUse = inUse;
84  	}
85  
86  	/***
87  	 * @return Returns the address.
88  	 */
89  	public InetAddress getAddress() {
90  		return address;
91  	}
92  
93  	/***
94  	 * @param address
95  	 *            The address to set.
96  	 */
97  	public void setAddress(InetAddress address) {
98  		this.address = address;
99  	}
100 
101 	/***
102 	 * @return Returns the description.
103 	 */
104 	public String getDescription() {
105 		return description;
106 	}
107 
108 	/***
109 	 * @param description
110 	 *            The description to set.
111 	 */
112 	public void setDescription(String description) {
113 		this.description = description;
114 	}
115 
116 	/***
117 	 * @return Returns the inUse.
118 	 */
119 	public boolean isInUse() {
120 		return inUse;
121 	}
122 
123 	/***
124 	 * @param inUse
125 	 *            The inUse to set.
126 	 */
127 	public void setInUse(boolean inUse) {
128 		this.inUse = inUse;
129 	}
130 
131 	/***
132 	 * @return Returns the name.
133 	 */
134 	public String getName() {
135 		return name;
136 	}
137 
138 	/***
139 	 * @param name
140 	 *            The name to set.
141 	 */
142 	public void setName(String name) {
143 		this.name = name;
144 	}
145 
146 	/***
147 	 * @return Returns the type.
148 	 */
149 	public String getType() {
150 		return type;
151 	}
152 
153 	/***
154 	 * @param type
155 	 *            The type to set.
156 	 */
157 	public void setType(String type) {
158 		this.type = type;
159 	}
160 
161 	/*
162 	 * (non-Javadoc)
163 	 * 
164 	 * @see java.lang.Object#toString()
165 	 */
166 	@Override
167 	public String toString() {
168 		StringBuffer sb = new StringBuffer();
169 		sb.append("WORKSTATION");
170 		sb.append("\n");
171 		sb.append("id: " + id);
172 		sb.append("\n");
173 		sb.append("name: " + name);
174 		sb.append("\n");
175 		sb.append("description: " + description);
176 		sb.append("\n");
177 		sb.append("type: " + type);
178 		sb.append("\n");
179 		sb.append("address: " + address);
180 		sb.append("\n");
181 		sb.append("port: " + port);
182 		sb.append("\n");
183 		sb.append("in use: " + inUse);
184 		return sb.toString();
185 	}
186 
187 	/***
188 	 * @return Returns the id.
189 	 */
190 	public int getId() {
191 		return id;
192 	}
193 
194 	/***
195 	 * @return Returns true if this workstation is a server, false otherwise.
196 	 */
197 	public boolean isServer() {
198 		return this.id == 0; // Dummy...we can also access the
199 		// InternetCafeManager
200 	}
201 
202 	/***
203 	 * @return Returns the port.
204 	 */
205 	public int getPort() {
206 		return port;
207 	}
208 
209 	/***
210 	 * @param port
211 	 *            The port to set.
212 	 */
213 	public void setPort(int port) {
214 		this.port = port;
215 	}
216 
217 	/***
218 	 * @return Returns the location.
219 	 */
220 	public String getLocation() {
221 		return location;
222 	}
223 
224 	/***
225 	 * @param location
226 	 *            The location to set.
227 	 */
228 	public void setLocation(String location) {
229 		this.location = location;
230 	}
231 
232 	/*
233 	 * (non-Javadoc)
234 	 * 
235 	 * @see test.base.IXMLSaveable#toXml(org.w3c.dom.Document)
236 	 */
237 	public Node toXml(Document document) {
238 		Element workstationElement = document
239 				.createElement(ICXmlTags.IC_WORKSTATION_TAG);
240 		workstationElement.setAttribute(ICXmlTags.IC_WORKSTATION_ID_ATTRIBUTE,
241 				"" + this.id);
242 		workstationElement.setAttribute(
243 				ICXmlTags.IC_WORKSTATION_IN_USE_ATTRIBUTE, "" + this.inUse);
244 
245 		Element nameElement = document
246 				.createElement(ICXmlTags.IC_WORKSTATION_NAME_TAG);
247 		nameElement.setAttribute(ICXmlTags.IC_VALUE_ATTRIBUTE, this.name);
248 		workstationElement.appendChild(nameElement);
249 
250 		Element typeElement = document
251 				.createElement(ICXmlTags.IC_WORKSTATION_TYPE_TAG);
252 		typeElement.setAttribute(ICXmlTags.IC_VALUE_ATTRIBUTE, this.type);
253 		workstationElement.appendChild(typeElement);
254 		// FIXME
255 		workstationElement
256 				.appendChild(document
257 						.createComment("We must find a way to retrieve the machine's MAC address..."));
258 		Element addressElement = document
259 				.createElement(ICXmlTags.IC_WORKSTATION_ADDRESS_TAG);
260 		addressElement.setAttribute(ICXmlTags.IC_WORKSTATION_ADDRESS_ATTRIBUTE,
261 				this.address.getHostAddress());
262 		addressElement.setAttribute(ICXmlTags.IC_WORKSTATION_PORT_ATTRIBUTE, ""
263 				+ this.port);
264 		workstationElement.appendChild(addressElement);
265 
266 		Element locationElement = document
267 				.createElement(ICXmlTags.IC_WORKSTATION_LOCATION_TAG);
268 		locationElement.setAttribute(ICXmlTags.IC_VALUE_ATTRIBUTE,
269 				this.location);
270 		workstationElement.appendChild(locationElement);
271 
272 		Element descriptionElement = document
273 				.createElement(ICXmlTags.IC_WORKSTATION_DESCRIPTION_TAG);
274 		descriptionElement.setAttribute(ICXmlTags.IC_VALUE_ATTRIBUTE,
275 				this.description);
276 		workstationElement.appendChild(descriptionElement);
277 
278 		return workstationElement;
279 	}
280 
281 }