⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tacagent.java

📁 TAC 客户端 Tac agent 客户端 TAC 客户端 Tac agent 客户端
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
/** * TAC AgentWare * http://www.sics.se/tac        tac-dev@sics.se * * Copyright (c) 2001-2006 SICS AB. All rights reserved. * * SICS grants you the right to use, modify, and redistribute this * software for noncommercial purposes, on the conditions that you: * (1) retain the original headers, including the copyright notice and * this text, (2) clearly document the difference between any derived * software and the original, and (3) acknowledge your use of this * software in pertaining publications and reports.  SICS provides * this software "as is", without any warranty of any kind.  IN NO * EVENT SHALL SICS BE LIABLE FOR ANY DIRECT, SPECIAL OR INDIRECT, * PUNITIVE, INCIDENTAL OR CONSEQUENTIAL LOSSES OR DAMAGES ARISING OUT * OF THE USE OF THE SOFTWARE. * * ----------------------------------------------------------------- * * TACAgent * * Author  : Joakim Eriksson, Niclas Finne, Sverker Janson * Created : 23 April, 2002 * Updated : $Date: 2006/05/02 12:15:04 $ *	     $Revision: 1.6 $ * Purpose : TAC Classic AgentWare for Java */package se.sics.tac.aw;import java.io.BufferedInputStream;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream;import java.util.Properties;import java.util.logging.FileHandler;import java.util.logging.Level;import java.util.logging.Logger;import javax.swing.table.AbstractTableModel;import se.sics.tac.util.ArgEnumerator;import se.sics.tac.util.LogFormatter;public class TACAgent implements Task, TACMessageReceiver {  private final static String EOL = System.getProperty("line.separator",						       "\r\n");  public final static String VERSION = "Beta 9";  private static final Logger log =    Logger.getLogger(TACAgent.class.getName());  /** Command status */  public final static int NO_ERROR = 0;  public final static int INTERNAL_ERROR = 1;  public final static int AGENT_NOT_AUTH = 2;  public final static int GAME_NOT_FOUND = 4;  public final static int NOT_MEMBER_OF_GAME = 5;  public final static int GAME_FUTURE = 7;  public final static int GAME_COMPLETE = 10;  public final static int AUCTION_NOT_FOUND = 11;  public final static int AUCTION_CLOSED = 12;  public final static int BID_NOT_FOUND = 13;  public final static int TRANS_NOT_FOUND = 14;  public final static int CANNOT_WITHDRAW_BID = 15;  public final static int BAD_BIDSTRING_FORMAT = 16;  public final static int NOT_SUPPORTED = 17;  /** Extension in 1.1 */  public final static int GAME_TYPE_NOT_SUPPORTED = 18;  private final static String[] statusName = {    "no error",    "internal error",    "agent not auth",    "game not found",    "not member of game",    "game future",    "game complete",    "auction not found",    "auction closed",    "bid not found",    "trans not found",    "cannot withdraw bid",    "bad bidstring format",    "not supported",    "game type not supported"  };  private final static int[] statusCodes = {    NO_ERROR,    INTERNAL_ERROR,    AGENT_NOT_AUTH,    GAME_NOT_FOUND,    NOT_MEMBER_OF_GAME,    GAME_FUTURE,    GAME_COMPLETE,    AUCTION_NOT_FOUND,    AUCTION_CLOSED,    BID_NOT_FOUND,    TRANS_NOT_FOUND,    CANNOT_WITHDRAW_BID,    BAD_BIDSTRING_FORMAT,    NOT_SUPPORTED,    GAME_TYPE_NOT_SUPPORTED  };  /** Client preferences types */  public final static int ARRIVAL = 0;  public final static int DEPARTURE = 1;  public final static int HOTEL_VALUE = 2;  public final static int E1 = 3;  public final static int E2 = 4;  public final static int E3 = 5;  public final static int CAT_FLIGHT = 0;  public final static int CAT_HOTEL = 1;  public final static int CAT_ENTERTAINMENT = 2;  private final static String[] categoryName = {    "flight", "hotel", "entertainment"  };  /** TAC Types */  public final static int TYPE_INFLIGHT = 1;  public final static int TYPE_OUTFLIGHT = 0;  public final static int TYPE_GOOD_HOTEL = 1;  public final static int TYPE_CHEAP_HOTEL = 0;  public final static int TYPE_ALLIGATOR_WRESTLING = 1;  public final static int TYPE_AMUSEMENT = 2;  public final static int TYPE_MUSEUM = 3;  public final static int MIN_FLIGHT = 0;  public final static int MIN_HOTEL = 8;  public final static int MIN_ENTERTAINMENT = 16;  public final static int MAX_FLIGHT = 7;  public final static int MAX_HOTEL = 15;  public final static int MAX_ENTERTAINMENT = 27;  /** Internal operations to perform after transactions have been received */  private final static int OP_NOOP = 0x000;  private final static int OP_GAME_STARTS = 0x001;  private final static int OP_GAME_ENDS = 0x002;  private final static int OP_CLOSE_AUCTION = 0x100;  private final static int OP_CLEAR_BID = 0x100000;  /** The number of auctions in a TAC game */  private final static int NO_AUCTIONS = 28;  /** Timeout for quotes (when waiting for reply) */  private final static int QUOTE_TIMEOUT = 120 * 1000;  /** Constants for automatic updates and game */  private final static int INFO_UPDATE_PERIOD = 30000;  private final static int DEFAULT_GAME_LENGTH = 12 * 60 * 1000;  private final static String[] auctionType = new String[] {    "Inflight 1", "Inflight 2", "Inflight 3", "Inflight 4",    "Outflight 2", "Outflight 3", "Outflight 4", "Outflight 5",    "Cheap Hotel 1", "Cheap Hotel 2", "Cheap Hotel 3", "Cheap Hotel 4",    "Good Hotel 1", "Good Hotel 2", "Good Hotel 3", "Good Hotel 4",    "Alligator 1", "Alligator 2", "Alligator 3", "Alligator 4",    "Amusement 1", "Amusement 2", "Amusement 3", "Amusement 4",    "Museum 1", "Museum 2", "Museum 3", "Museum 4"  };  private AgentImpl agent;  private String host = "localhost";  private int port = 6500;  private String userName;  private String password;  private String logPrefix = "aw";  private String childLogPrefix = logPrefix;  private LogFormatter logFormatter;  private FileHandler rootFileHandler;  private FileHandler childFileHandler;  private String childFileName;  private Properties config;  private TACConnection connection = null;  private int nextGameID = -1;  private long nextGameTime = -1;  private String gameType;  private int userID = -1;  private long timeDiff = 0;  private boolean isNextGameTaskRunning = false;  private int lastHotelAuction = -1;  private int clearID = 0;  // Client Preferences  private int[][] clientPrefs = new int[8][6];  // Auction and ownership information  private int[] auctionIDs = new int[NO_AUCTIONS];  private int[] owns = new int[NO_AUCTIONS];  private Bid[] bids = new Bid[NO_AUCTIONS];  private Quote[] quotes = new Quote[NO_AUCTIONS];  private float[] costs = new float[NO_AUCTIONS];  private long[] pendingQuotes = new long[NO_AUCTIONS];  private int[] allocate = new int[NO_AUCTIONS];  private int playingGame = -1;  private long startTime = 0;  private int gameLength = DEFAULT_GAME_LENGTH;  private String playingGameType;  private int earliestTransID = -1;  private boolean isGameStarted = false;  private int[] transActions = new int[10];  private int transActionsNum = 0;  private int[] waitActions = new int[10];  private int waitActionsNum = 0;  private long lastSentTransactionRequest = 0L;  private int printOwnDelay = 0;  private AgentTableModel tableModel;  private AgentDisplay display;  private String connectionClassName;  private int exitAfterGames = -1;  private int gamesPlayed = 0;  private int lastGamePlayed = -1;  private TACAgent(AgentImpl agent) {    this.agent = agent;    for (int i = 0; i < NO_AUCTIONS; i++) {      quotes[i] = new Quote(i);    }  }  /**   * This constructor is only for backward compability   *   * @deprecated   */  public TACAgent(AgentImpl agent, String host, int port,		  String user, String pwd, String className) {    this(agent);    if (host != null) {      this.host = host;      this.port = port;    }    userName = user;    password = pwd;    connectionClassName = className;    // Default initialization of logging. Should this be done??? FIX THIS!!!    initLogging(3, 0, false);    log.fine("Starting TAC AgentWare version " + VERSION);    log.fine("Using agent implementation " + agent.getClass().getName());    log.fine("Using TAC server " + host + " at port " + port);    agent.init(this, new ArgEnumerator(new String[0], "", false));    connect();  }  public TACAgent(AgentImpl agent, ArgEnumerator a, Properties config) {    this(agent);    this.config = config;    userName =      trim(a.getArgument("-agent", config.getProperty("agent", null)));    password = trim(a.getArgument("-password",				  config.getProperty("password", null)));    if (userName == null) {      System.err.println("Missing value for agent name");      a.usage(1);    }    if (password == null) {      System.err.println("Missing value for agent password");      a.usage(1);    }    host =      trim(a.getArgument("-host", config.getProperty("host", "localhost")));    port = a.getArgument("-port", getInt(config, "port", 6500));    connectionClassName =      trim(a.getArgument("-connection",			 config.getProperty("connection",					    "se.sics.tac.aw.TACReader")));    gameType =      trim(a.getArgument("-gameType", config.getProperty("gameType", null)));    exitAfterGames = a.getArgument("-exitAfterGames",				   getInt(config, "exitAfterGames", -1));    if (exitAfterGames == 0) {      // Exit immediately???      System.err.println("Exit as requested after " + exitAfterGames			 + " played games");      System.exit(0);    }    int consoleLevel =      a.getArgument("-consoleLogLevel", getInt(config, "consoleLogLevel", 3));    int fileLevel =      a.getArgument("-fileLogLevel", getInt(config, "fileLogLevel", 0));    this.logPrefix =      trim(a.getArgument("-logPrefix", config.getProperty("logPrefix", "aw")));    initLogging(consoleLevel, fileLevel, true);    // Create directories for logs    File fp = new File("games");    if ((!fp.exists() && !fp.mkdir()) || !fp.isDirectory()) {      this.childLogPrefix = this.logPrefix;      log.severe("could not create directory 'games'");    } else {      this.childLogPrefix = "games" + File.separatorChar + this.logPrefix;    }    printOwnDelay = a.getArgument("-printOwnDelay",				  getInt(config, "printOwnDelay", 0)) * 1000;    log.fine("Starting TAC AgentWare version " + VERSION);    log.fine("Using agent implementation " + agent.getClass().getName());    log.fine("Using TAC server " + host + " at port " + port);    agent.init(this, a);    // Make sure all arguments have been extracted    a.checkArguments();    // Allow garbage collection    this.config = null;    connect();  }  // -------------------------------------------------------------------  // Configuration handling - only available during agent initialization  // -------------------------------------------------------------------  public String getConfig(String name, String defaultValue) {    if (config == null) {      return defaultValue;    }    return trim(config.getProperty(name, defaultValue));  }  public int getConfig(String name, int defaultValue) {    if (config == null) {      return defaultValue;    }    return getInt(config, name, defaultValue);  }  // -------------------------------------------------------------------  // GUI handling  // -------------------------------------------------------------------  public void showGUI() {    if (display == null) {      tableModel = new AgentTableModel();      display = new AgentDisplay(tableModel, this);    }    display.setVisible(true);  }  // -------------------------------------------------------------------  // Connection handling  // -------------------------------------------------------------------  private void connect() {    do {      try {	connection = (TACConnection) Class.forName(connectionClassName).	  newInstance();      } catch (Exception e) {	log.log(Level.SEVERE, "could not create TACConnection object of class "		+ connectionClassName, e);	fatalError("no TACConnection  available");      }      try {	connection.init(this);      } catch (Exception e) {	log.log(Level.SEVERE, "Connection failure:", e);      }      if (!connection.isConnected()) {	log.warning("could not connect to server " + host + " at port "		    + port + " (will retry in 5 seconds)");	try { 	  Thread.sleep(5000);	} catch (Exception e) {	}      }    } while (!connection.isConnected());  }  // -------------------------------------------------------------------  // Timer tasks - handles game start/end, quote and bid requests, etc  // -------------------------------------------------------------------  private void cancelTimers() {    TimeDispatcher d = TimeDispatcher.getDefault();    d.cancelTask("gameStarts", this);    d.cancelTask("gameEnds", this);    d.cancelTask("hotelQuotes", this);    d.cancelTask("flightQuotes", this);    d.cancelTask("quotes", this);    d.cancelTask("bids", this);    d.cancelTask("printOwn", this);  }  public void performWork(long time, Object key, Object value) {    TimeDispatcher td = TimeDispatcher.getDefault();    if (key == "hotelQuotes") {      // Request all hotel quotes      if (value == connection) {	td.addTask(time + 60000, key, value, this);	TACConnection conn = (TACConnection) value;	for (int i = MIN_HOTEL; i <= MAX_HOTEL; i++) {	  if (!quotes[i].isAuctionClosed()) {	    lastHotelAuction = i;	    requestQuote(quotes[i], conn, false);	  }	}      }    } else if (key == "flightQuotes") {      // Request all flight quotes      if (value == connection) {	td.addTask(time + 10000, key, value, this);	TACConnection conn = (TACConnection) value;	for (int i = MIN_FLIGHT; i <= MAX_FLIGHT; i++) {	  if (!quotes[i].isAuctionClosed()) {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -