package websockets;

import java.lang.reflect.Method;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.ContextHandler;
import org.eclipse.jetty.websocket.server.WebSocketHandler;
import org.eclipse.jetty.websocket.servlet.WebSocketServletFactory;

import processing.core.PApplet;

/**
 *
 * @author Lasse Steenbock Vestergaard
 *
 */
public class WebsocketServer {
	private Method websocketServerEvent;
	private Method websocketServerEventBinary;
	private WebsocketServerController serverController;

	/**
	 *
	 * The websocket server object that is initiated directly in the Processing sketch
	 *
	 * @param parent Processing's PApplet object
	 * @param port The port number you want the websocket server to initiate its connection on
	 * @param uri The uri you want your server to respond to. Ex. /john (if the port is set to ex. 8025, then the full URI would be ws://localhost:8025/john).
	 */
	public WebsocketServer(PApplet parent, int port, String uri){
		this(parent, parent, port, uri);
	}

	/**
	 *
	 * @param parent Processing's PApplet object
	 * @param callbacks The object implementing .webSocketServerEvent()
	 * @param port The port number you want the websocket server to initiate its connection on
	 * @param uri The uri you want your server to respond to. Ex. /john (if the port is set to ex. 8025, then the full URI would be ws://localhost:8025/john).
	 */
	public WebsocketServer(PApplet parent, Object callbacks, int port,
						   String uri) {

		parent.registerMethod("dispose", this);

		try {
			websocketServerEvent = callbacks.getClass().getMethod(
					"webSocketServerEvent", String.class);
			websocketServerEventBinary = callbacks.getClass().getMethod(
					"webSocketServerEvent", byte[].class, int.class, int.class);
		} catch (Exception e) {
			// no such method, or an error.. which is fine, just ignore
		}

		Server server = new Server(port);
		serverController = new WebsocketServerController(callbacks,
				websocketServerEvent, websocketServerEventBinary);

		WebSocketHandler wsHandler = new WebSocketHandler() {

			@Override
			public void configure(WebSocketServletFactory factory){
				factory.setCreator(new WebsocketServerCreator(serverController));
			}
		};

		ContextHandler contextHandler = new ContextHandler(uri);
		contextHandler.setAllowNullPathInfo(true); // disable redirect from /ws to /ws/
		contextHandler.setHandler(wsHandler);

		server.setHandler(contextHandler);

		try {
			server.start();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 *
	 * This method is used for sending messages to all connected clients. This method will be updated with the possibility for sending messages to specific clients!
	 *
	 * @param message The message you want to send to all clients
	 */
	//TODO: Add possibility for sending back to specific client
	public void sendMessage(String message){
		serverController.writeAllMembers(message);

	}

	public void sendMessage(byte[] data){
		serverController.writeAllMembers(data);
	}

	public void dispose(){
		// Anything in here will be called automatically when
	    // the parent sketch shuts down. For instance, this might
	    // shut down a thread used by this library.
	}
}

@@ -352,16 +352,47 @@
 ergaard%0A
+ * @author Abe Pazos (changes)%0A
  *%0A */%0Ap
@@ -557,16 +557,59 @@
 oller;%0A%0A
+%09private static int MAX_MSG_SIZE = 65536;%0A%0A
 %09/**%0A%09 *
@@ -2227,16 +2227,140 @@
 ctory)%7B%0A
+%09%09%09%09factory.getPolicy().setMaxTextMessageSize(MAX_MSG_SIZE);%0A%09%09%09%09factory.getPolicy().setMaxBinaryMessageSize(MAX_MSG_SIZE);%0A
 %09%09%09%09fact
@@ -3214,24 +3214,199 @@
 (data);%0A%09%7D%0A%0A
+%09/**%0A%09 * Set the max message size in bytes, 64Kb by default%0A%09 * @param bytes%0A%09 */%0A%09public static void setMaxMessageSize(int bytes) %7B%0A%09%09MAX_MSG_SIZE = bytes;%0A%09%7D%0A%0A%09/**%0A%09 *%0A%09 */%0A
 %09public void

Fix #13, allow setting a maximum message size