summaryrefslogtreecommitdiffstats
path: root/src/jrummikub/control/network/ConnectionControl.java
blob: 97778539d9802f3bc1b3b2a0851f1043e4e2c342 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
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;
				}
			}
		}
	}
}