Commit 5b8ea6b2c6a69c56d51f4e2a34e9b00826aa646f

  • avatar
  • Oskari Leppaaho <oskari.leppaaho @gm…l.com> (Committer)
  • Mon Jul 07 16:49:17 EEST 2014
  • avatar
  • Oskari Leppaaho <oskari.leppaaho @gm…l.com> (Author)
  • Mon Jul 07 16:49:17 EEST 2014
Testi molempiin suuntiin toimivasta RMI-kommunikoinnista
Source/Java/ThesisAIProject/src/allow_all.policy
(3 / 0)
  
1grant {
2 permission java.security.AllPermission;
3};
Source/Java/ThesisAIProject/src/test/PolicyFileLocator.java
(36 / 0)
  
1package test;
2
3import java.io.BufferedWriter;
4import java.io.File;
5import java.io.FileWriter;
6import java.io.IOException;
7import java.io.InputStream;
8
9/**
10 * class to locate our most "secure" policy file
11 *
12 * @author srasul
13 *
14 */
15public class PolicyFileLocator {
16
17 public static final String POLICY_FILE_NAME = "/allow_all.policy";
18
19 public static String getLocationOfPolicyFile() {
20 try {
21 File tempFile = File.createTempFile("rmi-base", ".policy");
22 InputStream is = PolicyFileLocator.class.getResourceAsStream(POLICY_FILE_NAME);
23 BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));
24 int read = 0;
25 while((read = is.read()) != -1) {
26 writer.write(read);
27 }
28 writer.close();
29 tempFile.deleteOnExit();
30 return tempFile.getAbsolutePath();
31 }
32 catch(IOException e) {
33 throw new RuntimeException(e);
34 }
35 }
36}
Source/Java/ThesisAIProject/src/test/RmiStarter.java
(38 / 0)
  
1package test;
2
3/**
4 * class to do some common things for client & server to get RMI working
5 *
6 * @author srasul
7 *
8 */
9public abstract class RmiStarter {
10
11 /**
12 *
13 * @param clazzToAddToServerCodebase
14 * a class that should be in the java.rmi.server.codebase
15 * property.
16 */
17 public RmiStarter(Class clazzToAddToServerCodebase) {
18
19 System.setProperty("java.rmi.server.codebase",
20 clazzToAddToServerCodebase.getProtectionDomain()
21 .getCodeSource().getLocation().toString());
22
23 System.setProperty("java.security.policy",
24 PolicyFileLocator.getLocationOfPolicyFile());
25
26 if (System.getSecurityManager() == null) {
27 System.setSecurityManager(new SecurityManager());
28 }
29
30 doCustomRmiHandling();
31 }
32
33 /**
34 * extend this class and do RMI handling here
35 */
36 public abstract void doCustomRmiHandling();
37
38}
Source/Java/ThesisAIProject/src/test/TestPotentialFunctionProvider.java
(15 / 0)
  
1package test;
2
3import java.rmi.RemoteException;
4
5import thesis.evolution.PotentialFunctionProvider;
6
7public class TestPotentialFunctionProvider implements PotentialFunctionProvider {
8
9 @Override
10 public double evaluateFunction(double distance) throws RemoteException {
11 // TODO Auto-generated method stub
12 return 0;
13 }
14
15}
Source/Java/ThesisAIProject/src/test/TestPotentialFunctionProviderStarter.java
(71 / 0)
  
1package test;
2
3import java.io.BufferedReader;
4import java.io.IOException;
5import java.io.InputStreamReader;
6import java.rmi.registry.LocateRegistry;
7import java.rmi.registry.Registry;
8import java.rmi.server.UnicastRemoteObject;
9
10import thesis.bot.RemoteBotInterface;
11import thesis.evolution.PotentialFunctionProvider;
12
13public class TestPotentialFunctionProviderStarter extends RmiStarter {
14
15 Registry registry;
16
17 public TestPotentialFunctionProviderStarter() {
18 super(PotentialFunctionProvider.class);
19 }
20
21 @Override
22 public void doCustomRmiHandling() {
23 try {
24 PotentialFunctionProvider provider = new TestPotentialFunctionProvider();
25 PotentialFunctionProvider providerStub = (PotentialFunctionProvider)UnicastRemoteObject.exportObject(provider, 0);
26
27 registry = LocateRegistry.getRegistry();
28 registry.rebind(PotentialFunctionProvider.SERVICE_NAME, providerStub);
29 }
30 catch(Exception e ){
31 e.printStackTrace();
32 }
33 }
34
35 public void connectClient(String name) {
36 try {
37 registry.lookup(RemoteBotInterface.SERVICE_NAME + name);
38 } catch (Exception e ) {
39 // TODO Auto-generated catch block
40 e.printStackTrace();
41 }
42 }
43
44 public static void main(String[] args) {
45 TestPotentialFunctionProviderStarter starter = new TestPotentialFunctionProviderStarter();
46
47 BufferedReader buffer = new BufferedReader(new InputStreamReader(System.in));
48
49 System.out.println("Press enter when ready to initialize next client >");
50 try {
51 buffer.readLine();
52 } catch (IOException e) {
53 System.err.println("Something went wrong when waiting for \n"
54 + "the user to press enter on the console\n"
55 + "(for stepping).");
56 e.printStackTrace();
57 }
58 starter.connectClient("1");
59 System.out.println("Press enter when ready to initialize next client >");
60 try {
61 buffer.readLine();
62 } catch (IOException e) {
63 System.err.println("Something went wrong when waiting for \n"
64 + "the user to press enter on the console\n"
65 + "(for stepping).");
66 e.printStackTrace();
67 }
68 starter.connectClient("2");
69
70 }
71}
Source/Java/ThesisAIProject/src/test/TestRemoteBot.java
(17 / 0)
  
1package test;
2
3import java.rmi.RemoteException;
4
5import thesis.bot.RemoteBotInterface;
6import thesis.evolution.PotentialFunctionProvider;
7
8public class TestRemoteBot implements RemoteBotInterface {
9
10 @Override
11 public int getRoundScore(PotentialFunctionProvider problem)
12 throws RemoteException {
13 // TODO Auto-generated method stub
14 return 0;
15 }
16
17}
Source/Java/ThesisAIProject/src/test/TestRemoteBotStarter.java
(43 / 0)
  
1package test;
2
3import java.rmi.registry.LocateRegistry;
4import java.rmi.registry.Registry;
5import java.rmi.server.UnicastRemoteObject;
6
7import thesis.bot.RemoteBotInterface;
8import thesis.evolution.PotentialFunctionProvider;
9
10public class TestRemoteBotStarter extends RmiStarter {
11
12 static String currentName;
13
14 public TestRemoteBotStarter() {
15 super(TestRemoteBot.class);
16 }
17
18 public static void main(String[] args) {
19 currentName = args[0];
20 new TestRemoteBotStarter();
21 }
22
23 @Override
24 public void doCustomRmiHandling() {
25 try {
26 Registry registry = LocateRegistry.getRegistry();
27 RemoteBotInterface remoteBot = new TestRemoteBot();
28 RemoteBotInterface remoteBotStub = (RemoteBotInterface)UnicastRemoteObject.exportObject(remoteBot, 0);
29
30 registry.rebind(RemoteBotInterface.SERVICE_NAME + currentName, remoteBotStub);
31 System.out.println("Bind " + RemoteBotInterface.SERVICE_NAME + currentName);
32
33
34 PotentialFunctionProvider provider = (PotentialFunctionProvider)registry.lookup(PotentialFunctionProvider.SERVICE_NAME);
35 System.out.println("Remote bot evaluating using remote potential: " + provider.evaluateFunction(5));
36 }
37 catch(Exception e) {
38 e.printStackTrace();
39 }
40
41 }
42
43}
Source/Java/ThesisAIProject/src/thesis/bot/RemoteBotInterface.java
(12 / 0)
  
1package thesis.bot;
2
3import java.rmi.Remote;
4import java.rmi.RemoteException;
5
6import thesis.evolution.PotentialFunctionProvider;
7
8public interface RemoteBotInterface extends Remote {
9 public static final String SERVICE_NAME = "BotService";
10
11 public int getRoundScore(PotentialFunctionProvider problem) throws RemoteException;
12}
Source/Java/ThesisAIProject/src/thesis/evolution/PotentialFunctionProvider.java
(10 / 0)
  
1package thesis.evolution;
2
3import java.rmi.Remote;
4import java.rmi.RemoteException;
5
6public interface PotentialFunctionProvider extends Remote {
7 public static final String SERVICE_NAME = "PotentialFunctionService";
8
9 public double evaluateFunction(double distance) throws RemoteException;
10}
Source/Java/ThesisAIProject/src/thesis/evolution/RTSSmallScaleCombatProblem.java
(1 / 1)
  
2929
3030 DoubleData localInput;
3131
32 public synchronized void setup(final EvolutionState state, final Parameter base) {
32 public void setup(final EvolutionState state, final Parameter base) {
3333 super.setup(state, base);
3434
3535 // verify our input is the right class (or subclasses from it)