Netzwerk hat Panel mit laufenden Spielen und einem funktionierenden Abbrechen-Button
git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@406 72836036-5685-4462-b002-a69064685172
This commit is contained in:
parent
9c553786f2
commit
cc4797fd8b
11 changed files with 469 additions and 106 deletions
|
@ -28,7 +28,7 @@ public class ApplicationControl {
|
|||
* Creates a new application control
|
||||
*
|
||||
* @param view
|
||||
* the view to use
|
||||
* the view to use
|
||||
*/
|
||||
public ApplicationControl(final IView view) {
|
||||
this.view = view;
|
||||
|
@ -54,10 +54,11 @@ public class ApplicationControl {
|
|||
new IListener3<GameSettings, GameState, IRoundState>() {
|
||||
|
||||
@Override
|
||||
public void handle(GameSettings settings, GameState gameState,
|
||||
IRoundState roundState) {
|
||||
public void handle(GameSettings settings,
|
||||
GameState gameState, IRoundState roundState) {
|
||||
abortControls();
|
||||
gameControl = new GameControl(settings, saveControl, view);
|
||||
gameControl = new GameControl(settings, saveControl,
|
||||
view);
|
||||
addGameControlListeners(gameControl);
|
||||
gameControl.continueGame(gameState, roundState);
|
||||
|
||||
|
@ -154,10 +155,15 @@ public class ApplicationControl {
|
|||
}
|
||||
|
||||
private void createNetworkControl(LoginData loginData) {
|
||||
networkControl = new NetworkControl(loginData);
|
||||
networkControl = new NetworkControl(loginData, view);
|
||||
|
||||
// TODO Add listeners
|
||||
networkControl.getStopNetworkEvent().add(new IListener() {
|
||||
@Override
|
||||
public void handle() {
|
||||
startApplication();
|
||||
}
|
||||
});
|
||||
|
||||
networkControl.connect();
|
||||
networkControl.startNetwork();
|
||||
}
|
||||
}
|
||||
|
|
114
src/jrummikub/control/network/ConnectionControl.java
Normal file
114
src/jrummikub/control/network/ConnectionControl.java
Normal file
|
@ -0,0 +1,114 @@
|
|||
package jrummikub.control.network;
|
||||
|
||||
import javax.swing.SwingUtilities;
|
||||
|
||||
import jrummikub.util.Event;
|
||||
import jrummikub.util.IEvent;
|
||||
import jrummikub.util.LoginData;
|
||||
|
||||
import org.jivesoftware.smack.Connection;
|
||||
import org.jivesoftware.smack.XMPPConnection;
|
||||
import org.jivesoftware.smack.XMPPException;
|
||||
import org.jivesoftware.smack.packet.XMPPError;
|
||||
import org.jivesoftware.smack.packet.XMPPError.Type;
|
||||
import org.jivesoftware.smackx.muc.MultiUserChat;
|
||||
|
||||
class ConnectionControl {
|
||||
private final LoginData loginData;
|
||||
private volatile Connection connection;
|
||||
private volatile MultiUserChat muc;
|
||||
|
||||
private Event connectedEvent = new Event();
|
||||
private Event connectionFailedEvent = new Event();
|
||||
|
||||
ConnectionControl(LoginData loginData) {
|
||||
this.loginData = loginData;
|
||||
}
|
||||
|
||||
void connect() {
|
||||
new Thread(new ConnectRunner()).start();
|
||||
}
|
||||
|
||||
void disconnect() {
|
||||
connectedEvent = new Event();
|
||||
connectionFailedEvent = new Event();
|
||||
new Thread(new DisconnectRunner()).start();
|
||||
|
||||
}
|
||||
|
||||
IEvent getConnectedEvent() {
|
||||
return connectedEvent;
|
||||
}
|
||||
|
||||
IEvent getConnectionFailedEvent() {
|
||||
return connectionFailedEvent;
|
||||
}
|
||||
|
||||
private static void emitLater(final Event event) {
|
||||
SwingUtilities.invokeLater(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
event.emit();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private class ConnectRunner implements Runnable {
|
||||
@Override
|
||||
public void run() {
|
||||
synchronized (ConnectionControl.this) {
|
||||
connection = new XMPPConnection(loginData.getServerName());
|
||||
try {
|
||||
connection.connect();
|
||||
connection.login(loginData.getUserName(),
|
||||
loginData.getPassword(), "JRummikub");
|
||||
muc = new MultiUserChat(connection,
|
||||
loginData.getChannelName());
|
||||
|
||||
String nickname = loginData.getUserName();
|
||||
// Loop until a unused nickname is found
|
||||
while (true) {
|
||||
try {
|
||||
muc.join(nickname);
|
||||
break; // Join was successful, break the loop
|
||||
} catch (XMPPException e) {
|
||||
XMPPError error = e.getXMPPError();
|
||||
if (error.getType() == Type.CANCEL
|
||||
&& error.getCode() == 409) {
|
||||
// There was a conflict, try again with another
|
||||
// nickname
|
||||
nickname += "_";
|
||||
continue;
|
||||
} else {
|
||||
// An unknown error has occurred, cancel connect
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
emitLater(connectedEvent);
|
||||
} catch (XMPPException e) {
|
||||
connection.disconnect();
|
||||
connection = null;
|
||||
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
|
||||
emitLater(connectionFailedEvent);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private class DisconnectRunner implements Runnable {
|
||||
@Override
|
||||
public void run() {
|
||||
synchronized (ConnectionControl.this) {
|
||||
if (connection != null) {
|
||||
connection.disconnect();
|
||||
connection = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,87 +1,87 @@
|
|||
package jrummikub.control.network;
|
||||
|
||||
import javax.swing.SwingUtilities;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import jrummikub.util.Connection;
|
||||
import jrummikub.util.Event;
|
||||
import jrummikub.util.IEvent;
|
||||
import jrummikub.util.IListener;
|
||||
import jrummikub.util.IListener1;
|
||||
import jrummikub.util.LoginData;
|
||||
|
||||
import org.jivesoftware.smack.Connection;
|
||||
import org.jivesoftware.smack.XMPPConnection;
|
||||
import org.jivesoftware.smack.XMPPException;
|
||||
import org.jivesoftware.smack.packet.XMPPError;
|
||||
import org.jivesoftware.smack.packet.XMPPError.Type;
|
||||
import org.jivesoftware.smackx.muc.MultiUserChat;
|
||||
import jrummikub.view.IGameListPanel;
|
||||
import jrummikub.view.IGameListPanel.GameData;
|
||||
import jrummikub.view.IView;
|
||||
|
||||
public class NetworkControl {
|
||||
private final LoginData loginData;
|
||||
private Connection connection;
|
||||
private MultiUserChat muc;
|
||||
private Thread networkThread;
|
||||
private ConnectionControl connectionControl;
|
||||
private IView view;
|
||||
private List<Connection> connections = new ArrayList<Connection>();
|
||||
private Event stopNetworkEvent = new Event();
|
||||
|
||||
private Event connectedEvent = new Event();
|
||||
private Event connectionFailedEvent = new Event();
|
||||
public NetworkControl(LoginData loginData, final IView view) {
|
||||
this.view = view;
|
||||
connectionControl = new ConnectionControl(loginData);
|
||||
|
||||
public NetworkControl(LoginData loginData) {
|
||||
this.loginData = loginData;
|
||||
connections.add(connectionControl.getConnectedEvent().add(
|
||||
new IListener() {
|
||||
@Override
|
||||
public void handle() {
|
||||
view.showGameListPanel(true);
|
||||
}
|
||||
}));
|
||||
|
||||
connections.add(connectionControl.getConnectionFailedEvent().add(
|
||||
new IListener() {
|
||||
@Override
|
||||
public void handle() {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
}));
|
||||
|
||||
connections.add(view.getGameListPanel().getJoinEvent()
|
||||
.add(new IListener1<IGameListPanel.GameData>() {
|
||||
@Override
|
||||
public void handle(GameData value) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
}));
|
||||
|
||||
connections.add(view.getGameListPanel().getOpenNewGameEvent()
|
||||
.add(new IListener() {
|
||||
@Override
|
||||
public void handle() {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
}));
|
||||
|
||||
connections.add(view.getGameListPanel().getCancelEvent()
|
||||
.add(new IListener() {
|
||||
@Override
|
||||
public void handle() {
|
||||
abort();
|
||||
stopNetworkEvent.emit();
|
||||
}
|
||||
}));
|
||||
}
|
||||
|
||||
public void connect() {
|
||||
new Thread(new ConnectRunner()).run();
|
||||
public void startNetwork() {
|
||||
connectionControl.connect();
|
||||
}
|
||||
|
||||
public void abort() {
|
||||
// TODO Implement this
|
||||
}
|
||||
|
||||
private static void emitLater(final Event event) {
|
||||
SwingUtilities.invokeLater(new Runnable() {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
event.emit();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private class ConnectRunner implements Runnable {
|
||||
@Override
|
||||
public void run() {
|
||||
connection = new XMPPConnection(loginData.getServerName());
|
||||
try {
|
||||
connection.connect();
|
||||
connection.login(loginData.getUserName(), loginData.getPassword(),
|
||||
"JRummikub");
|
||||
muc = new MultiUserChat(connection, loginData.getChannelName());
|
||||
|
||||
String nickname = loginData.getUserName();
|
||||
// Loop until a unused nickname is found
|
||||
while (true) {
|
||||
try {
|
||||
muc.join(nickname);
|
||||
break; // Join was successful, break the loop
|
||||
} catch (XMPPException e) {
|
||||
XMPPError error = e.getXMPPError();
|
||||
if (error.getType() == Type.CANCEL && error.getCode() == 409) {
|
||||
// There was a conflict, try again with another nickname
|
||||
nickname += "_";
|
||||
continue;
|
||||
} else {
|
||||
// An unknown error has occurred, cancel connect
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
emitLater(connectedEvent);
|
||||
} catch (XMPPException e) {
|
||||
connection.disconnect();
|
||||
connection = null;
|
||||
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
|
||||
emitLater(connectionFailedEvent);
|
||||
}
|
||||
for (Connection c : connections) {
|
||||
c.remove();
|
||||
}
|
||||
connectionControl.disconnect();
|
||||
view.showGameListPanel(false);
|
||||
}
|
||||
|
||||
public IEvent getStopNetworkEvent() {
|
||||
return stopNetworkEvent;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Reference in a new issue