package client;

import util.Log;
import org.webbitserver.WebSocketConnection;
import protobuf.ProtobufMessageHandler;
import protobuf.RblProto.*;

/**
 * Created by Peter Mösenthin.
 *
 * This is the representaion of a RaspberryHomeClient using a Websocket
 * connection.
 */
public class RBLWebSocketClient extends RaspberryLifeClient {

    public static final String DEBUG_TAG = RBLWebSocketClient.class.getSimpleName();

    private WebSocketConnection connection;
    private final ProtobufMessageHandler messageHandler =
            new ProtobufMessageHandler(this);

    public RBLWebSocketClient(WebSocketConnection connection){
        this.connection = connection;
    }


    //==========================================================================
    // Connection state handling
    //==========================================================================
    @Override
    protected void onConnectionClosed() {
        //TODO implement
    }

    @Override
    protected void onConnectionDenied(String reason) {
        //TODO implement
    }

    @Override
    protected void onConnectionAccepted() {
        //TODO implement
    }

    //==========================================================================
    // Message handling
    //==========================================================================

    @Override
    public void sendMessage(RBLMessage message) {
        if(message != null) {
            connection.send(message.toByteArray());
        }else {
            Log.add(DEBUG_TAG, "Message was empty. ID=" + getId());
        }
    }

    //@Override
    public void readMessage(RBLMessage message){
            messageHandler.handleMessage(message);
    }

    //==========================================================================
    // Getter & Setter
    //==========================================================================


    public WebSocketConnection getWebSocketConnection(){
        return connection;
    }


}

@@ -10,16 +10,66 @@
 lient;%0A%0A
+import data.Config;%0Aimport protobuf.ProtoFactory;%0A
 import u
@@ -1091,114 +1091,767 @@
     
-//TODO implement%0A    %7D%0A%0A    @Override%0A    protected void onConnectionAccepted() %7B%0A        //TODO implement
+RBLMessage m =%0A                ProtoFactory.buildAuthMessage(Config.getConf().getString(%22server.id%22),%0A                        RBLMessage.MessageFlag.RESPONSE,%0A                        ProtoFactory.buildPlainText(%22Client denied. REASON=%22 + reason)%0A                );%0A        sendMessage(m);%0A    %7D%0A%0A    @Override%0A    protected void onConnectionAccepted() %7B%0A        RBLMessage m =%0A                ProtoFactory.buildAuthMessage(Config.getConf().getString(%22server.id%22),%0A                        RBLMessage.MessageFlag.RESPONSE,%0A                        ProtoFactory.buildPlainText(%22Accepted client with id: %22%0A                                + getId()%0A                                + %22 TIME= %22 + System.currentTimeMillis())%0A                );%0A        sendMessage(m);
 %0A   

add auth messages to web client