From 6a761c728caa99a115fcf3eb379f1ef46ab673ee Mon Sep 17 00:00:00 2001 From: Andrew Date: Tue, 21 Jan 2025 10:13:14 +0000 Subject: [PATCH] [CT414]: Add RMI sample code --- .../materials/code/01-rmisample/.DS_Store | Bin 0 -> 6148 bytes .../code/01-rmisample/CityClient.java | 15 +++ .../code/01-rmisample/CityServer.java | 8 ++ .../code/01-rmisample/CityServerImpl.java | 54 ++++++++ .../code/01-rmisample/start-client.sh | 1 + .../code/01-rmisample/start-registry.sh | 1 + .../code/01-rmisample/start-server.sh | 1 + .../materials/code/02-rmicompute/.DS_Store | Bin 0 -> 12292 bytes .../code/02-rmicompute/client/.DS_Store | Bin 0 -> 6148 bytes .../code/02-rmicompute/client/ComputePi.java | 65 +++++++++ .../code/02-rmicompute/client/Pi.java | 126 ++++++++++++++++++ .../code/02-rmicompute/compile-code.sh | 3 + .../materials/code/02-rmicompute/compute.jar | Bin 0 -> 936 bytes .../code/02-rmicompute/compute/.DS_Store | Bin 0 -> 6148 bytes .../code/02-rmicompute/compute/Compute.java | 39 ++++++ .../code/02-rmicompute/compute/Task.java | 36 +++++ .../02-rmicompute/engine/ComputeEngine.java | 76 +++++++++++ .../code/02-rmicompute/start-client.sh | 1 + .../code/02-rmicompute/start-registry.sh | 1 + .../code/02-rmicompute/start-server.sh | 1 + 20 files changed, 428 insertions(+) create mode 100644 year4/semester2/CT414/materials/code/01-rmisample/.DS_Store create mode 100644 year4/semester2/CT414/materials/code/01-rmisample/CityClient.java create mode 100644 year4/semester2/CT414/materials/code/01-rmisample/CityServer.java create mode 100644 year4/semester2/CT414/materials/code/01-rmisample/CityServerImpl.java create mode 100755 year4/semester2/CT414/materials/code/01-rmisample/start-client.sh create mode 100755 year4/semester2/CT414/materials/code/01-rmisample/start-registry.sh create mode 100755 year4/semester2/CT414/materials/code/01-rmisample/start-server.sh create mode 100644 year4/semester2/CT414/materials/code/02-rmicompute/.DS_Store create mode 100644 year4/semester2/CT414/materials/code/02-rmicompute/client/.DS_Store create mode 100644 year4/semester2/CT414/materials/code/02-rmicompute/client/ComputePi.java create mode 100644 year4/semester2/CT414/materials/code/02-rmicompute/client/Pi.java create mode 100755 year4/semester2/CT414/materials/code/02-rmicompute/compile-code.sh create mode 100644 year4/semester2/CT414/materials/code/02-rmicompute/compute.jar create mode 100644 year4/semester2/CT414/materials/code/02-rmicompute/compute/.DS_Store create mode 100644 year4/semester2/CT414/materials/code/02-rmicompute/compute/Compute.java create mode 100644 year4/semester2/CT414/materials/code/02-rmicompute/compute/Task.java create mode 100644 year4/semester2/CT414/materials/code/02-rmicompute/engine/ComputeEngine.java create mode 100755 year4/semester2/CT414/materials/code/02-rmicompute/start-client.sh create mode 100755 year4/semester2/CT414/materials/code/02-rmicompute/start-registry.sh create mode 100755 year4/semester2/CT414/materials/code/02-rmicompute/start-server.sh 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 0000000000000000000000000000000000000000..5008ddfcf53c02e82d7eee2e57c38e5672ef89f6 GIT binary patch literal 6148 zcmeH~Jr2S!425mzP>H1@V-^m;4Wg<&0T*E43hX&L&p$$qDprKhvt+--jT7}7np#A3 zem<@ulZcFPQ@L2!n>{z**++&mCkOWA81W14cNZlEfg7;MkzE(HCqgga^y>{tEnwC%0;vJ&^%eQ zLs35+`xjp>T0oDpRe+`iB*y?}uZjt<%S7MonjG$JXkMecN{0nX2t~&bhm8?k3b$ zMjU(Z%)RH{^SJk(Z-3wUe&6pd0ANcZTm?`H01EBA)aKCPW`KZ7W!lEu`JS=Oq8g-K zqn*yF7=mF)Kpgb2nbAA5!bMeld=~9FFaWXi@dJ@iEEWO7j8|4Zi>_H&v$J!Q97R?3 zbq&VGy5gC)b7~w5I2Ld$;8?)1fMbETVF4a{X;W`2OT(t=k1)ri;d2CI>74!R@r2gCuK=3w4oeIr5uV@ih*)C)d#G8O;&kQmU1|t zcrx9`dNNuW3W}#weL!>vY?7sObS&U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oQb~H?)g?gKEy2ip7wO*V}de`e9soo)|u>~XiqDB=3sC{-#$i( zRkHteiXv1js^|Jjd)SD_QjI%#qh>@$jX3F3qFVz+d4FN_gj`2y+>hQ^H$^>quJJfKAvz zM7fiw%MK#SL88i>wnTAMh%1~fpTaxvPNFNPNm}7#`OA17AIA$Musns&;3WPUU&5F1 z6?_fBM3+f_pxDdLI^TN>cW{ICzTo@>1jE#_hPk} zOM%*KdAU?A8s}2qBcujuJqM1F5d9Rs`Y9ER~faei3Q$5 zPvd%3)qvcb$s4u$+NJPg$10KRnUH!Oz70>pk14Ht0e(vu{TmVNTtcUhgcvUTRbVA0 za}5?goADalLh_%KV)}3|i7{M?F-QW82q}h(fa64}KZG9^V$8=Wx%(u3io}@v@eFH1@V-^m;4Wg<&0T*E43hX&L&p$$qDprKhvt+--jT7}7np#A3 zem<@ulZcFPQ@L2!n>{z**++&mCkOWA81W14cNZlEfg7;MkzE(HCqgga^y>{tEnwC%0;vJ&^%eQ zLs35+`xjp>T0 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 0000000000000000000000000000000000000000..22a4a7218f0da718b16c9eadabfec35950d9ce66 GIT binary patch literal 936 zcmWIWW@h1HVBlb2IG&~-#ef7j8CV#6T|*poJ^kGD|D9rB2mmS-Vc_84z)&gz)CO1T z>*(j{<{BKL=j-;__snS@Z(Y5MyxzK6=gyqp9At3C_`%a6JuhD!Pv48BtF{CgFm=6< z)XMlFuB!HYiI&B4anUE@s?!)mvp*GmEMf%O$yUxKoVEQczLtJ@sFW!dqB%oV<@dR5ZRd(=NYJY2n_f?l0e@ZYiAhdTp57 zf-{Tx%{ZCUmma>6(X`e->f|Z= zt~lT{b+^)H(<_pZ83uWF0k8Q$p&`8d-p$iMzZx^*3k@Np&<({yQ4$lnP^9_5s=iMg>gt~s*F8B5kMQrglk1gu^`J~CV)~Ha%h86 s7y_&ZGO?yNbR&@c28tF0*b9t8m=TCb3h-uS1F2yFLI$9UNlYLf0DzPqk^lez literal 0 HcmV?d00001 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 0000000000000000000000000000000000000000..5008ddfcf53c02e82d7eee2e57c38e5672ef89f6 GIT binary patch literal 6148 zcmeH~Jr2S!425mzP>H1@V-^m;4Wg<&0T*E43hX&L&p$$qDprKhvt+--jT7}7np#A3 zem<@ulZcFPQ@L2!n>{z**++&mCkOWA81W14cNZlEfg7;MkzE(HCqgga^y>{tEnwC%0;vJ&^%eQ zLs35+`xjp>T0 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