diff --git a/year4/semester2/CT414/materials/code/01-rmisample/.DS_Store b/year4/semester2/CT414/materials/code/01-rmisample/.DS_Store new file mode 100644 index 00000000..5008ddfc Binary files /dev/null and b/year4/semester2/CT414/materials/code/01-rmisample/.DS_Store differ diff --git a/year4/semester2/CT414/materials/code/01-rmisample/CityClient.java b/year4/semester2/CT414/materials/code/01-rmisample/CityClient.java new file mode 100644 index 00000000..ef34f6a2 --- /dev/null +++ b/year4/semester2/CT414/materials/code/01-rmisample/CityClient.java @@ -0,0 +1,15 @@ +import java.rmi.*; + +public class CityClient +{ + public static void main (String args[]) + { + try { + String target = (args.length == 0) ? "Ireland" : args[0]; + CityServer cities = (CityServer) Naming.lookup("//localhost/Capitals"); + String capital = cities.getCapital(target); + System.out.println(capital); + } + catch (Exception e) {} + } + } diff --git a/year4/semester2/CT414/materials/code/01-rmisample/CityServer.java b/year4/semester2/CT414/materials/code/01-rmisample/CityServer.java new file mode 100644 index 00000000..9805db58 --- /dev/null +++ b/year4/semester2/CT414/materials/code/01-rmisample/CityServer.java @@ -0,0 +1,8 @@ +// Remote Object has a single method that is passed +// the name of a country and returns the capital city. +import java.rmi.*; + +public interface CityServer extends Remote +{ + String getCapital(String Country) throws RemoteException; +} diff --git a/year4/semester2/CT414/materials/code/01-rmisample/CityServerImpl.java b/year4/semester2/CT414/materials/code/01-rmisample/CityServerImpl.java new file mode 100644 index 00000000..fb8a8d36 --- /dev/null +++ b/year4/semester2/CT414/materials/code/01-rmisample/CityServerImpl.java @@ -0,0 +1,54 @@ +import java.rmi.*; +import java.rmi.server.*; +import java.rmi.registry.*; + +public class CityServerImpl implements CityServer +{ + // constructor is required in RMI + CityServerImpl() throws RemoteException + { + super(); // call the parent constructor + } + + // Remote method we are implementing! + public String getCapital(String country) throws RemoteException + { + System.out.println("Sending return string now - country requested: " + country); + if (country.toLowerCase().compareTo("usa") == 0) + return "Washington"; + else if (country.toLowerCase().compareTo("ireland") == 0) + return "Dublin"; + else if (country.toLowerCase().compareTo("france") == 0) + return "Paris"; + return "Don't know that one!"; + } + + // main is required because the server is standalone + public static void main(String args[]) + { + try + { + // First reset our Security manager - deprecated so no longer used + //if (System.getSecurityManager() == null) { + // System.setSecurityManager(new SecurityManager()); + // System.out.println("Security manager set"); + // } + + // Create an instance of the local object + CityServer cityServer = new CityServerImpl(); + System.out.println("Instance of City Server created"); + CityServer stub = (CityServer) UnicastRemoteObject.exportObject(cityServer, 0); + + // Put the server object into the Registry + // Naming.rebind("Capitals", stub); + Registry registry = LocateRegistry.getRegistry(); + registry.rebind("Capitals", stub); + System.out.println("Name rebind completed"); + System.out.println("Server ready for requests!"); + } + catch(Exception exc) + { + System.out.println("Error in main - " + exc.toString()); + } + } +} diff --git a/year4/semester2/CT414/materials/code/01-rmisample/start-client.sh b/year4/semester2/CT414/materials/code/01-rmisample/start-client.sh new file mode 100755 index 00000000..74e44e21 --- /dev/null +++ b/year4/semester2/CT414/materials/code/01-rmisample/start-client.sh @@ -0,0 +1 @@ +java -cp /Users/des/Documents/CT414-Samples/rmisample -Djava.rmi.server.codebase=file:/Users/des/Documents/CT414-Samples/rmisample CityClient diff --git a/year4/semester2/CT414/materials/code/01-rmisample/start-registry.sh b/year4/semester2/CT414/materials/code/01-rmisample/start-registry.sh new file mode 100755 index 00000000..c7703065 --- /dev/null +++ b/year4/semester2/CT414/materials/code/01-rmisample/start-registry.sh @@ -0,0 +1 @@ +rmiregistry -J-Djava.rmi.server.codebase=file:/Users/des/Documents/CT414-Samples/rmisample/ diff --git a/year4/semester2/CT414/materials/code/01-rmisample/start-server.sh b/year4/semester2/CT414/materials/code/01-rmisample/start-server.sh new file mode 100755 index 00000000..d681d112 --- /dev/null +++ b/year4/semester2/CT414/materials/code/01-rmisample/start-server.sh @@ -0,0 +1 @@ +java -cp /Users/des/Documents/CT414-Samples/rmisample -Djava.rmi.server.codebase=file:/Users/des/Documents/CT414-Samples/rmisample/ CityServerImpl diff --git a/year4/semester2/CT414/materials/code/02-rmicompute/.DS_Store b/year4/semester2/CT414/materials/code/02-rmicompute/.DS_Store new file mode 100644 index 00000000..da61452c Binary files /dev/null and b/year4/semester2/CT414/materials/code/02-rmicompute/.DS_Store differ diff --git a/year4/semester2/CT414/materials/code/02-rmicompute/client/.DS_Store b/year4/semester2/CT414/materials/code/02-rmicompute/client/.DS_Store new file mode 100644 index 00000000..5008ddfc Binary files /dev/null and b/year4/semester2/CT414/materials/code/02-rmicompute/client/.DS_Store differ diff --git a/year4/semester2/CT414/materials/code/02-rmicompute/client/ComputePi.java b/year4/semester2/CT414/materials/code/02-rmicompute/client/ComputePi.java new file mode 100644 index 00000000..53c9257c --- /dev/null +++ b/year4/semester2/CT414/materials/code/02-rmicompute/client/ComputePi.java @@ -0,0 +1,65 @@ +/* + * Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * - Neither the name of Oracle or the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package client; + +import java.rmi.registry.LocateRegistry; +import java.rmi.registry.Registry; +import java.math.BigDecimal; +import compute.Compute; + +public class ComputePi { + public static void main(String args[]) { + int registryport = 20345; + + if (args.length > 0) + registryport = Integer.parseInt(args[0]); + + System.out.println("RMIRegistry port = " + registryport); + + // SecurityManager is deprecated so no longer used in this application + // if (System.getSecurityManager() == null) { + // System.setSecurityManager(new SecurityManager()); + // } + + try { + String name = "Compute"; + Registry registry = LocateRegistry.getRegistry(registryport); + Compute comp = (Compute) registry.lookup(name); + Pi task = new Pi(Integer.parseInt(args[1])); + BigDecimal pi = comp.executeTask(task); + System.out.println(pi); + } catch (Exception e) { + System.err.println("ComputePi exception:"); + e.printStackTrace(); + } + } +} diff --git a/year4/semester2/CT414/materials/code/02-rmicompute/client/Pi.java b/year4/semester2/CT414/materials/code/02-rmicompute/client/Pi.java new file mode 100644 index 00000000..73017af1 --- /dev/null +++ b/year4/semester2/CT414/materials/code/02-rmicompute/client/Pi.java @@ -0,0 +1,126 @@ +/* + * Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * - Neither the name of Oracle or the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package client; + +import compute.Task; +import java.io.Serializable; +import java.math.BigDecimal; + +public class Pi implements Task, Serializable { + + private static final long serialVersionUID = 227L; + + /** constants used in pi computation */ + private static final BigDecimal FOUR = + BigDecimal.valueOf(4); + + /** rounding mode to use during pi computation */ + private static final int roundingMode = + BigDecimal.ROUND_HALF_EVEN; + + /** digits of precision after the decimal point */ + private final int digits; + + /** + * Construct a task to calculate pi to the specified + * precision. + */ + public Pi(int digits) { + this.digits = digits; + } + + /** + * Calculate pi. + */ + public BigDecimal execute() { + return computePi(digits); + } + + /** + * Compute the value of pi to the specified number of + * digits after the decimal point. The value is + * computed using Machin's formula: + * + * pi/4 = 4*arctan(1/5) - arctan(1/239) + * + * and a power series expansion of arctan(x) to + * sufficient precision. + */ + public static BigDecimal computePi(int digits) { + int scale = digits + 5; + BigDecimal arctan1_5 = arctan(5, scale); + BigDecimal arctan1_239 = arctan(239, scale); + BigDecimal pi = arctan1_5.multiply(FOUR).subtract( + arctan1_239).multiply(FOUR); + return pi.setScale(digits, + BigDecimal.ROUND_HALF_UP); + } + /** + * Compute the value, in radians, of the arctangent of + * the inverse of the supplied integer to the specified + * number of digits after the decimal point. The value + * is computed using the power series expansion for the + * arc tangent: + * + * arctan(x) = x - (x^3)/3 + (x^5)/5 - (x^7)/7 + + * (x^9)/9 ... + */ + public static BigDecimal arctan(int inverseX, + int scale) + { + BigDecimal result, numer, term; + BigDecimal invX = BigDecimal.valueOf(inverseX); + BigDecimal invX2 = + BigDecimal.valueOf(inverseX * inverseX); + + numer = BigDecimal.ONE.divide(invX, + scale, roundingMode); + + result = numer; + int i = 1; + do { + numer = + numer.divide(invX2, scale, roundingMode); + int denom = 2 * i + 1; + term = + numer.divide(BigDecimal.valueOf(denom), + scale, roundingMode); + if ((i % 2) != 0) { + result = result.subtract(term); + } else { + result = result.add(term); + } + i++; + } while (term.compareTo(BigDecimal.ZERO) != 0); + return result; + } +} diff --git a/year4/semester2/CT414/materials/code/02-rmicompute/compile-code.sh b/year4/semester2/CT414/materials/code/02-rmicompute/compile-code.sh new file mode 100755 index 00000000..5c821693 --- /dev/null +++ b/year4/semester2/CT414/materials/code/02-rmicompute/compile-code.sh @@ -0,0 +1,3 @@ +javac engine/*.java +javac compute/*.java +javac client/*.java diff --git a/year4/semester2/CT414/materials/code/02-rmicompute/compute.jar b/year4/semester2/CT414/materials/code/02-rmicompute/compute.jar new file mode 100644 index 00000000..22a4a721 Binary files /dev/null and b/year4/semester2/CT414/materials/code/02-rmicompute/compute.jar differ diff --git a/year4/semester2/CT414/materials/code/02-rmicompute/compute/.DS_Store b/year4/semester2/CT414/materials/code/02-rmicompute/compute/.DS_Store new file mode 100644 index 00000000..5008ddfc Binary files /dev/null and b/year4/semester2/CT414/materials/code/02-rmicompute/compute/.DS_Store differ diff --git a/year4/semester2/CT414/materials/code/02-rmicompute/compute/Compute.java b/year4/semester2/CT414/materials/code/02-rmicompute/compute/Compute.java new file mode 100644 index 00000000..d2914840 --- /dev/null +++ b/year4/semester2/CT414/materials/code/02-rmicompute/compute/Compute.java @@ -0,0 +1,39 @@ +/* + * Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * - Neither the name of Oracle or the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package compute; + +import java.rmi.Remote; +import java.rmi.RemoteException; + +public interface Compute extends Remote { + T executeTask(Task t) throws RemoteException; +} diff --git a/year4/semester2/CT414/materials/code/02-rmicompute/compute/Task.java b/year4/semester2/CT414/materials/code/02-rmicompute/compute/Task.java new file mode 100644 index 00000000..f03ada12 --- /dev/null +++ b/year4/semester2/CT414/materials/code/02-rmicompute/compute/Task.java @@ -0,0 +1,36 @@ +/* + * Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * - Neither the name of Oracle or the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package compute; + +public interface Task { + T execute(); +} diff --git a/year4/semester2/CT414/materials/code/02-rmicompute/engine/ComputeEngine.java b/year4/semester2/CT414/materials/code/02-rmicompute/engine/ComputeEngine.java new file mode 100644 index 00000000..9762bf02 --- /dev/null +++ b/year4/semester2/CT414/materials/code/02-rmicompute/engine/ComputeEngine.java @@ -0,0 +1,76 @@ +/* + * Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * - Neither the name of Oracle or the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package engine; + +import java.rmi.registry.LocateRegistry; +import java.rmi.registry.Registry; +import java.rmi.server.UnicastRemoteObject; +import compute.Compute; +import compute.Task; + +public class ComputeEngine implements Compute { + + public ComputeEngine() { + super(); + } + + public T executeTask(Task t) { + return t.execute(); + } + + public static void main(String[] args) { + int registryport = 20345; + + if (args.length > 0) + registryport = Integer.parseInt(args[0]); + + System.out.println("RMIRegistry port = " + registryport); + + // SecurityManager is deprecated so no longer used in this application + // if (System.getSecurityManager() == null) { + // System.setSecurityManager(new SecurityManager()); + // } + + try { + String name = "Compute"; + Compute engine = new ComputeEngine(); + Compute stub = + (Compute) UnicastRemoteObject.exportObject(engine, 0); + Registry registry = LocateRegistry.getRegistry(registryport); + registry.rebind(name, stub); + System.out.println("ComputeEngine bound"); + } catch (Exception e) { + System.err.println("ComputeEngine exception:"); + e.printStackTrace(); + } + } +} diff --git a/year4/semester2/CT414/materials/code/02-rmicompute/start-client.sh b/year4/semester2/CT414/materials/code/02-rmicompute/start-client.sh new file mode 100755 index 00000000..e98a36f2 --- /dev/null +++ b/year4/semester2/CT414/materials/code/02-rmicompute/start-client.sh @@ -0,0 +1 @@ +java -cp /Users/des/Documents/CT414-Samples/rmicompute/compute.jar:. client.ComputePi 20345 45 diff --git a/year4/semester2/CT414/materials/code/02-rmicompute/start-registry.sh b/year4/semester2/CT414/materials/code/02-rmicompute/start-registry.sh new file mode 100755 index 00000000..8256a154 --- /dev/null +++ b/year4/semester2/CT414/materials/code/02-rmicompute/start-registry.sh @@ -0,0 +1 @@ +rmiregistry -J-Djava.rmi.server.codebase=file:/Users/des/Documents/CT414-Samples/rmicompute/ 20345 diff --git a/year4/semester2/CT414/materials/code/02-rmicompute/start-server.sh b/year4/semester2/CT414/materials/code/02-rmicompute/start-server.sh new file mode 100755 index 00000000..f5e3c414 --- /dev/null +++ b/year4/semester2/CT414/materials/code/02-rmicompute/start-server.sh @@ -0,0 +1 @@ +java -cp /Users/des/Documents/CT414-Samples/rmicompute/compute.jar:. engine.ComputeEngine 20345