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.

151 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. double ULx = -0.16099999999999995, ULy = -0.9365333333333333, LRx = -0.03533333333333327, LRy = -0.8108666666666666;
  71. int iterateOnePoint(Complex c, int maxIterations, double maxModulus) {
  72. Complex z = new Complex(0., 0.);
  73. for (int i = 0; i < maxIterations; i++) {
  74. z.mulBy(z);
  75. z.add(c);
  76. double m = z.absValue();
  77. if (m > maxModulus) {
  78. return i;
  79. }
  80. }
  81. return maxIterations;
  82. }
  83. public void iterateAllPoints(int maxIterations, double nrPointsX, double nrPointsY) {
  84. this.result = new int[(int) (nrPointsX * nrPointsY)];
  85. int counter = 0;
  86. double stepX = (this.LRx - this.ULx) / nrPointsX;
  87. double stepY = (this.LRy - this.ULy) / nrPointsY;
  88. double i = this.ULy;
  89. for (int cy = 0; cy < nrPointsY; cy++) {
  90. double r = this.ULx;
  91. for (int cx = 0; cx < nrPointsX; cx++) {
  92. Complex c = new Complex(r, i);
  93. this.result[counter] = maxIterations - this.iterateOnePoint(c, maxIterations, 2.0);
  94. counter += 1;
  95. r += stepX;
  96. }
  97. i += stepY;
  98. }
  99. }
  100. public int[] getResult() {
  101. return this.result;
  102. }
  103. public double getULx() {
  104. return this.ULx;
  105. }
  106. public double getLRx() {
  107. return this.LRx;
  108. }
  109. public double getULy() {
  110. return this.ULy;
  111. }
  112. public double getLRy() {
  113. return this.LRy;
  114. }
  115. public void setView(double ULx, double ULy, double LRx, double LRy) {
  116. this.ULx = ULx;
  117. this.ULy = ULy;
  118. this.LRx = LRx;
  119. this.LRy = LRy;
  120. }
  121. }