You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

155 lines
3.9 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. package verteiltesysteme.mandelbrot.rmi;
  2. import java.io.IOException;
  3. import java.net.InetAddress;
  4. import java.net.ServerSocket;
  5. import java.net.Socket;
  6. //import java.io.IOException;
  7. //import java.net.InetAddress;
  8. //import java.net.ServerSocket;
  9. //import java.net.Socket;
  10. import java.rmi.registry.LocateRegistry;
  11. import java.rmi.registry.Registry;
  12. import java.rmi.server.RMISocketFactory;
  13. //import java.rmi.server.RMISocketFactory;
  14. import java.rmi.server.UnicastRemoteObject;
  15. class LoopbackSocketFactory extends RMISocketFactory {
  16. public ServerSocket createServerSocket(int port) throws IOException {
  17. return new ServerSocket(port, 5, InetAddress.getByName("0.0.0.0"));
  18. }
  19. public Socket createSocket(String host, int port) throws IOException {
  20. // just call the default client socket factory
  21. return RMISocketFactory.getDefaultSocketFactory()
  22. .createSocket(host, port);
  23. }
  24. }
  25. class Complex {
  26. double r;
  27. double i;
  28. Complex() {
  29. r = 0.0;
  30. i = 0.0;
  31. }
  32. Complex(double r, double i) {
  33. this.r = r;
  34. this.i = i;
  35. }
  36. void mulBy(Complex other) {
  37. double rTmp = this.r * other.r - this.i * other.i;
  38. this.i = this.r * other.i + this.i * other.r;
  39. this.r = rTmp;
  40. }
  41. void add(Complex other) {
  42. this.r += other.r;
  43. this.i += other.i;
  44. }
  45. void sub(Complex other) {
  46. this.r -= other.r;
  47. this.i -= other.i;
  48. }
  49. double absValue() {
  50. return Math.sqrt(this.r * this.r + this.i * this.i);
  51. }
  52. }
  53. public class RMIMandelbrotCalculationsServer implements RMIMandelbrotCalculationsInterface {
  54. public RMIMandelbrotCalculationsServer() {}
  55. public static void main(String args[]) {
  56. try {
  57. //RMISocketFactory.setSocketFactory(new LoopbackSocketFactory());
  58. RMIMandelbrotCalculationsServer obj = new RMIMandelbrotCalculationsServer();
  59. RMIMandelbrotCalculationsInterface stub = (RMIMandelbrotCalculationsInterface) UnicastRemoteObject.exportObject(obj, 0);
  60. // Bind the remote object's stub in the registry
  61. Registry registry = LocateRegistry.getRegistry();
  62. registry.bind("RMIMandelbrotCalculationsInterface", stub);
  63. System.err.println("Server ready");
  64. } catch (Exception e) {
  65. System.err.println("Server exception: " + e.toString());
  66. e.printStackTrace();
  67. }
  68. }
  69. // hier wird das Ergebnis von iterateAll() gespeichert
  70. protected int[] result = null;
  71. // der augenblicklich gewaehlte Ausschnitt
  72. //double ULx = -2, ULy = 2, LRx = 2, LRy = -2;
  73. int iterateOnePoint(Complex c, int maxIterations, double maxModulus) {
  74. Complex z = new Complex(0., 0.);
  75. for (int i = 0; i < maxIterations; i++) {
  76. z.mulBy(z);
  77. z.add(c);
  78. double m = z.absValue();
  79. if (m > maxModulus) {
  80. return i;
  81. }
  82. }
  83. return maxIterations;
  84. }
  85. public void iterateAllPoints(int maxIterations, double nrPointsX, double nrPointsY, double ULx, double ULy, double LRx, double LRy) {
  86. this.result = new int[(int) (nrPointsX * nrPointsY)];
  87. int counter = 0;
  88. double stepX = (LRx - ULx) / nrPointsX;
  89. double stepY = (LRy - ULy) / nrPointsY;
  90. double i = ULy;
  91. for (int cy = 0; cy < nrPointsY; cy++) {
  92. double r = ULx;
  93. for (int cx = 0; cx < nrPointsX; cx++) {
  94. Complex c = new Complex(r, i);
  95. this.result[counter] = maxIterations - this.iterateOnePoint(c, maxIterations, 2.0);
  96. counter += 1;
  97. r += stepX;
  98. }
  99. i += stepY;
  100. }
  101. }
  102. public int[] getResult() {
  103. return this.result;
  104. }
  105. /*
  106. public double getULx() {
  107. return this.ULx;
  108. }
  109. public double getLRx() {
  110. return this.LRx;
  111. }
  112. public double getULy() {
  113. return this.ULy;
  114. }
  115. public double getLRy() {
  116. return this.LRy;
  117. }
  118. public void setView(double ULx, double ULy, double LRx, double LRy) {
  119. this.ULx = ULx;
  120. this.ULy = ULy;
  121. this.LRx = LRx;
  122. this.LRy = LRy;
  123. }
  124. */
  125. }