package net.dlogic.kryonet.client;

import java.io.IOException;

import net.dlogic.kryonet.common.entity.Room;
import net.dlogic.kryonet.common.entity.User;
import net.dlogic.kryonet.common.request.GetRoomsRequest;
import net.dlogic.kryonet.common.request.JoinRoomRequest;
import net.dlogic.kryonet.common.request.LeaveRoomRequest;
import net.dlogic.kryonet.common.request.LoginRequest;
import net.dlogic.kryonet.common.request.LogoutRequest;
import net.dlogic.kryonet.common.request.PrivateMessageRequest;
import net.dlogic.kryonet.common.request.PublicMessageRequest;
import net.dlogic.kryonet.common.utility.KryonetUtility;

import com.esotericsoftware.kryonet.Client;
import com.esotericsoftware.minlog.Log;

public class KryonetClient {
	public Client endpoint;
	public final KryonetClientListener listener = new KryonetClientListener();
	public KryonetClient(int writeBufferSize, int objectBufferSize) {
		Log.info("KryonetClient(" + writeBufferSize + ", " + objectBufferSize + ")");
		endpoint = new Client(writeBufferSize, objectBufferSize);
		KryonetUtility.registerClasses(endpoint);
		endpoint.addListener(listener);
	}
	public void start(final int timeout, String host, int tcpPort, int udpPort) throws IOException, KryonetClientException {
		Log.info("KryonetClient.start(" + timeout + ", " + host + ", " + tcpPort + ", " + udpPort + ")");
		if (!endpoint.isConnected()) {
			endpoint.start();
			endpoint.connect(timeout, host, tcpPort, udpPort);
			new Thread(new Runnable() {
				public void run() {
					try {
						endpoint.update(timeout);
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}).start();
		} else {
			throw new KryonetClientException("client already started");
		}
	}
	public void sendGetRoomsRequest(String search) {
		GetRoomsRequest request = new GetRoomsRequest();
		request.search = search;
		endpoint.sendTCP(request);
	}
	public void sendJoinRoomRequest(Room roomToJoin, String password) {
		JoinRoomRequest request = new JoinRoomRequest();
		request.targetRoomId = roomToJoin.id;
		request.password = password;
		endpoint.sendTCP(request);
	}
	public void sendLeaveRoomRequest(Room roomToLeave) {
		LeaveRoomRequest request = new LeaveRoomRequest();
		request.targetRoomId = roomToLeave.id;
		endpoint.sendTCP(request);
	}
	public void sendLoginRequest(String username, String password) {
		Log.info("KryonetClient.sendLoginRequest(" + username + ", " + password + ")");
		LoginRequest request = new LoginRequest();
		request.username = username;
		request.password = password;
		endpoint.sendTCP(request);
	}
	public void sendLogoutRequest() {
		LogoutRequest request = new LogoutRequest();
		endpoint.sendTCP(request);
	}
	public void sendPrivateMessageRequest(User targetUser, String message) {
		PrivateMessageRequest request = new PrivateMessageRequest();
		request.targetUserId = targetUser.id;
		request.message = message;
		endpoint.sendTCP(request);
	}
	public void sendPublicMessageRequest(Room targetRoom, String message) {
		PublicMessageRequest request = new PublicMessageRequest();
		request.targetRoomId = targetRoom.id;
		request.message = message;
		endpoint.sendTCP(request);
	}
}

@@ -1382,24 +1382,35 @@
 )) %7B%0A%09%09%09
+new Thread(
 endpoint
 .start()
@@ -1401,16 +1401,17 @@
 endpoint
+)
 .start()
@@ -1470,192 +1470,8 @@
 t);%0A
-%09%09%09new Thread(new Runnable() %7B%0A%09%09%09%09public void run() %7B%0A%09%09%09%09%09try %7B%0A%09%09%09%09%09%09endpoint.update(timeout);%0A%09%09%09%09%09%7D catch (IOException e) %7B%0A%09%09%09%09%09%09e.printStackTrace();%0A%09%09%09%09%09%7D%0A%09%09%09%09%7D%0A%09%09%09%7D).start();%0A
 %09%09%7D 

refactor replaced the thread that is continually updating client.update by starting a new thread on the client (endpoint) itself