diff options
author | Matthias Schiffer <mschiffer@universe-factory.net> | 2011-06-10 19:06:52 +0200 |
---|---|---|
committer | Matthias Schiffer <mschiffer@universe-factory.net> | 2011-06-10 19:06:52 +0200 |
commit | 182d3b499b306f660d3bf69ddd83d60f47b9e223 (patch) | |
tree | ae3272dcb64c70b14bdd1bf94d3b41b04dedc628 /src/jrummikub | |
parent | 28e5462b08ad8d086b445c08113ab5b2d0151466 (diff) | |
download | JRummikub-182d3b499b306f660d3bf69ddd83d60f47b9e223.tar JRummikub-182d3b499b306f660d3bf69ddd83d60f47b9e223.zip |
Make XMPP login work
git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@404 72836036-5685-4462-b002-a69064685172
Diffstat (limited to 'src/jrummikub')
-rw-r--r-- | src/jrummikub/control/ApplicationControl.java | 28 | ||||
-rw-r--r-- | src/jrummikub/control/network/NetworkControl.java | 87 | ||||
-rw-r--r-- | src/jrummikub/view/impl/LoginPanel.java | 13 |
3 files changed, 114 insertions, 14 deletions
diff --git a/src/jrummikub/control/ApplicationControl.java b/src/jrummikub/control/ApplicationControl.java index b42bc4f..29114a1 100644 --- a/src/jrummikub/control/ApplicationControl.java +++ b/src/jrummikub/control/ApplicationControl.java @@ -1,5 +1,6 @@ package jrummikub.control; +import jrummikub.control.network.NetworkControl; import jrummikub.model.GameSettings; import jrummikub.model.GameState; import jrummikub.model.IRoundState; @@ -17,6 +18,7 @@ import jrummikub.view.IView.BottomPanelType; public class ApplicationControl { private SettingsControl settingsControl; private LoginControl loginControl; + private NetworkControl networkControl; private SaveControl saveControl; private GameControl gameControl; @@ -76,13 +78,12 @@ public class ApplicationControl { } loginControl = new LoginControl(view); - loginControl.getLoginEvent().add( - new IListener1<LoginData>() { - @Override - public void handle(LoginData loginData) { - // TODO Auto-generated method stub - } - }); + loginControl.getLoginEvent().add(new IListener1<LoginData>() { + @Override + public void handle(LoginData loginData) { + createNetworkControl(loginData); + } + }); loginControl.getCancelEvent().add(new IListener() { @Override public void handle() { @@ -109,6 +110,11 @@ public class ApplicationControl { loginControl.abort(); loginControl = null; } + + if (networkControl != null) { + networkControl.abort(); + networkControl = null; + } } /** @@ -146,4 +152,12 @@ public class ApplicationControl { } }); } + + private void createNetworkControl(LoginData loginData) { + networkControl = new NetworkControl(loginData); + + // TODO Add listeners + + networkControl.connect(); + } } diff --git a/src/jrummikub/control/network/NetworkControl.java b/src/jrummikub/control/network/NetworkControl.java new file mode 100644 index 0000000..4494cb6 --- /dev/null +++ b/src/jrummikub/control/network/NetworkControl.java @@ -0,0 +1,87 @@ +package jrummikub.control.network; + +import javax.swing.SwingUtilities; + +import jrummikub.util.Event; +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; + +public class NetworkControl { + private final LoginData loginData; + private Connection connection; + private MultiUserChat muc; + private Thread networkThread; + + private Event connectedEvent = new Event(); + private Event connectionFailedEvent = new Event(); + + public NetworkControl(LoginData loginData) { + this.loginData = loginData; + } + + public void connect() { + new Thread(new ConnectRunner()).run(); + } + + 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); + } + } + } +} diff --git a/src/jrummikub/view/impl/LoginPanel.java b/src/jrummikub/view/impl/LoginPanel.java index 450c392..842bf74 100644 --- a/src/jrummikub/view/impl/LoginPanel.java +++ b/src/jrummikub/view/impl/LoginPanel.java @@ -10,6 +10,7 @@ import javax.swing.Box; import javax.swing.JButton; import javax.swing.JLabel; import javax.swing.JPanel; +import javax.swing.JPasswordField; import javax.swing.JTextField; import javax.swing.border.CompoundBorder; import javax.swing.border.EmptyBorder; @@ -39,10 +40,10 @@ class LoginPanel extends JPanel implements ILoginPanel { c.weightx = 1; c.weighty = 1; - userNameField = addInputRow("Benutzername:"); - serverNameField = addInputRow("Server:"); - passwordField = addInputRow("Passwort:"); - channelNameField = addInputRow("Channel:"); + userNameField = addInputRow("Benutzername:", new JTextField()); + serverNameField = addInputRow("Server:", new JTextField()); + passwordField = addInputRow("Passwort:", new JPasswordField()); + channelNameField = addInputRow("Channel:", new JTextField()); add(Box.createVerticalGlue(), c); c.gridwidth = 1; @@ -85,7 +86,7 @@ class LoginPanel extends JPanel implements ILoginPanel { return cancelEvent; } - private JTextField addInputRow(String label) { + private JTextField addInputRow(String label, JTextField textField) { GridBagConstraints c = new GridBagConstraints(); c.fill = GridBagConstraints.BOTH; c.gridwidth = 1; @@ -94,8 +95,6 @@ class LoginPanel extends JPanel implements ILoginPanel { add(new JLabel(label), c); - JTextField textField = new JTextField(); - c.gridwidth = GridBagConstraints.REMAINDER; add(textField, c); return textField; |