package com.github.noxan.aves.demo.chat;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.github.noxan.aves.auth.session.SessionManager;
import com.github.noxan.aves.auth.storage.InMemoryUsernamePasswordStorage;
import com.github.noxan.aves.net.Connection;
import com.github.noxan.aves.server.ServerHandler;
import com.github.noxan.aves.server.SocketServer;

public class ChatServer implements ServerHandler {
    public static void main(String[] args) {
        ChatServer server = new ChatServer();
        try {
            server.start();
        } catch(IOException e) {
            e.printStackTrace();
        }
    }

    private Logger logger = Logger.getLogger(getClass().getName());

    private InMemoryUsernamePasswordStorage storage;
    private SessionManager sessionManager;

    private SocketServer server;

    public ChatServer() {
        storage = new InMemoryUsernamePasswordStorage();
        sessionManager = new SessionManager(storage);
        server = new SocketServer(this);
    }

    public void start() throws IOException {
        server.start();
    }

    @Override
    public void readData(Connection connection, Object data) {
        logger.log(Level.INFO, data.toString());
    }

    @Override
    public void clientConnect(Connection connection) {
        logger.log(Level.INFO, connection + " connected");
    }

    @Override
    public void clientDisconnect(Connection connection) {
        logger.log(Level.INFO, connection + " disconnected");
    }

    @Override
    public void clientLost(Connection connection) {
        logger.log(Level.INFO, connection + " lost");
    }
}

@@ -1263,16 +1263,348 @@
 ing());%0A
+        String%5B%5D parts = data.toString().split(%22;%22);%0A        try %7B%0A            switch(parts%5B0%5D) %7B%0A                default:%0A                    logger.log(Level.WARNING, %22unknown packet header: %22 + data.toString());%0A                    break;%0A            %7D%0A        %7D catch(IOException e) %7B%0A            e.printStackTrace();%0A        %7D%0A
     %7D%0A%0A 

split received message and log unknown messages