sessionservicermiclient.java

来自「Java mulitplayer strategy game. Adaptati」· Java 代码 · 共 144 行

JAVA
144
字号
/*
 * SessionServiceRMIClient.java
 *
 * Created on 17 pa焏ziernik 2005, 21:22
 *
 * To change this template, choose Tools | Options and locate the template under
 * the Source Creation and Management node. Right-click the template and choose
 * Open. You can then make changes to the template in the Source Editor.
 */

package net.sf.jawp.gf.api.services.rmi;

import java.rmi.Remote;
import java.rmi.RemoteException;
import java.util.Collection;
import net.sf.jawp.gf.api.domain.GameOptions;

import net.sf.jawp.gf.api.domain.GameWorld;
import net.sf.jawp.gf.api.domain.Player;
import net.sf.jawp.gf.api.domain.User;
import net.sf.jawp.gf.api.services.SessionService;
import net.sf.jawp.util.RMIVoidWrapper;
import net.sf.jawp.util.RMIWrapper;

/**
 *
 * @author jarek
 * @param <FACTORY>  factory for creating RMI game client
 * @param <GAMESERVICE> native game service
 */
public final class SessionServiceRMIClient<GAMESERVICE, FACTORY extends GameServiceClientFactory<GAMESERVICE> > implements 
	SessionService<GAMESERVICE>
{
	private final SessionServiceRMI<GAMESERVICE> service;
	
	private final FACTORY gameClientFactory;
	/** Creates a new instance of SessionServiceRMIClient */
	public SessionServiceRMIClient( final SessionServiceRMI<GAMESERVICE> srv, final FACTORY f )
	{
		this.service = srv;
		this.gameClientFactory = f;
	}

	public GameWorld createGame(final String name, final GameOptions options)
	{
		return new RMIWrapper<SessionServiceRMI<GAMESERVICE>, GameWorld>( this.service )
		{
			@Override
			protected GameWorld perform(final SessionServiceRMI<GAMESERVICE> rmi) throws RemoteException
			{
				return rmi.createGame(name, options);
			}
		}.call(); 
	}

	public GAMESERVICE getGameService(final long gameKey, final long playerKey)
	{
		return  new RMIWrapper<SessionServiceRMI<GAMESERVICE>, GAMESERVICE>( this.service )
		{
			@Override
			protected GAMESERVICE perform(final SessionServiceRMI<GAMESERVICE> rmi) throws RemoteException
			{
				final Remote remote =  rmi.getGameServiceRemote(gameKey, playerKey);
				return gameClientFactory.createClient( remote);
			}
		}.call();

	}

	public Collection<GameWorld> getGames()
	{
		return new RMIWrapper<SessionServiceRMI<GAMESERVICE>, Collection<GameWorld> >( this.service )
		{
			@Override
			protected Collection<GameWorld> perform(final SessionServiceRMI<GAMESERVICE> rmi) throws RemoteException
			{
				return rmi.getGames();
			}
		}.call();
	}

	public User getLoggedUser()
	{
		return new RMIWrapper<SessionServiceRMI<GAMESERVICE>, User>( this.service )
		{
			@Override
			protected User perform(final SessionServiceRMI<GAMESERVICE> rmi) throws RemoteException
			{
				return rmi.getLoggedUser();
			}
		}.call();
	}

	public Collection<Player> getUserPlayersForGame(final long gameKey)
	{
		return new RMIWrapper<SessionServiceRMI<GAMESERVICE>, Collection<Player> >( this.service )
		{
			@Override
			protected Collection<Player> perform(final SessionServiceRMI<GAMESERVICE> rmi) throws RemoteException
			{
				return rmi.getUserPlayersForGame(gameKey);
			}
		}.call();
	}

	public Player joinGame(final long gameKey)
	{
		return new RMIWrapper<SessionServiceRMI<GAMESERVICE>, Player >( this.service )
		{
			@Override
			protected Player perform(final SessionServiceRMI<GAMESERVICE> rmi) throws RemoteException
			{
				return rmi.joinGame(gameKey);
			}
		}.call();
	}

	public void quitGame(final long gameKey, final long playerKey)
	{
		new RMIVoidWrapper<SessionServiceRMI<GAMESERVICE> >( this.service )
		{
			@Override
			protected void performVoid(final SessionServiceRMI<GAMESERVICE> rmi) throws RemoteException
			{
				rmi.quitGame(gameKey, playerKey);
			}
		}.call();
	}

	public  Remote getGameServiceRemote(final long gameKey, final long playerKey)
	{
		return new RMIWrapper<SessionServiceRMI<GAMESERVICE>, Remote >( this.service )
		{
			@Override
			protected Remote perform(final SessionServiceRMI<GAMESERVICE> rmi) throws RemoteException
			{
				return rmi.getGameServiceRemote(gameKey , playerKey);
			}
		}.call();
	}
	
	
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?