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.

150 lines
3.7 KiB

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