Console.java

codebase/src/net/sourceforge/pain/network/console/Console.java · 3.9 KB

From the archive pain_0_44.tgz

Download the archive   « Back to the archive contents

package net.sourceforge.pain.network.console;


import net.sourceforge.pain.*;
import net.sourceforge.pain.data.type.*;
import net.sourceforge.pain.util.*;

import java.util.*;

public final class Console {

    protected static final int STATE_NEW = -1; //new connection
    protected static final int STATE_ACTIVE = 0; // active connection
    protected static final int STATE_SYSTEM_CLOSED = 1; // connection marked to be closed from system
    protected static final int STATE_REMOTE_CLOSED_1 = 2; // remotely closed connection
    protected static final int STATE_REMOTE_CLOSED_2 = 3; //

    private static final HashMap consoleByPlayerId = new HashMap();

    public static int MAX_INACTIVE_TIME = 6000; // in pulses

    private Object playerId = null;

    protected Object rawCommand = null;

    protected LinkedList input = new LinkedList();
    protected ConsoleAdapter adapter = null;
    protected int expireTime = 0; // in net.sourceforge.pain.Time pulses
    protected int state = STATE_NEW;

    protected boolean hasBufferedOutput = false;

//	private Map attributes = new HashMap();

    boolean newLineProcessingStarted = true;

    protected Console(ConsoleAdapter adapter) {
        this.adapter = adapter;
    }

//	protected ConsoleAdapter getAdapter() {
//		return adapter;
//	}

    public boolean isRawMode() {
        return rawCommand != null;
    }

    public boolean isCommandMode() {
        return rawCommand == null;
    }

    public void setRawMode(Object rawCommand) {
        this.rawCommand = rawCommand;
    }

    public void setCommandMode() {
        rawCommand = null;
    }

    public Object getRawCommand() {
        return rawCommand;
    }

    public String peekInputLine() {
        if (input.isEmpty()) {
            return null;
        }
        return (String) input.getFirst();
    }

    public String popInputLine() {
        if (input.isEmpty()) {
            return null;
        }
        return (String) input.removeFirst();
    }

    public Console out(String text) {
        if (text == null) {
            Log.warn("Console: out 'null'");
            return this;
        }
        if (!hasBufferedOutput && !isRawMode() && !newLineProcessingStarted) { //new console output should starts from new line
            adapterOut(ConsoleAdapter.NEW_LINE);
        }
        hasBufferedOutput = true;
        newLineProcessingStarted = false;
        adapterOut(text);
        return this;
    }

    private void adapterOut(String text) {
        if (adapter != null) {
            adapter.outText(text);
        }
    }

    public Player getPlayer() {
        return (Player) (playerId == null ? null : Core.getDB().getObject(playerId));
    }

    public void setPlayer(Player newPlayer) {
        if (playerId != null && newPlayer != null) {
            throw new RuntimeException("Owner redefinition not supported!");
        }
        if (playerId == null && newPlayer == null) {
            throw new RuntimeException("BUG, newPlayer is null!");
        }
        if (newPlayer == null) {
            consoleByPlayerId.remove(playerId);
            playerId = null;
        } else {
            playerId = newPlayer.getOid();
            consoleByPlayerId.put(newPlayer.getOid(), this);
        }
    }


    public void flushOutput() {
        if (!hasBufferedOutput) {
            return;
        }
        if (adapter != null) {
            try {
                adapter.flush();
            } catch (Exception e) {
                Log.error(e.getMessage(), e); // todo: revise ignorance
            }
        }
        hasBufferedOutput = false;
    }

    public void pushInputLine(String line) {
        input.addFirst(line);
    }

    public static Console getConsoleByOwner(Player player) {
        return (Console) consoleByPlayerId.get(player.getOid());
    }
}

Site Stats

Snippets: 1791
Codebases: 571