[126] | 1 | package agents.qoagent;
|
---|
| 2 |
|
---|
| 3 | /**
|
---|
| 4 | * The AutomatedAgentDelayedMessageThread class allows to send a message in a delay
|
---|
| 5 | * @author Raz Lin
|
---|
| 6 | * @version 1.0
|
---|
| 7 | * @see AutomatedAgent
|
---|
| 8 | */
|
---|
| 9 | public class AutomatedAgentDelayedMessageThread extends Thread {
|
---|
| 10 |
|
---|
| 11 | public final static long SLEEP_TIME_FACTOR = 3; // sleep half of turn time
|
---|
| 12 | private String m_sOffer;
|
---|
| 13 | private String m_sResponse;
|
---|
| 14 | private int m_nCurrentTurn;
|
---|
| 15 | private long m_lSleepTime;
|
---|
| 16 | private AutomatedAgent m_agent;
|
---|
| 17 | private int m_nMessageType = NO_TYPE;
|
---|
| 18 | public static final int NEW_OFFER_TYPE = 0;
|
---|
| 19 | public static final int RESPONSE_TYPE = 1;
|
---|
| 20 | public static final int NO_TYPE = -1;
|
---|
| 21 | public static final long RESPONSE_SLEEP_TIME_MILLIS = 15000; // 15 seconds
|
---|
| 22 |
|
---|
| 23 | AutomatedAgentDelayedMessageThread(AutomatedAgent agent, String sOffer, int nCurrentTurn)
|
---|
| 24 | {
|
---|
| 25 | m_nMessageType = NEW_OFFER_TYPE;
|
---|
| 26 | m_agent = agent;
|
---|
| 27 | m_sOffer = sOffer;
|
---|
| 28 | m_nCurrentTurn = nCurrentTurn;
|
---|
| 29 |
|
---|
| 30 | // sleep for a while - time in milliseconds
|
---|
| 31 | long lSecondsPerTurn = m_agent.getSecondsForTurn();
|
---|
| 32 | long lMillisPerTurn = lSecondsPerTurn * 1000;
|
---|
| 33 |
|
---|
| 34 | m_lSleepTime = lMillisPerTurn / SLEEP_TIME_FACTOR;
|
---|
| 35 | }
|
---|
| 36 |
|
---|
| 37 | AutomatedAgentDelayedMessageThread(AutomatedAgent agent, String sResponse)
|
---|
| 38 | {
|
---|
| 39 | m_nMessageType = RESPONSE_TYPE;
|
---|
| 40 | m_agent = agent;
|
---|
| 41 | m_sResponse = sResponse;
|
---|
| 42 |
|
---|
| 43 | // sleep for 15 seconds before answering
|
---|
| 44 | m_lSleepTime = RESPONSE_SLEEP_TIME_MILLIS;
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | public void run()
|
---|
| 48 | {
|
---|
| 49 | try {
|
---|
| 50 | sleep((long)m_lSleepTime);
|
---|
| 51 | } catch (InterruptedException e) {
|
---|
| 52 | e.printStackTrace();
|
---|
| 53 | System.out.println("[AA]ERROR: Error during sleep" + e.getMessage() + " [AutomatedAgentDelayedMessageThread::run(35)]");
|
---|
| 54 | System.err.println("[AA]ERROR: Error during sleep" + e.getMessage() + " [AutomatedAgentDelayedMessageThread::run(35)]");
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 | if (m_nMessageType == NEW_OFFER_TYPE)
|
---|
| 58 | {
|
---|
| 59 | // check if message is still valid
|
---|
| 60 | boolean bSendOffer = m_agent.getSendOfferFlag();
|
---|
| 61 | int nCurrentTurn = m_agent.getCurrentTurn();
|
---|
| 62 |
|
---|
| 63 | if (nCurrentTurn == m_nCurrentTurn)
|
---|
| 64 | {
|
---|
| 65 | // check whether to send the message or not
|
---|
| 66 | if (bSendOffer)
|
---|
| 67 | m_agent.printMessageToServer(m_sOffer);
|
---|
| 68 | }
|
---|
| 69 | }
|
---|
| 70 | else if (m_nMessageType == RESPONSE_TYPE)
|
---|
| 71 | {
|
---|
| 72 | m_agent.printMessageToServer(m_sResponse);
|
---|
| 73 | }
|
---|
| 74 | }
|
---|
| 75 | }
|
---|