summaryrefslogtreecommitdiffstats
path: root/src/jrummikub/server/DedicatedServer.java
blob: ae071f050c19543e6909f85839c7b1229d756c9f (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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package jrummikub.server;

import java.net.BindException;
import java.net.InetAddress;

import org.apache.vysper.mina.TCPEndpoint;
import org.apache.vysper.storage.OpenStorageProviderRegistry;
import org.apache.vysper.xmpp.addressing.Entity;
import org.apache.vysper.xmpp.authorization.UserAuthorization;
import org.apache.vysper.xmpp.modules.extension.xep0045_muc.MUCModule;
import org.apache.vysper.xmpp.modules.extension.xep0045_muc.storage.InMemoryOccupantStorageProvider;
import org.apache.vysper.xmpp.modules.extension.xep0045_muc.storage.InMemoryRoomStorageProvider;
import org.apache.vysper.xmpp.modules.roster.persistence.MemoryRosterManager;
import org.apache.vysper.xmpp.server.XMPPServer;

/**
 * Implements a simple XMPP server with a global server password
 */
public class DedicatedServer {
	private volatile String serverPassword;
	private String hostName;

	/**
	 * Creates a new dedicated server with the specified password
	 * 
	 * @param serverPassword
	 *            the password
	 */
	public DedicatedServer(String serverPassword) {
		this.serverPassword = serverPassword;
		try {
			InetAddress addr = InetAddress.getLocalHost();
			hostName = addr.getCanonicalHostName();
		} catch (Exception e) {
			hostName = "localhost";
		}

	}

	/**
	 * Change the current server password
	 * 
	 * @param password
	 *            the new server password
	 */
	public void setServerPassword(String password) {
		serverPassword = password;
	}

	/**
	 * Get the current server password
	 * 
	 * @return the current server password
	 */
	public String getServerPassword() {
		return serverPassword;
	}

	/**
	 * Getter for host name
	 * 
	 * @return host name
	 */
	public String getHostName() {
		return hostName;
	}

	/**
	 * Result of server startup attempt
	 */
	public enum ServerStatus {
		/** Server successfully started */
		STARTED,
		/** Server already running on this machine */
		ALREADY_RUNNING,
		/** Other error */
		ERROR
	}

	/**
	 * Start the server
	 * 
	 * @return Whether we could start the server
	 */
	public ServerStatus start() {
		try {
			XMPPServer server = new XMPPServer(hostName);

			OpenStorageProviderRegistry providerRegistry = new OpenStorageProviderRegistry();
			providerRegistry.add(new ServerPasswordAuthorization());
			providerRegistry.add(new MemoryRosterManager());
			providerRegistry.add(new InMemoryRoomStorageProvider());
			providerRegistry.add(new InMemoryOccupantStorageProvider());

			server.setStorageProviderRegistry(providerRegistry);
			server.addEndpoint(new TCPEndpoint());

			server.setTLSCertificateInfo(
					getClass().getResource(
							"/jrummikub/resource/bogus_mina_tls.cert")
							.openStream(), "boguspw");

			server.start();
			MUCModule muc = new MUCModule("play");
			server.addModule(muc);
			return ServerStatus.STARTED;
		} catch (BindException e) {
			return ServerStatus.ALREADY_RUNNING;
		} catch (Exception e) {
			e.printStackTrace();
			return ServerStatus.ERROR;
		}

	}

	/**
	 * Allow authorization using a single password for all users
	 */
	public class ServerPasswordAuthorization implements UserAuthorization {
		@Override
		public boolean verifyCredentials(Entity entity, String password,
				Object credentials) {
			return password.equals(serverPassword);
		}

		@Override
		public boolean verifyCredentials(String entity, String password,
				Object credentials) {
			return password.equals(serverPassword);
		}
	}

	/**
	 * Main for a simple command line dedicated server
	 * 
	 * @param args
	 *            first argument specifies the server password, it is
	 *            "jrummikub" when none is specified
	 */
	public static void main(String[] args) {
		DedicatedServer server = new DedicatedServer(args.length >= 1 ? args[0]
				: "jrummikub");
		System.out.println("Server hostname is " + server.getHostName());
		try {
			switch (server.start()) {
			case ALREADY_RUNNING:
				System.out.println("Server already running");
				// fall through
			case ERROR:
				System.exit(1);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}