DrunkenMaster.java

import java.nio.*;
import java.nio.channels.*;

public class DrunkenMaster extends BasicQuagent{
    
    private boolean mission_completed = false;

    public DrunkenMaster (SocketChannel s) {

	super(s);
    }

    public void run() {

	// get the greeting message
	getGreeting();

	while(!mission_completed ) {

	    handleEvent();

	    double dist = Math.random() * 100.;
	    Request walk = new WalkCommand((float)dist);
	    send(walk);
	    Response walk_rsp = getResponse(walk);

	    Request probe = new RadiusQuery(100.0f);
	    send(probe);
	    Response env = getResponse(probe);
	    if(env.isOK()) 
		((RadiusResponse)env).dumpValue();

	    double angle = Math.random() * 90.;
	    Request turn = new TurnCommand((float)angle);
	    send(turn);
	    Response turn_rsp = getResponse(turn);

	    Request well = new WellbeingCommand();
	    send(well);
	    Response age = getResponse(well);
	    if(age.isOK()) 
		((WellbeingResponse)age).dumpValue();

	    try{
		Thread.sleep(2000);
	    }catch (InterruptedException e) { }

	}

    }

    public void handleEvent(){

	while(!event_list.isEmpty()){

	    // handle event here
	    QuagentEvent ev = (QuagentEvent)event_list.removeFirst();
	    if(ev.type() == "DEAD")
		mission_completed = true;

	    System.out.println("Processing event "+ev.value());
	}
    }
}


Generated by GNU enscript 1.6.1.