package org.aktin.broker.client;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import org.eclipse.jetty.websocket.api.WebSocketListener;

public class ClientWebsocket implements WebSocketListener {
	public CountDownLatch expectedMessages;
	public List<String> messages;
	
	public ClientWebsocket() {
		messages = new ArrayList<>();
	}

	public void prepareForMessages(int count){
		expectedMessages = new CountDownLatch(count);
	}
	public void waitForMessages(long delay, TimeUnit unit) throws InterruptedException{
		expectedMessages.await(delay, unit);
	}
	@Override
	public void onWebSocketBinary(byte[] payload, int offset, int len) {
		System.out.println("Client websocket closed");
	}
	@Override
	public void onWebSocketClose(int statusCode, String reason) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void onWebSocketConnect(org.eclipse.jetty.websocket.api.Session session) {
		System.out.println("Client websocket open");
		try {
			session.getRemote().sendString("Hallo vom Client");
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
		
	}
	@Override
	public void onWebSocketError(Throwable cause) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void onWebSocketText(String message) {
		System.out.println("Client received message: "+message);
		messages.add(message);
		if( expectedMessages != null ){
			expectedMessages.countDown();
		}
	}
}

@@ -753,60 +753,11 @@
 ) %7B%0A
-%09%09System.out.println(%22Client websocket closed%22);%0A
 %09%7D%0A
+
 %09@Ov
@@ -967,55 +967,8 @@
 ) %7B%0A
-%09%09System.out.println(%22Client websocket open%22);%0A
 %09%09tr
@@ -1092,19 +1092,16 @@
 e);%0A%09%09%7D%0A
-%09%09%0A
 %09%7D%0A%09@Ove
@@ -1201,32 +1201,32 @@
 %09%0A%09%7D%0A%09@Override%0A
+
 %09public void onW
@@ -1260,67 +1260,8 @@
 ) %7B%0A
-%09%09System.out.println(%22Client received message: %22+message);%0A
 %09%09me

remove print statements from test websocket