View Javadoc

1   package org.xvsm.remote.tcp;
2   
3   import java.io.IOException;
4   import java.io.OutputStreamWriter;
5   import java.net.ServerSocket;
6   import java.net.Socket;
7   import java.net.SocketException;
8   import java.net.URI;
9   import java.util.ArrayList;
10  import java.util.List;
11  import java.util.concurrent.ExecutorService;
12  import java.util.concurrent.Executors;
13  
14  import org.xvsm.core.AtomicEntry;
15  import org.xvsm.core.ContainerRef;
16  import org.xvsm.core.Entry;
17  import org.xvsm.core.Tuple;
18  import org.xvsm.interfaces.ICapi;
19  import org.xvsm.internal.ContainerManager;
20  import org.xvsm.internal.EventProcessingPool;
21  import org.xvsm.internal.exceptions.FatalException;
22  import org.xvsm.internal.tasks.OperationTask;
23  import org.xvsm.internal.tasks.OperationTaskType;
24  import org.xvsm.internal.tasks.Task;
25  import org.xvsm.remote.interfaces.IMarshaller;
26  import org.xvsm.remote.interfaces.ITransportListener;
27  import org.xvsm.remote.interfaces.ITransportSender;
28  import org.xvsm.selectors.FifoSelector;
29  import org.xvsm.selectors.Selector;
30  
31  /***
32   * 
33   * @author Christian Schreiber, Michael Proestler
34   * 
35   */
36  public class WebAccess implements ITransportListener<byte[]>,
37  		IMarshaller<byte[]>, ITransportSender<byte[]> {
38  
39  	// /***
40  	// * The Logger.
41  	// */
42  	// private static Logger logger = Logger.getLogger(WebAccess.class);
43  
44  	/***
45  	 * The maximal amount of Threads in the TcpListenerPool.
46  	 */
47  	private static final int MAX_THREADS = 5; // TODO: Get MAX_THREADS from
48  
49  	// Configuration.
50  
51  	/***
52  	 * The ServerSocket of the TcpTransportListener.
53  	 */
54  	private ServerSocket ssocket;
55  
56  	/***
57  	 * Indicates if the TcpTransportListener is running or not.
58  	 */
59  	private boolean running = false;
60  
61  	/***
62  	 * The URI where the listener should listen.
63  	 */
64  	private URI inboundURI;
65  
66  	/***
67  	 * The ExecutorService to execute the TcpListenerThreads.
68  	 */
69  	private ExecutorService service = Executors.newFixedThreadPool(MAX_THREADS);
70  
71  	/***
72  	 * Default Constructor.
73  	 */
74  	public WebAccess() {
75  
76  	}
77  
78  	/***
79  	 * {@inheritDoc}
80  	 */
81  	// @Override
82  	public void setURI(URI nuri) {
83  		this.inboundURI = nuri;
84  	}
85  
86  	/***
87  	 * {@inheritDoc}
88  	 */
89  	// @Override
90  	public void run() {
91  		Thread.currentThread().setName(this.getClass().getName());
92  		this.running = true;
93  		try {
94  			ssocket = new ServerSocket(inboundURI.getPort());
95  			while (this.running) {
96  				this.service.execute(new HttpListenerThread(ssocket.accept()));
97  			}
98  
99  		} catch (SocketException e) {
100 			// Is thrown when the socket gets closed -> ignore
101 
102 		} catch (IOException e) {
103 			// TODO Auto-generated catch block
104 
105 		}
106 
107 	}
108 
109 	/***
110 	 * {@inheritDoc}
111 	 */
112 	// @Override
113 	public void stop() {
114 		try {
115 			this.running = false;
116 			this.ssocket.close();
117 
118 		} catch (IOException e) {
119 			throw new FatalException(e);
120 		}
121 
122 	}
123 
124 	/***
125 	 * {@inheritDoc}
126 	 */
127 	// @Override
128 	public void setMarshaller(IMarshaller<byte[]> m) {
129 
130 	}
131 
132 	/***
133 	 * 
134 	 * @author Christian Schreiber, Michael Proestler
135 	 * 
136 	 */
137 	class HttpListenerThread implements Runnable {
138 
139 		/***
140 		 * The Socket over which the communication takes place.
141 		 */
142 		private Socket socket;
143 
144 		/***
145 		 * Default Constructor.
146 		 * 
147 		 * @param socket
148 		 *            The socket over which the communication takes place.
149 		 */
150 		public HttpListenerThread(Socket socket) {
151 			this.socket = socket;
152 		}
153 
154 		/***
155 		 * {@inheritDoc}
156 		 */
157 		// @Override
158 		@SuppressWarnings("unchecked")
159 		public void run() {
160 			Thread.currentThread().setName(this.getClass().getName());
161 			// TODO: implement functionality
162 			try {
163 
164 				List<ContainerRef> crefs = ContainerManager.getInstance()
165 						.getAllContainer(null);
166 				OutputStreamWriter writer = new OutputStreamWriter(socket
167 						.getOutputStream());
168 				writer.write("<h1>Welcome MozartSpaces</h1>");
169 				writer
170 						.write("<h4><br>Status: <font color=\"green\">Core running</font></h4>");
171 				writer.write("<h4>Following container exist:</h4>");
172 				for (ContainerRef cref : crefs) {
173 					writer.write(cref.getId() + "<br>");
174 				}
175 
176 				ContainerRef recent = ContainerManager.getInstance()
177 						.getContainer(null, "Logger");
178 
179 				if (recent != null) {
180 					writer.write("<h4>Recent Actions:</h4>");
181 					List<Selector> selectors = new ArrayList<Selector>();
182 					selectors.add(new FifoSelector(Selector.CNT_ALL));
183 					OperationTask task = new OperationTask(
184 							OperationTaskType.READ, null, recent, null,
185 							selectors, ICapi.INFINITE_TIMEOUT);
186 					EventProcessingPool.getInstance().execute(task);
187 					List<Entry> entries = (List<Entry>) task.readResult();
188 					for (Entry e : entries) {
189 						try {
190 							String s = new String();
191 							Tuple t = (Tuple) e;
192 							s += "Operation: "
193 									+ ((AtomicEntry) t.getEntryAt(0))
194 											.getValue() + " ";
195 							s += "Type: "
196 									+ ((AtomicEntry) t.getEntryAt(1))
197 											.getValueClass().getSimpleName()
198 									+ " ";
199 							s += "Value: "
200 									+ ((AtomicEntry) t.getEntryAt(1))
201 											.getValue();
202 
203 							writer.write(s + "<br>");
204 						} catch (ClassCastException e1) {
205 							// ignore
206 						}
207 					}
208 
209 				}
210 				writer.flush();
211 				socket.close();
212 
213 			} catch (Exception e) {
214 				// TODO Auto-generated catch block
215 
216 			}
217 		}
218 	}
219 
220 	/***
221 	 * {@inheritDoc}
222 	 */
223 	// @Override
224 	public void send(URI uri, Task t) {
225 		// TODO Auto-generated method stub
226 
227 	}
228 
229 	/***
230 	 * {@inheritDoc}
231 	 */
232 	// @Override
233 	public Task unmarshall(byte[] x) {
234 		// TODO Auto-generated method stub
235 		return null;
236 	}
237 
238 	/***
239 	 * {@inheritDoc}
240 	 */
241 	// @Override
242 	public byte[] marshall(Task y) {
243 		// TODO Auto-generated method stub
244 		return null;
245 	}
246 
247 	// @Override
248 	public URI getUri() {
249 		// TODO Auto-generated method stub
250 		return null;
251 	}
252 
253 	/***
254 	 * {@inheritDoc}
255 	 */
256 	public void sendRequest(URI uri, Task t) {
257 		// TODO Auto-generated method stub
258 
259 	}
260 
261 	/***
262 	 * {@inheritDoc}
263 	 */
264 	public void sendResponse(URI uri, Task t) {
265 		// TODO Auto-generated method stub
266 
267 	}
268 }