/**
 * 
 */
package fr.utbm.to52.smarthome.controller;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.LinkedList;
import java.util.List;

/**
 * @author alexandre
 *
 */
public class SocketInput implements Runnable{

	private ServerSocket server;

	private List<Socket> listSocket;

	private Event ringEvent;

	private boolean running;

	public SocketInput(int port) throws IOException{
		this.setListSocket(new LinkedList<Socket>());
		this.server = new ServerSocket(port);
	}

	public void setRingEventController(Event r){
		this.ringEvent = r;
	}

	@Override
	public void run() {
		this.running =true;
		while(this.running){
			try {
				this.getListSocket().add(this.server.accept());
			} catch (IOException e) {
				e.printStackTrace();
			}

			synchronized (this) {
				Runnable r = new Runnable() {

					private Socket s;

					@Override
					public void run() {
						this.s = SocketInput.this.getListSocket().get(SocketInput.this.getListSocket().size() - 1);
						BufferedReader reader;
						try {
							reader = new BufferedReader(new InputStreamReader(this.s.getInputStream()));
							String command = reader.readLine();
							if(command.equals("Ring")){
								if(SocketInput.this.ringEvent != null)
									SocketInput.this.ringEvent.inform(null);
							}else if(command.equals("Quit")){
								SocketInput.this.running = false;
							}else{
								System.out.println("unhandled event : " + command);
							}

							this.s.close();
						} catch (IOException e) {
							e.printStackTrace();
						}

					}
				};

				Thread t = new Thread(r);
				t.start();

			}
		}
	}

	public List<Socket> getListSocket() {
		return this.listSocket;
	}

	public void setListSocket(List<Socket> listSocket) {
		this.listSocket = listSocket;
	}

}

@@ -594,80 +594,8 @@
 %09%7D%0A%0A
-%09public void setRingEventController(Event r)%7B%0A%09%09this.ringEvent = r;%0A%09%7D%0A%0A
 %09@Ov
@@ -633,21 +633,24 @@
 his.
-r
+setR
 unning
- =
+(
 true
+)
 ;%0A%09%09
@@ -660,23 +660,28 @@
 le(this.
-r
+getR
 unning
+()
 )%7B%0A%09%09%09tr
@@ -1249,25 +1249,28 @@
 ut.this.
-r
+getR
 ingEvent
  != null
@@ -1261,16 +1261,18 @@
 ingEvent
+()
  != null
@@ -1299,25 +1299,28 @@
 ut.this.
-r
+getR
 ingEvent
 .inform(
@@ -1311,16 +1311,18 @@
 ingEvent
+()
 .inform(
@@ -1398,23 +1398,25 @@
 his.
-r
+setR
 unning
- = 
+(
 false
+)
 ;%0A%09%09
@@ -1516,24 +1516,80 @@
 .s.close();%0A
+%09%09%09%09%09%09%09SocketInput.this.getListSocket().remove(this.s);%0A
 %09%09%09%09%09%09%7D catc
@@ -1717,16 +1717,271 @@
 %7D%0A%09%09%7D%0A%09%7D
+%0A%09%0A%09public void setRingEventController(Event r)%7B%0A%09%09this.ringEvent = r;%0A%09%7D%0A%09%0A%09protected void setRunning(boolean b) %7B%0A%09%09this.running = b;%0A%09%7D%0A%0A%09public Event getRingEvent()%7B%0A%09%09return this.ringEvent;%0A%09%7D%0A%09%0A%09public boolean getRunning()%7B%0A%09%09return this.running;%0A%09%7D
 %0A%0A%09publi

Clean some warnings