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

📄 tacagent.java

📁 TAC 客户端 Tac agent 客户端 TAC 客户端 Tac agent 客户端
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
	handleBidSubmission(msg);      } else if (msg.isTag("replaceBid")) {	handleBidSubmission(msg);      } else if (msg.isTag("getQuote")) {	handleQuote(msg);      } else if (msg.isTag("bidInfo")) {	handleBidInfo(msg);      } else if (msg.isTag("transIDs")) {	handleTransIDs(msg);      } else if (msg.isTag("transInfo")) {	handleTransInfo(msg);      } else if (msg.isTag("recoverBidIDs")) {	handleRecoverBidIDs(msg);      }    }  }  private void requestNextGame() {    TACMessage msg = new TACMessage("nextGame");    if (gameType != null) {      msg.setParameter("type", gameType);    }    // Set stat info if more than 5 messages sent!    if (TACMessage.getMessageCount() > 5) {      msg.setParameter("stat.avgResponseTime",		       "" + TACMessage.getAverageResponseTime());      msg.setParameter("stat.messageCount",		       "" + TACMessage.getMessageCount());    }    sendMessage(msg, this);  }  private void requestQuotes(TACConnection conn, boolean flightQuotes,			     boolean hotelQuotes) {    // This should be changed so that it will only request those quotes    // that are old enough...    if (flightQuotes) {      for (int i = MIN_FLIGHT; i <= MAX_FLIGHT ; i++) {	if (!quotes[i].isAuctionClosed()) {	  requestQuote(quotes[i], conn, false);	}      }    }    for (int i = MIN_ENTERTAINMENT; i <= MAX_ENTERTAINMENT ; i++) {      if (!quotes[i].isAuctionClosed()) {	requestQuote(quotes[i], conn, false);      }    }    if (hotelQuotes) {      for (int i = MIN_HOTEL; i <= MAX_HOTEL ; i++) {	if (!quotes[i].isAuctionClosed()) {	  lastHotelAuction = i;	  requestQuote(quotes[i], conn, false);	}      }    }  }  private void requestQuote(Quote quote, TACConnection conn, boolean force) {    int auction = quote.getAuction();    int auctionID = auctionIDs[auction];    if (auctionID > 0) {      long currentTime = System.currentTimeMillis();//       log.info("requesting quote for " + auctionID);      if (!force && (pendingQuotes[auction] > 0)	  && ((pendingQuotes[auction] + QUOTE_TIMEOUT) > currentTime)) {	// Quote is already pending and it has not passed sufficient time	// to regards a retransmission (no use to request quotes faster	// than they arrive)	long delay = currentTime - pendingQuotes[auction];	if (delay > 4000) {	  // Warn if the quote has been delayed too long	  log.warning("still awaiting quote for auction " + auction		      + " (" + getAuctionTypeAsString(auction)		      + ") after " + (delay / 1000) + " sec");	}      } else {	pendingQuotes[auction] = currentTime;	try {	  TACMessage msg = new TACMessage("getQuote");	  msg.setParameter("auctionID", auctionID);	  Bid bid = bids[auction];	  msg.setUserData(quote);	  if (bid != null) {	    int id;	    if ((id = bid.getID()) != Bid.NO_ID) {	      msg.setParameter("bidID", id);	      msg.setUserData(bid);	    } else if (((bid = bid.getReplacing()) != null)		       && ((id = bid.getID()) != Bid.NO_ID)) {	      // Request HQW for previous bid if it currently is being	      // replaced (in case the new bid is rejected)	      msg.setParameter("bidID", id);	      msg.setUserData(bid);	    }	  }	  conn.sendMessage(msg, this);	} catch (Exception e) {	  log.log(Level.SEVERE, "could not request quote for auction "		  + auction + " (" + getAuctionTypeAsString(auction) + ')', e);	  pendingQuotes[auction] = 0L;	  reset(0, conn);	}      }    }  }  private void requestBidInfos(TACConnection conn) {    Bid bid;    int bidID;    try {      for (int i = 0; i < NO_AUCTIONS; i++) {	bid = bids[i];	if (bid != null && ((bidID = bid.getID()) != Bid.NO_ID)	    && !quotes[i].isAuctionClosed()) {	  TACMessage msg = new TACMessage("bidInfo");	  msg.setParameter("bidID", bidID);	  msg.setUserData(bid);	  conn.sendMessage(msg, this);	}      }    } catch (IOException e) {      log.log(Level.SEVERE, "could not request bid infos", e);      reset(0, conn);    }    requestTransactions(OP_NOOP);  }  private synchronized void requestTransactions(int call) {    if (transActionsNum == 0) {      transActions[0] = call;      transActionsNum++;      TACMessage msg = new TACMessage("transIDs");      msg.setParameter("earliestTransID", earliestTransID);      lastSentTransactionRequest = System.currentTimeMillis();      sendMessage(msg, this);    } else {      if (waitActionsNum == waitActions.length) {	int[] tmp = new int[waitActions.length * 2];	System.arraycopy(waitActions, 0, tmp, 0, waitActionsNum);	waitActions = tmp;      }      waitActions[waitActionsNum++] = call;      long currentTime = System.currentTimeMillis();      if ((currentTime - lastSentTransactionRequest) > 30000) {	// Too long time after last sent transaction	TACMessage msg = new TACMessage("transIDs");	msg.setParameter("earliestTransID", earliestTransID);	lastSentTransactionRequest = currentTime;	log.warning("WARNING: transaction timeout after "		    + ((currentTime - lastSentTransactionRequest) / 1000)		    + " sec!!! (resending)");	sendMessage(msg, this);      }    }  }  private void prepareBidMsg(TACMessage msg, Bid bid) {    int auction = bid.getAuction();    msg.setParameter("auctionID", auctionIDs[auction]);    msg.setParameter("bidString", bid.getBidString());    msg.setParameter("expireTime", 0);    msg.setParameter("expireMode", 0);    msg.setParameter("divisible", 1);    msg.setUserData(bid);  }  private void nextGameStarts(TACConnection conn) {    clearAll();    playingGame = nextGameID;    startTime = nextGameTime;    playingGameType = null;    gameLength = DEFAULT_GAME_LENGTH;    earliestTransID = -1;    enterGameLog(nextGameID);    log.fine("Starting up game: " + playingGame);    // If illegal state, end game and restart... should not happen?    if (playingGame == -1) {      log.severe("GameID error, GameID = -1 - aborting game start...");      gameEnds();      return;    }    try {      TACMessage msg = new TACMessage("getGameAuctionIDs");      msg.setParameter("gameID", nextGameID);      conn.sendMessage(msg, this);    } catch (IOException e) {      log.log(Level.SEVERE, "could not request game auctions", e);      reset(0, conn);    }  }  private void gameEnds() {    log.fine("Game " + nextGameID + " has ended");    playingGame = -1;    nextGameID = -1;    isGameStarted = false;    cancelTimers();    if (earliestTransID != -1) {      requestTransactions(OP_GAME_ENDS);    } else {      handleGameEnd();    }    if (exitAfterGames <= 0 || gamesPlayed < exitAfterGames) {      requestNextGame();    }  }  private synchronized void handleGameEnd() {    exitGameLog();    if (exitAfterGames > 0 && gamesPlayed >= exitAfterGames) {      // We have played the specified number of games      log.info("Exit as requested after " + exitAfterGames	       + " played games");      disconnect(500);      if (rootFileHandler != null) {	rootFileHandler.close();      }      System.exit(0);    }  }  private boolean handleLogin(TACMessage msg) {    int status = NO_ERROR;    while (msg.nextTag()) {      if (msg.isTag("userID")) {	userID = msg.getValueAsInt(-1);	log.fine("Logged in as " + userID);	TACMessage msg2 = new TACMessage("serverTime");	sendMessage(msg2, this);	// Do not request "nextGame" if already playing a game!!	if (playingGame == -1) {	  requestNextGame();	}	return true;      } else if (msg.isTag("commandStatus")) {	status = msg.getValueAsInt(NO_ERROR);      }    }    fatalError("Failed to login as " + userName + ": status="	       + commandStatusToString(status));    return false;  }  private void handleBidSubmission(TACMessage msg) {    Bid bid = (Bid) msg.getUserData();    int status = NO_ERROR;    while (msg.nextTag()) {      if (msg.isTag("bidID")) {	int id = msg.getValueAsInt(Bid.NO_ID);	bid.setID(id);      } else if (msg.isTag("bidHash")) {	String hash = msg.getValue();	bid.setBidHash(hash);      } else if (msg.isTag("rejectReason")) {	int reject = msg.getValueAsInt(Bid.NOT_REJECTED);	bid.setRejectReason(reject);	if (reject != Bid.NOT_REJECTED) {	  bid.setProcessingState(Bid.REJECTED);	}      } else if (msg.isTag("commandStatus")) {	status = mapCommandStatus(msg.getValueAsInt(NO_ERROR));      }    }    if (bid.isRejected()) {      // reset the active bid!      revertBid(bid, NO_ERROR);    } else if (status == AUCTION_CLOSED) {      // reset the active bid!      revertBid(bid, status);      // Let the quote close the auction later!    } else if (status != NO_ERROR) {      fatalError("Can not handle bid submission: "		 + commandStatusToString(status), 5000);    } else {      // Request Bid info      TACMessage msg2 = new TACMessage("bidInfo");      msg2.setParameter("bidID", bid.getID());      msg2.setUserData(bid);      sendMessage(msg2, this);      // Should not do this until bidinfo arrives where the bid is      // hopefully no longer preliminary. (For backward compability!)//       agent.bidUpdated(bid);    }  }  // the bid "bid" has been rejected/ or in error  // ensure that the information about active bid, etc is correct  // call agent  private synchronized void revertBid(Bid bid, int status) {    int auction = bid.getAuction();    Bid activeBid = getBid(auction);    if (bid.same(activeBid)) {      activeBid = bid.getReplacing();      if (activeBid != null) {	bids[auction] = activeBid;      } else {	bids[auction] = null;      }    } else if (activeBid != null) {      Bid child;      while ((child = activeBid.getReplacing()) != null && !child.same(bid)) {	activeBid = child;      }      if (child != null && child.same(bid)) {	activeBid.setReplacing(child.getReplacing());	bid = null;      }      activeBid = null;    }    // if this was the active bid    if (bid != null) {      if (status == NO_ERROR) {	try {	  agent.bidRejected(bid);	} catch (Exception e) {	  log.log(Level.SEVERE, "agent could not handle bidRejected", e);	}      } else {	try {	  agent.bidError(bid, status);	} catch (Exception e) {	  log.log(Level.SEVERE, "agent could not handle bidError", e);	}      }    }//     if (activeBid != null && !activeBid.isPreliminary()) {//       agent.bidUpdated(activeBid);//     }  }  private void handleTransIDs(TACMessage msg) {    TACMessage msg2 = null;    int oldEarliest = earliestTransID;    while (msg.nextTag()) {      if (msg.isTag("transID")) {	int id = msg.getValueAsInt(-1);	if (id > earliestTransID) {	  earliestTransID = id;	}	if (id > oldEarliest) {	  msg2 = new TACMessage("transInfo");	  msg2.setParameter("transID", id);	  sendMessage(msg2, this);	}      }    }    if (msg2 != null) {      // Indicate that this was the last for this transaction sessions      msg2.setUserData(this);    } else {      // Nothing to retrieve      callAgent();    }  }  private void handleTransInfo(TACMessage msg) {    int quantity = 0;    int auction = 0;    float price = 0f;    int status = NO_ERROR;    while (msg.nextTag()) {      if (msg.isTag("/transInfo")) {	if (status == NO_ERROR) {	  Transaction trans = new Transaction(auction, quantity, price);	  owns[auction] += quantity;	  costs[auction] += quantity * price;	  try {	    if (tableModel != null) {	      tableModel.fireTableRowsUpdated(auction, auction);	    }	    agent.transaction(trans);	  } catch (Exception e) {	    log.log(Level.SEVERE, "agent could not handle transaction "		    + trans, e);	  }	} else {	  // What should we do here??? FIX THIS!!!	}      } else if (msg.isTag("quantity")) {	quantity = (int) msg.getValueAsFloat(0f);      } else if (msg.isTag("price")) {	price =  msg.getValueAsFloat(0f);      } else if (msg.isTag("auctionID")) {	auction = getAuctionPos(msg.getValueAsInt(0));      } else if (msg.isTag("commandStatus")) {	status = msg.getValueAsInt(NO_ERROR);      }    }    Object obj = msg.getUserData();    if (obj != null) {      callAgent();    }  }  private void callAgent() {    for (int i = 0; i < transActionsNum; i++) {      int ival = transActions[i];      if ((OP_CLEAR_BID & ival) != 0) {	clearBid(ival - OP_CLEAR_BID);      } else {	try {	  if ((OP_CLOSE_AUCTION & ival) != 0) {	    agent.auctionClosed(ival - OP_CLOSE_AUCTION);	  } else if (ival == OP_GAME_STARTS) {	    // Another game is being played	    if (playingGame != lastGamePlayed) {	      lastGamePlayed = playingGame;	      gamesPlayed++;	    }	    agent.gameStarted();	  } else if (ival == OP_GAME_ENDS) {	    agent.gameStopped();	  }	} catch (Throwable e) {	  log.log(Level.SEVERE, "agent could not handle operation " +		  ival, e);	  // Check if thread was killed	  if (e instanceof ThreadDeath) {	    throw (ThreadDeath) e;	  }	}	if (ival == OP_GAME_ENDS) {	  handleGameEnd();	} else if (ival == OP_GAME_STARTS) {	  TimeDispatcher d = TimeDispatcher.getDefault();

⌨️ 快捷键说明

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