summaryrefslogtreecommitdiffstats
path: root/src/jrummikub/server/DedicatedServer.java
blob: 42345a61bd7a79a6b5aeb7fdf9c0c541e40fe0fd (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
package jrummikub.server;

import java.io.IOException;
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;

@SuppressWarnings("deprecation")
public class DedicatedServer {
	String serverPassword;
	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";
		}

	}

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

	public void start() throws Exception {
		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);

	}

	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);
		}
	}

	public static void main(String[] args) {

		DedicatedServer server = new DedicatedServer("password");
		System.out.println("Server hostname is " + server.getHostName());
		try {
			server.start();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}