55import org .slf4j .Logger ;
66import org .slf4j .LoggerFactory ;
77import sc .api .plugins .IGameState ;
8- import sc .api .plugins .host .IRequestResult ;
98import sc .framework .plugins .Player ;
109import sc .networking .INetworkInterface ;
1110import sc .networking .TcpNetwork ;
12- import sc .protocol .helpers .AsyncResultManager ;
13- import sc .protocol .helpers .RequestResult ;
1411import sc .protocol .requests .*;
1512import sc .protocol .responses .*;
1613import sc .shared .GameResult ;
17- import sc .shared .SharedConfiguration ;
1814import sc .shared .SlotDescriptor ;
1915
2016import java .io .IOException ;
3127 */
3228public final class LobbyClient extends XStreamClient implements IPollsHistory {
3329 private static final Logger logger = LoggerFactory .getLogger (LobbyClient .class );
34- private final AsyncResultManager asyncManager = new AsyncResultManager ();
3530 private final List <ILobbyClientListener > listeners = new ArrayList <>();
3631 private final List <IHistoryListener > historyListeners = new ArrayList <>();
3732 private final List <IAdministrativeListener > administrativeListeners = new ArrayList <>();
3833
39- public static final String DEFAULT_HOST = "127.0.0.1" ;
40-
41- public LobbyClient () throws IOException {
42- this (DEFAULT_HOST , SharedConfiguration .DEFAULT_PORT );
43- }
44-
4534 public LobbyClient (String host , int port ) throws IOException {
4635 super (createTcpNetwork (host , port ));
4736 }
@@ -58,8 +47,6 @@ protected final void onObject(ProtocolMessage message) {
5847 return ;
5948 }
6049
61- invokeHandlers (message );
62-
6350 if (message instanceof RoomPacket ) {
6451 RoomPacket packet = (RoomPacket ) message ;
6552 String roomId = packet .getRoomId ();
@@ -137,9 +124,6 @@ private void onGameObserved(String roomId) {
137124 }
138125 }
139126
140- private void invokeHandlers (@ NotNull ProtocolMessage message ) {
141- this .asyncManager .invokeHandlers (message );
142- }
143127
144128 protected void onGamePrepared (GamePreparedResponse response ) {
145129 for (ILobbyClientListener listener : this .listeners ) {
@@ -151,17 +135,6 @@ public void authenticate(String password) {
151135 send (new AuthenticateRequest (password ));
152136 }
153137
154- @ SuppressWarnings ("unchecked" )
155- public RequestResult <GamePreparedResponse > prepareGameAndWait (String gameType ) throws InterruptedException {
156- return blockingRequest (new PrepareGameRequest (gameType ), GamePreparedResponse .class );
157- }
158-
159- @ SuppressWarnings ("unchecked" )
160- public RequestResult <GamePreparedResponse > prepareGameAndWait (
161- PrepareGameRequest request ) throws InterruptedException {
162- return blockingRequest (request , GamePreparedResponse .class );
163- }
164-
165138 public void prepareGame (String gameType ) {
166139 send (new PrepareGameRequest (gameType ));
167140 }
@@ -208,71 +181,14 @@ public void sendMessageToRoom(String roomId, ProtocolMessage o) {
208181 send (new RoomPacket (roomId , o ));
209182 }
210183
211- /**
212- * used in server
213- *
214- * @param reservation reservation ID
215- */
216184 public void joinPreparedGame (String reservation ) {
217185 send (new JoinPreparedRoomRequest (reservation ));
218186 }
219187
220- /**
221- * currently not used in server
222- *
223- * @param gameType GameID
224- */
225188 public void joinRoomRequest (String gameType ) {
226189 send (new JoinRoomRequest (gameType ));
227190 }
228191
229- /**
230- * used in server
231- *
232- * @param request ProtocolMessage which contains the request
233- * @param response Response class to be created
234- * @param handler Handler for the requests
235- */
236- protected void request (ProtocolMessage request , Class <? extends ProtocolMessage > response ,
237- IRequestResult handler ) {
238- this .asyncManager .addHandler (response , handler );
239- send (request );
240- }
241-
242- protected RequestResult blockingRequest (ProtocolMessage request ,
243- Class <? extends ProtocolMessage > response ) throws InterruptedException {
244- // TODO return a proper future here
245- // This is really old async code, so the variable needs to be final but still mutable
246- // IDEA suggested to use an array and we'll stay with that until we reimplement it properly.
247- final RequestResult [] requestResult = {null };
248- final Object beacon = new Object ();
249- synchronized (beacon ) {
250- IRequestResult blockingHandler = new IRequestResult () {
251- @ Override
252- public void handleError (ProtocolErrorMessage e ) {
253- requestResult [0 ] = new RequestResult .Error (e );
254- notifySemaphore ();
255- }
256-
257- @ Override
258- public void accept (ProtocolMessage result ) {
259- requestResult [0 ] = new RequestResult .Success <>(result );
260- notifySemaphore ();
261- }
262-
263- private void notifySemaphore () {
264- synchronized (beacon ) {
265- beacon .notify ();
266- }
267- }
268- };
269- request (request , response , blockingHandler );
270- beacon .wait ();
271- }
272-
273- return requestResult [0 ];
274- }
275-
276192 public void addListener (ILobbyClientListener listener ) {
277193 this .listeners .add (listener );
278194 }
0 commit comments