package com.jscape.util.net.connection;

/**
 * @author Alex Usun
 */
public interface AsyncMessageConnection<M>
        extends MessageConnection<M> {

    void start(Listener<M> listener)
            throws ConnectionException;

    interface Listener<M> {

        void onMessage(AsyncMessageConnection<M> connection, M message);

        void onEof(AsyncMessageConnection<M> connection);

        void onError(AsyncMessageConnection<M> connection, Throwable error);

        void onClosed(AsyncMessageConnection<M> connection);

        void onIdle(AsyncMessageConnection<M> connection);
    }

    class NullListener<M>
            implements AsyncMessageConnection.Listener<M> {

        public void onMessage(AsyncMessageConnection<M> connection, M message) {
        }

        @Override
        public void onEof(AsyncMessageConnection<M> connection) {
        }

        public void onError(AsyncMessageConnection<M> connection, Throwable error) {
        }

        public void onClosed(AsyncMessageConnection<M> connection) {
        }

        public void onIdle(AsyncMessageConnection<M> connection) {
        }
    }

}