Skip to content

Commit 55a4df3

Browse files
committed
Move attaching states responsibility to GUIAppstate
Fix the initFakeConnection stuff - that was more than incorrect
1 parent 4476a24 commit 55a4df3

File tree

2 files changed

+37
-22
lines changed

2 files changed

+37
-22
lines changed

blastback_client/src/com/blastback/GameClient.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.blastback;
22

33
import com.blastback.appstates.BulletFactoryAppState;
4+
import com.blastback.appstates.GUIAppState;
45
import com.blastback.appstates.InputManagerAppState;
56
import com.blastback.appstates.MapAppState;
67
import com.blastback.appstates.NetworkAppState;
@@ -28,13 +29,7 @@ public GameClient()
2829
super(new StatsAppState(),
2930
new AudioListenerState(),
3031
new DebugKeysAppState(),
31-
new InputManagerAppState(),
32-
new MapAppState(),
33-
new PlayerAppState(),
34-
new BulletFactoryAppState(),
35-
new TopDownCameraAppState(),
36-
new NetworkAppState(),
37-
new SimulationAppState());
32+
new GUIAppState());
3833
}
3934

4035
public static void main(String[] args)

blastback_client/src/com/blastback/appstates/GUIAppState.java

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,16 @@
1414
import de.lessvoid.nifty.screen.Screen;
1515
import de.lessvoid.nifty.screen.ScreenController;
1616
import java.io.IOException;
17+
import java.net.Socket;
18+
import java.net.UnknownHostException;
1719
import java.util.ArrayList;
1820
import java.util.List;
1921
import java.util.logging.Level;
2022
import java.util.logging.Logger;
2123

2224
public class GUIAppState extends BaseAppState implements ScreenController
2325
{
26+
2427
private Nifty _niftyInstance;
2528
Element _scoreboardElement;
2629
private List<BaseAppState> _gameStates;
@@ -58,15 +61,16 @@ protected void initialize(Application app)
5861
_gameStates.add(new InputManagerAppState());
5962
_gameStates.add(new MapAppState());
6063
_gameStates.add(new PlayerAppState());
64+
_gameStates.add(new BulletFactoryAppState());
6165
_gameStates.add(new TopDownCameraAppState());
6266
}
6367

6468
/**
6569
* Handle join button onClick (only in hud-screen).
6670
*/
67-
public void joinButtonClicked() throws InterruptedException
71+
public void joinButtonClicked()
6872
{
69-
boolean canConnect = initFakeConnection();
73+
boolean canConnect = hostAvailabilityCheck();
7074
if (canConnect)
7175
{
7276
attachGameStates();
@@ -145,35 +149,51 @@ public void updateTimer(long timeLeft)
145149
}
146150

147151
/**
148-
* It is a very bad way of verifying user's ip/port combination. I wan´t
149-
* able to check that while attaching the state. This is the only workaround
150-
* I have found and I am not proud of it.
152+
* It is a way of verifying user's ip/port combination. I wasn´t able to
153+
* check that while attaching the state. This is the only workaround I have
154+
* found and I am not proud of it.
151155
*
152156
* @return value indicating whether it is possible to connect to given port
153157
*/
154-
private boolean initFakeConnection()
158+
public boolean hostAvailabilityCheck()
155159
{
160+
boolean available = true;
161+
Socket s;
156162
try
157163
{
158-
Client _clientInstance = Network.connectToServer(getTextIp(), getTextPort());
159-
_clientInstance.start();
160-
_clientInstance.close();
161-
} catch (IOException ex)
164+
s = new Socket(getTextIp(), getTextPort());
165+
if (s.isConnected())
166+
{
167+
s.close();
168+
}
169+
} catch (UnknownHostException e)
170+
{
171+
// unknown host
172+
available = false;
173+
} catch (IOException e)
162174
{
163-
Log("initFakeConnection failed");
164-
ex.printStackTrace();
165-
return false;
175+
// io exception, service probably not running
176+
available = false;
177+
} catch (NullPointerException e)
178+
{
179+
available = false;
180+
} finally
181+
{
182+
s = null;
166183
}
167-
return true;
184+
185+
return available;
168186
}
169187

170-
private void attachGameStates() throws InterruptedException
188+
private void attachGameStates()
171189
{
172190
_gameStates.add(new NetworkAppState(getTextIp(), getTextPort()));
191+
_gameStates.add(new SimulationAppState());
173192
for (BaseAppState state : _gameStates)
174193
{
175194
_application.getStateManager().attach(state);
176195
}
196+
177197
}
178198

179199
private void detachGameStates()

0 commit comments

Comments
 (0)