[127] | 1 | package agents.qoagent;
|
---|
| 2 |
|
---|
| 3 | //file name: GameTimeServer.java
|
---|
| 4 | import javax.swing.*;
|
---|
| 5 | import java.io.*;
|
---|
| 6 | import java.net.*;
|
---|
| 7 |
|
---|
| 8 | /*****************************************************************
|
---|
| 9 | * Class name: GameTimeServer
|
---|
| 10 | * Goal: Creating the stop-watch using threads.
|
---|
| 11 | * Input: None.
|
---|
| 12 | * Output: None.
|
---|
| 13 | ****************************************************************/
|
---|
| 14 | class GameTimeServer extends JPanel implements Runnable
|
---|
| 15 | {
|
---|
| 16 | private static final long serialVersionUID = 1L;
|
---|
| 17 | //DT: private ServerThread m_st = null; //Allows access to the agent's thread at the server
|
---|
| 18 | //DT: private MultiServer m_server; //Allows access to the server
|
---|
| 19 | private Agent m_agent; //Allows access to the agent running the stop watch
|
---|
| 20 | private int m_nMaxTurn; //Number of turns in the negotiation
|
---|
| 21 | private boolean m_bIsTurn; //True value in this variable means that the
|
---|
| 22 | //stop watch is used for end-turn. False value means it is used for
|
---|
| 23 | //end-negotiation.
|
---|
| 24 | private long m_nTime; //the timer
|
---|
| 25 | private JLabel m_lTime; //label for displaying the stop-watch
|
---|
| 26 | private boolean m_bRun; //should the clock continue to run?
|
---|
| 27 | private boolean m_bCountUp; //specifies whether the clock counts down or up.
|
---|
| 28 | private String m_strStartTime; //the text in m_lTime label
|
---|
| 29 | private int m_nStartSeconds; //seconds value
|
---|
| 30 | private int m_nStartMinutes; //minutes value
|
---|
| 31 | private int m_nStartHours; //hours value
|
---|
| 32 |
|
---|
| 33 |
|
---|
| 34 | /*****************************************************************
|
---|
| 35 | * Method name: GameTimeServer()
|
---|
| 36 | * Goal: Constructor.
|
---|
| 37 | * Description: Initialize the class variables. The timer itself is
|
---|
| 38 | * set by the values of the variables: hours, minutes and seconds.
|
---|
| 39 | * Input: A boolean which specifies whether the stop watch counts up or not,
|
---|
| 40 | * three integers (hours, minutes and seconds), an Agent (the one running
|
---|
| 41 | * the stop watch), another boolean which specifies whether the stop watch is
|
---|
| 42 | * of type end-turn or end-negotiation, another integer (number of turns in
|
---|
| 43 | * the negotiation), a MultiServer object and a ServerThread object.
|
---|
| 44 | * Output: None.
|
---|
| 45 | ****************************************************************/
|
---|
| 46 | public GameTimeServer(boolean bCountUp,int nHours,int nMinutes, int nSeconds,Agent agent,boolean bTurnOrNeg,int nMaxTurn /* DT: , MultiServer server,ServerThread st */)
|
---|
| 47 | {
|
---|
| 48 | /*DT: m_st=st;
|
---|
| 49 | m_server=server; */
|
---|
| 50 | m_nMaxTurn=nMaxTurn;
|
---|
| 51 | m_agent=agent;
|
---|
| 52 | m_bIsTurn=bTurnOrNeg;
|
---|
| 53 | m_bCountUp = bCountUp;
|
---|
| 54 | m_bRun = false;
|
---|
| 55 |
|
---|
| 56 | if (!bCountUp)
|
---|
| 57 | {
|
---|
| 58 | m_nStartMinutes = nMinutes;
|
---|
| 59 | m_nStartSeconds = nSeconds;
|
---|
| 60 | m_nStartHours = nHours;
|
---|
| 61 |
|
---|
| 62 | String sec, min, hour;
|
---|
| 63 |
|
---|
| 64 | m_nTime = nHours*3600+ nMinutes*60 + nSeconds;
|
---|
| 65 |
|
---|
| 66 | // initializing the seconds and minutes variables
|
---|
| 67 | if ((m_nTime % 60) <10)
|
---|
| 68 | sec = "0"+ m_nTime % 60;
|
---|
| 69 | else
|
---|
| 70 | sec = m_nTime % 60 + "";
|
---|
| 71 |
|
---|
| 72 | if (( (m_nTime/60) % 60) <10)
|
---|
| 73 | min = "0"+ (m_nTime/60) % 60;
|
---|
| 74 | else
|
---|
| 75 | min = (m_nTime/60) % 60 + "";
|
---|
| 76 |
|
---|
| 77 | if ((m_nTime / 3600) <10)
|
---|
| 78 | hour = "0"+ m_nTime / 3600;
|
---|
| 79 | else
|
---|
| 80 | hour = m_nTime / 3600 + "";
|
---|
| 81 |
|
---|
| 82 | m_strStartTime = hour + ":" + min + ":" + sec;
|
---|
| 83 | }
|
---|
| 84 | else
|
---|
| 85 | {
|
---|
| 86 | m_nStartSeconds = 0;
|
---|
| 87 | m_nStartMinutes = 0;
|
---|
| 88 | m_nStartHours = 0;
|
---|
| 89 | m_strStartTime = "00:00";
|
---|
| 90 | m_nTime = 0;
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | m_lTime = new JLabel(m_strStartTime);
|
---|
| 94 | add(m_lTime);
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | /*****************************************************************
|
---|
| 98 | * Method name: GameTimeServer()
|
---|
| 99 | * Goal: default Constructor.
|
---|
| 100 | * Description: Initialize the class variables.
|
---|
| 101 | * Input: None.
|
---|
| 102 | * Output: None.
|
---|
| 103 | ****************************************************************/
|
---|
| 104 | public GameTimeServer()
|
---|
| 105 | {
|
---|
| 106 | m_bCountUp = true;
|
---|
| 107 | m_bRun = false;
|
---|
| 108 | m_nTime = 0;
|
---|
| 109 | m_strStartTime = "00:00:00";
|
---|
| 110 |
|
---|
| 111 | m_lTime = new JLabel(m_strStartTime);
|
---|
| 112 | add(m_lTime);
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 | /*****************************************************************
|
---|
| 116 | * Method name: run()
|
---|
| 117 | * Goal: run the stop-watch.
|
---|
| 118 | * Description: Increasing/decreasing the time every second while the
|
---|
| 119 | * game is in progress.
|
---|
| 120 | * Input: None.
|
---|
| 121 | * Output: None.
|
---|
| 122 | ****************************************************************/
|
---|
| 123 | public void run()
|
---|
| 124 | {
|
---|
| 125 | while(m_bRun)
|
---|
| 126 | {
|
---|
| 127 | String sec, min, hour;
|
---|
| 128 |
|
---|
| 129 | try{
|
---|
| 130 | Thread.sleep(1000);
|
---|
| 131 | if (m_bCountUp)
|
---|
| 132 | m_nTime++; // increasing the time each 1 sec.
|
---|
| 133 | else
|
---|
| 134 | m_nTime--; // decreasing the time each 1 sec.
|
---|
| 135 | } catch(Exception e)
|
---|
| 136 | {
|
---|
| 137 | System.out.println("ERROR----" + e.getMessage() + " [GameTimeServer::run(135)]");
|
---|
| 138 | System.err.println("ERROR----" + e.getMessage() + " [GameTimeServer::run(135)]");
|
---|
| 139 | }
|
---|
| 140 |
|
---|
| 141 | // initializing the seconds and minutes variables
|
---|
| 142 | if ((m_nTime % 60) <10)
|
---|
| 143 | sec = "0"+ m_nTime % 60;
|
---|
| 144 | else
|
---|
| 145 | sec = m_nTime % 60 + "";
|
---|
| 146 |
|
---|
| 147 | if (( (m_nTime/60) % 60) <10)
|
---|
| 148 | min = "0"+ (m_nTime/60) % 60;
|
---|
| 149 | else
|
---|
| 150 | min = (m_nTime/60) % 60 + "";
|
---|
| 151 |
|
---|
| 152 | if ((m_nTime / 3600) <10)
|
---|
| 153 | hour = "0"+ m_nTime / 3600;
|
---|
| 154 | else
|
---|
| 155 | hour = m_nTime / 3600 + "";
|
---|
| 156 |
|
---|
| 157 | String t = hour + ":" + min + ":" + sec;
|
---|
| 158 | m_lTime.setText(t);
|
---|
| 159 |
|
---|
| 160 | if (m_nTime == 0)
|
---|
| 161 | stopRunning(); //stop the timer when it reaches 0.
|
---|
| 162 | }
|
---|
| 163 | }
|
---|
| 164 |
|
---|
| 165 | /*****************************************************************
|
---|
| 166 | * Method name: stopRunning()
|
---|
| 167 | * Goal: Stop the stop-watch. If the stop watch is of type end-turn -
|
---|
| 168 | * we send an endTurn message to the client program. If the stop watch
|
---|
| 169 | * is of type end-negotiation - we send an endNegotiation message to the
|
---|
| 170 | * client program, createFrom the log file and remove the client from the
|
---|
| 171 | * clients vector at the server.
|
---|
| 172 | * Input: None.
|
---|
| 173 | * Output: None.
|
---|
| 174 | ****************************************************************/
|
---|
| 175 | public void stopRunning()
|
---|
| 176 | {
|
---|
| 177 | m_bRun = false;
|
---|
| 178 |
|
---|
| 179 | int nCurrentTurn = m_agent.getCurrentTurn();
|
---|
| 180 |
|
---|
| 181 | try
|
---|
| 182 | {
|
---|
| 183 | Socket socket = m_agent.getSocket();
|
---|
| 184 |
|
---|
| 185 | PrintWriter out=new PrintWriter(socket.getOutputStream(),true);
|
---|
| 186 | if(m_bIsTurn)
|
---|
| 187 | {
|
---|
| 188 | out.println("type endTurn");
|
---|
| 189 | System.out.println("COMM-------" + "[Agent " + m_agent.getId() + "] " + "->type endTurn " + nCurrentTurn);
|
---|
| 190 | System.err.println("COMM-------" + "[Agent " + m_agent.getId() + "] " + "->type endTurn " + nCurrentTurn);
|
---|
| 191 | }
|
---|
| 192 | else
|
---|
| 193 | {
|
---|
| 194 | /*DT: m_st.buildEndNegMessageForAgent(m_agent,"time");
|
---|
| 195 |
|
---|
| 196 | /////make log file///////
|
---|
| 197 | m_st.makeLog(m_agent,false);
|
---|
| 198 | */
|
---|
| 199 | if(m_agent.getSide().equals("Mediator"))
|
---|
| 200 | {
|
---|
| 201 | try{
|
---|
| 202 | Thread.sleep(2000);
|
---|
| 203 | }
|
---|
| 204 | catch(Exception e){}
|
---|
| 205 | }
|
---|
| 206 |
|
---|
| 207 | //m_server.removeAgentAt(m_server.getAgentIndex(m_agent));
|
---|
| 208 | /*DT: m_server.removeAgentById(m_agent);
|
---|
| 209 |
|
---|
| 210 | if (m_st != null)
|
---|
| 211 | m_st.setShouldStop(true);*/
|
---|
| 212 | }
|
---|
| 213 | }
|
---|
| 214 | catch(IOException e)
|
---|
| 215 | {
|
---|
| 216 | System.out.println("ERROR----" + "[Agent " + m_agent.getId() + "] " + "Can't write to socket: " + e.getMessage() + " [GameTimeServer::stopRunning(208)]");
|
---|
| 217 | System.err.println("ERROR----" + "[Agent " + m_agent.getId() + "] " + "Can't write to socket: " + e.getMessage() + " [GameTimeServer::stopRunning(208)]");
|
---|
| 218 | //System.exit(1);
|
---|
| 219 | }
|
---|
| 220 |
|
---|
| 221 | if((m_bIsTurn)&&(nCurrentTurn<m_nMaxTurn-1))
|
---|
| 222 | {
|
---|
| 223 | m_agent.incrementCurrentTurn();
|
---|
| 224 |
|
---|
| 225 | newGame(); //restart timer since the negotiation is not over yet.
|
---|
| 226 | m_bRun = true;
|
---|
| 227 | }
|
---|
| 228 | else if((m_bIsTurn)&&(nCurrentTurn==m_nMaxTurn-1))
|
---|
| 229 | {
|
---|
| 230 | m_agent.incrementCurrentTurn();
|
---|
| 231 | }
|
---|
| 232 | }
|
---|
| 233 |
|
---|
| 234 | /*****************************************************************
|
---|
| 235 | * Method name: newGame()
|
---|
| 236 | * Goal: Start a new stop-watch.
|
---|
| 237 | * Input: None.
|
---|
| 238 | * Output: None.
|
---|
| 239 | ****************************************************************/
|
---|
| 240 | public void newGame()
|
---|
| 241 | {
|
---|
| 242 | m_lTime.setText(m_strStartTime);
|
---|
| 243 | m_bRun = true;
|
---|
| 244 | m_nTime = m_nStartHours*3600 + m_nStartMinutes * 60 + m_nStartSeconds;
|
---|
| 245 | }
|
---|
| 246 |
|
---|
| 247 | /*****************************************************************
|
---|
| 248 | * Method name: getTimeLabel()
|
---|
| 249 | * Goal: Return the label with the time.
|
---|
| 250 | * Input: None.
|
---|
| 251 | * Output: Label lTime.
|
---|
| 252 | ****************************************************************/
|
---|
| 253 | public JLabel getTimeLabel()
|
---|
| 254 | {
|
---|
| 255 | return m_lTime;
|
---|
| 256 | }
|
---|
| 257 |
|
---|
| 258 | /*****************************************************************
|
---|
| 259 | * Method name: setRunMethod()
|
---|
| 260 | * Goal: Setting the timer to count up or down.
|
---|
| 261 | * Input: A boolean (True=up, false=down).
|
---|
| 262 | * Output: None.
|
---|
| 263 | ****************************************************************/
|
---|
| 264 | public void setRunMethod(boolean bCountUp)
|
---|
| 265 | {
|
---|
| 266 | m_bCountUp = bCountUp;
|
---|
| 267 | }
|
---|
| 268 |
|
---|
| 269 | /*****************************************************************
|
---|
| 270 | * Method name: setRun()
|
---|
| 271 | * Goal: Setting the timer to run or stop.
|
---|
| 272 | * Input: A boolean (True=run, false=stop).
|
---|
| 273 | * Output: None.
|
---|
| 274 | ****************************************************************/
|
---|
| 275 | public void setRun(boolean bRun)
|
---|
| 276 | {
|
---|
| 277 | m_bRun = bRun;
|
---|
| 278 | }
|
---|
| 279 |
|
---|
| 280 | } // end class - GameTime
|
---|