package jrummikub.server; 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 { 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; } /** * Start the server, this blocks * * @throws Exception * when there is an error during startup */ 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); } /** * 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 { server.start(); } catch (Exception e) { e.printStackTrace(); } } }