source: src/main/java/agents/qoagent2/GameTimeServer.java@ 203

Last change on this file since 203 was 127, checked in by Wouter Pasman, 6 years ago

#41 ROLL BACK of rev.126 . So this version is equal to rev. 125

File size: 8.6 KB
RevLine 
[127]1package agents.qoagent2;
2
3//file name: GameTimeServer.java
4import javax.swing.*;
5import java.io.*;
6import java.net.*;
7
8/*****************************************************************
9 * Class name: GameTimeServer
10 * Goal: Creating the stop-watch using threads.
11 * Input: None.
12 * Output: None.
13 ****************************************************************/
14class GameTimeServer extends JPanel implements Runnable
15{
16 private static final long serialVersionUID = -7439433284667379204L;
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//DT: 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: String msg=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/* DT:
207 //m_server.removeAgentAt(m_server.getAgentIndex(m_agent));
208 m_server.removeAgentById(m_agent);
209
210 if (m_st != null)
211 m_st.setShouldStop(true);
212*/
213 }
214 }
215 catch(IOException e)
216 {
217 System.out.println("ERROR----" + "[Agent " + m_agent.getId() + "] " + "Can't write to socket: " + e.getMessage() + " [GameTimeServer::stopRunning(208)]");
218 System.err.println("ERROR----" + "[Agent " + m_agent.getId() + "] " + "Can't write to socket: " + e.getMessage() + " [GameTimeServer::stopRunning(208)]");
219 //System.exit(1);
220 }
221
222 if((m_bIsTurn)&&(nCurrentTurn<m_nMaxTurn-1))
223 {
224 m_agent.incrementCurrentTurn();
225
226 newGame(); //restart timer since the negotiation is not over yet.
227 m_bRun = true;
228 }
229 else if((m_bIsTurn)&&(nCurrentTurn==m_nMaxTurn-1))
230 {
231 m_agent.incrementCurrentTurn();
232 }
233 }
234
235/*****************************************************************
236 * Method name: newGame()
237 * Goal: Start a new stop-watch.
238 * Input: None.
239 * Output: None.
240 ****************************************************************/
241 public void newGame()
242 {
243 m_lTime.setText(m_strStartTime);
244 m_bRun = true;
245 m_nTime = m_nStartHours*3600 + m_nStartMinutes * 60 + m_nStartSeconds;
246 }
247
248/*****************************************************************
249 * Method name: getTimeLabel()
250 * Goal: Return the label with the time.
251 * Input: None.
252 * Output: Label lTime.
253 ****************************************************************/
254 public JLabel getTimeLabel()
255 {
256 return m_lTime;
257 }
258
259 /*****************************************************************
260 * Method name: setRunMethod()
261 * Goal: Setting the timer to count up or down.
262 * Input: A boolean (True=up, false=down).
263 * Output: None.
264 ****************************************************************/
265 public void setRunMethod(boolean bCountUp)
266 {
267 m_bCountUp = bCountUp;
268 }
269
270 /*****************************************************************
271 * Method name: setRun()
272 * Goal: Setting the timer to run or stop.
273 * Input: A boolean (True=run, false=stop).
274 * Output: None.
275 ****************************************************************/
276 public void setRun(boolean bRun)
277 {
278 m_bRun = bRun;
279 }
280
281} // end class - GameTime
Note: See TracBrowser for help on using the repository browser.