2008年6月25日星期三

代码高亮测试


/*
* Copyrights (C) 2008 Bearice (Bearice@Gmail.com)
* Release under GNU/GPL Version 2.
*/
package cn.bearice.ipcontroller.client.core.remote.guet;

import cn.bearice.java.security.Account;
import cn.bearice.ipcontroller.controller.ControllerServer;
import cn.bearice.ipcontroller.RemoteServer;
import cn.bearice.ipcontroller.events.ServerStatusChangeListerer;
import cn.bearice.java.util.listener.*;
import java.io.IOException;
import java.lang.reflect.Method;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketAddress;
import java.net.SocketException;
import java.util.Collection;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.security.auth.login.AccountException;
import javax.security.auth.login.AccountLockedException;
import javax.security.auth.login.AccountNotFoundException;

/**
*
* @author Bearice
*/
public class GUET_UDPControllerServer implements ControllerServer {

public static final int STAT_NOT_LOGED_ON = 0;
public static final int STAT_LOGED_ON = 2;
public static final int STAT_LOGON_PENDDING = 1;
private static final Method statusChangedMethod;


static {
try {
statusChangedMethod = ServerStatusChangeListerer.class.getDeclaredMethod(
"serverStatusChanged",
new Class[]{
RemoteServer.class,
int.class,
int.class
});
} catch (Exception e) {
throw new ExceptionInInitializerError(e);
}
}

class StatusChangeListenerSupport extends ListenerSupport {

void fireStatusChanged(int before, int after) {
this.fireEventAsync(statusChangedMethod, GUET_UDPControllerServer.this, before, after);
}
}
public static final int CLIENT_LISTEN_PORT = 26000;
StatusChangeListenerSupport statusChangeListenerSupport = new StatusChangeListenerSupport();
SocketAddress remoteAddress;
DatagramSocket socket;
protected int status = STAT_NOT_LOGED_ON;

protected void receive(byte[] buff) throws IOException {
DatagramPacket packet = new DatagramPacket(buff, buff.length);
do {
socket.receive(packet);
} while (packet.getSocketAddress().equals(remoteAddress));
}

protected void send(byte[] buff) throws IOException {
DatagramPacket packet = new DatagramPacket(buff, buff.length);
packet.setSocketAddress(remoteAddress);
socket.send(packet);
}

public GUET_UDPControllerServer(SocketAddress remoteAddress) {
try {
this.remoteAddress = remoteAddress;
socket = new DatagramSocket(CLIENT_LISTEN_PORT);
} catch (SocketException ex) {
Logger.getLogger(GUET_UDPControllerServer.class.getName()).log(Level.SEVERE, null, ex);
throw new RuntimeException(ex);
}
}

public void logon(Account account) throws AccountException {
try {
setStatus(STAT_LOGON_PENDDING);
GUET_PreLogonPacket lp = new GUET_PreLogonPacket(account.getName());
send(lp.toByteArray());
byte[] buff = new byte[22];
receive(buff);
lp.readByteArray(buff);
switch (lp.status) {
case GUET_PreLogonPacket.STATUS_ERROR:
throw new AccountNotFoundException();
case GUET_PreLogonPacket.STATUS_LOCKED:
throw new AccountLockedException();
case GUET_PreLogonPacket.STATUS_OK:
break;
default:
throw new IOException("Illegal PLP.status");
}
lp = new GUET_LogonPacket(account.getName(), account.getPassword());
send(lp.toByteArray());
buff = new byte[32];
receive(buff);
lp.readByteArray(buff);
switch (lp.status) {
case GUET_PreLogonPacket.STATUS_ERROR:
throw new AccountNotFoundException();
case GUET_PreLogonPacket.STATUS_LOCKED:
throw new AccountLockedException();
case GUET_PreLogonPacket.STATUS_OK:
break;
default:
throw new IOException("Illegal PLP.status");
}
setStatus(STAT_LOGED_ON);
} catch (IOException ex) {
Logger.getLogger(GUET_UDPControllerServer.class.getName()).log(Level.SEVERE, null, ex);
throw new AccountException("Error while commucating with server: " + ex);
}
}

public void logoff() {
if (getStatus() != STAT_LOGED_ON) {
throw new IllegalStateException("not loged on!");
}

}

public int getStatus() {
return status;
}

protected void setStatus(int status) {
int oldstst = this.status;
this.status = status;
statusChangeListenerSupport.fireStatusChanged(oldstst, status);
}

public void addStatusChangeListerer(ServerStatusChangeListerer linstener) {
throw new UnsupportedOperationException("Not supported yet.");
}

public void removeStatusChangeLinstener(ServerStatusChangeListerer lintener) {
throw new UnsupportedOperationException("Not supported yet.");
}

public Collection getStatusChangeListeners() {
throw new UnsupportedOperationException("Not supported yet.");
}
}

0 人次吐槽:

发表评论