package com.haxademic.core.net;

import java.net.InetSocketAddress;
import java.net.UnknownHostException;
import java.util.Collection;

import org.java_websocket.WebSocket;
import org.java_websocket.handshake.ClientHandshake;
import org.java_websocket.server.WebSocketServer;

import com.haxademic.core.app.P;

public class SocketServerHandler extends WebSocketServer {
	
	public SocketServerHandler( int port ) throws UnknownHostException {
		super( new InetSocketAddress( port ) );
	}
	
	public SocketServerHandler( InetSocketAddress address ) {
		super( address );
	}
	
	protected String connAddress(WebSocket conn) {
		return conn.getRemoteSocketAddress().getHostName();
	}
	
	@Override
	public void onOpen( WebSocket conn, ClientHandshake handshake ) {
		// handshake.getResourceDescriptor()
		if(SocketServer.FORWARDS_ALL_MESSAGES) this.sendToAll( "{\"message\":\"new connection: " + connAddress(conn) + " has entered the room.\"}" );
		if(SocketServer.DEBUG == true) P.println( connAddress(conn) + " entered the room!" );
	}
	
	@Override
	public void onClose( WebSocket conn, int code, String reason, boolean remote ) {
		if(SocketServer.FORWARDS_ALL_MESSAGES) this.sendToAll( "{\"message\":\"" + connAddress(conn) + " has left the room.\"}" );
		if(SocketServer.DEBUG == true) P.println( connAddress(conn) + " has left the room!" );
	}
	
	@Override
	public void onMessage( WebSocket conn, String message ) {
		if(SocketServer.FORWARDS_ALL_MESSAGES) this.sendToAll( message );
		receiveMessage( message );
		if(SocketServer.DEBUG == true) P.println( connAddress(conn) + ": " + message );
	}
	
	@Override
	public void onError( WebSocket conn, Exception ex ) {
		ex.printStackTrace();
		if( conn != null ) {
			// some errors like port binding failed may not be assignable to a specific websocket
		}
	}
	
	protected void receiveMessage(String message) {
		// OVERRIDE THIS
	}
	
	/**
	 * Sends <var>text</var> to all currently connected WebSocket clients.
	 * 
	 * @param text
	 *            The String to send across the network.
	 * @throws InterruptedException
	 *             When socket related I/O errors occur.
	 */
	public void sendToAll( String text ) {
		Collection<WebSocket> con = connections();
		synchronized ( con ) {
			for( WebSocket c : con ) {
				c.send( text );
			}
		}
	}

	@Override
	public void onStart() {
		// TODO Auto-generated method stub
		
	}
}

@@ -614,16 +614,134 @@
 conn) %7B%0A
+%09%09if(conn != null && conn.getRemoteSocketAddress() != null && conn.getRemoteSocketAddress().getHostName() != null) %7B%0A%09
 %09%09return
@@ -786,16 +786,66 @@
 Name();%0A
+%09%09%7D else %7B%0A%09%09%09return %22Error: no connAddress%22;%0A%09%09%7D%0A
 %09%7D%0A%09%0A%09@O
@@ -958,16 +958,38 @@
 iptor()%0A
+%09%09if(conn != null) %7B%0A%09
 %09%09if(Soc
@@ -1118,32 +1118,33 @@
  room.%5C%22%7D%22 );%0A%09%09
+%09
 if(SocketServer.
@@ -1150,32 +1150,38 @@
 .DEBUG == true) 
+%7B%0A%09%09%09%09
 P.println( connA
@@ -1211,32 +1211,94 @@
 d the room!%22 );%0A
+%09%09%09%09P.println(%22Connections: %22+connections().size());%0A%09%09%09%7D%0A%09%09%7D%0A
 %09%7D%0A%09%0A%09@Override%0A
@@ -1375,24 +1375,46 @@
  remote ) %7B%0A
+%09%09if(conn != null) %7B%0A%09
 %09%09if(SocketS
@@ -1524,24 +1524,25 @@
 m.%5C%22%7D%22 );%0A%09%09
+%09
 if(SocketSer
@@ -1612,24 +1612,28 @@
 e room!%22 );%0A
+%09%09%7D%0A
 %09%7D%0A%09%0A%09@Overr
@@ -1691,24 +1691,46 @@
 message ) %7B%0A
+%09%09if(conn != null) %7B%0A%09
 %09%09if(SocketS
@@ -1787,16 +1787,17 @@
 ge );%0A%09%09
+%09
 receiveM
@@ -1815,16 +1815,17 @@
 sage );%0A
+%09
 %09%09if(Soc
@@ -1894,24 +1894,28 @@
  message );%0A
+%09%09%7D%0A
 %09%7D%0A%09%0A%09@Overr
@@ -2571,24 +2571,46 @@
 c : con ) %7B%0A
+%09%09%09%09if(c.isOpen()) %7B%0A%09
 %09%09%09%09c.send( 
@@ -2617,16 +2617,22 @@
 text );%0A
+%09%09%09%09%7D%0A
 %09%09%09%7D%0A%09%09%7D

Update socker server handler