2011-11-15 14 views
-1

私は、マスターがソケット接続の通信任務を遂行し、1人または複数のワーカーに番号を送信し、それができるポイント数を受け取ることができるモンテカルロ用の分散ソリューションを実行するタスクを持っています受信した他のカウントと合計します。私はstuck.itはちょうどこれが私のコードJavaのモンテカルロシミュレーション

import java.io.IOException; 
    import java.io.ObjectInputStream; 
    import java.io.ObjectOutputStream; 
    import java.io.PrintWriter; 
    import java.net.ServerSocket; 
    import java.net.Socket; 
    import java.util.Random; 
    import java.util.Scanner; 

public class PICalcDistributedMaster { 

    PrintWriter pWriter; 

    // x coordinate of circle centre also equal to x coordinate of square centre 

    private double xPoint; 

    // y coordinate of circle centre also equal to y coordinate of square centre 

    private double yPoint; 

    // Half the width of the square, also equal to circle radius. 

    private double radius; 

public PICalcDistributedMaster(double radius) { 
      this.xPoint = radius; 
      this.yPoint = radius; 
      this.radius = radius; 
    } 

public void setRadius(double radius) { 

     this.radius = radius; 

    } 

public double getRadius() { 

     return radius; 
    } 

    /** 
    * 
    * @param x The x coordinate of a point,p 
    * @param y The y coordinate of a point,p 
    * @return true if p lies within a circle inscribed within the square, 
    * such that circle diameter = square diameter. 
    * To do this, this method finds the distance between the point 
    * and the centre of the circle and if the distance is greater than 
    * 
    */ 

    public boolean isInside(double x , double y){ 

    double dx = this.xPoint-x; 
    double dy = this.yPoint-y; 
    double dist = Math.sqrt(dx*dx+dy*dy); 

    return dist <= this.radius; 

    }//end method. 

    /** 
    * Assumes a square whose centre is at point r,r 
    * where r = square's radius. and its edges are at 0,0, 2r,0, 0,2r and 2r,2r. 
    */ 
    public void go(){ 

     calculate(); 
     send(); 
     connect(); 
    } 
    public void send(){ 

      try{         
       Socket s = new Socket(); 
       ServerSocket ss = new ServerSocket(1234, 50); 
       System.out.println("Waiting for connection"); 

       s = ss.accept(); // accepts network connection 
       PrintWriter pWriter = new PrintWriter(s.getOutputStream()); 
       pWriter.println("Sending number to Slave Machine"); 
       System.out.println("Waiting for connection"); 
       System.out.println("Connection received from "+s.getInetAddress());  

       ObjectOutputStream ostream = new ObjectOutputStream(s.getOutputStream()); 
       ostream.flush(); 

       ObjectInputStream istream = new ObjectInputStream(s.getInputStream()); 
       System.out.println("IO streams found"); 
       istream.read(); //reads the input stream 

       String message = "connection successful"; 
       ostream.writeObject(message); 
       ostream.flush(); 
       System.out.println(message);     
       } 

      catch (IOException ioe){ 

      ioe.printStackTrace(); 
      }   
      } 

     public void connect(){ 

     String message = "connection successful"; 
     } 

     void calculate(){ 

     PICalcDistributedMaster pim = new PICalcDistributedMaster(10); 
     Scanner input = new Scanner(System.in); 
     System.out.print("Please enter number of throws: "); 

     double numThrows = input.nextDouble(); 
     double PI = pim.computePI(numThrows); 
     double difference = PI - Math.PI; 

      System.out.println(" THE CALCULATED VALUE OF PI FOR "+numThrows+"THROWS="+PI); 
      System.out.println(" THE ACTUAL VALUE OF PI = "+Math.PI); 
      System.out.println(" THE DIFFERENCE IN DEVIATION FROM CALCULATED VALUE TO ACTUAL VALUE IS = "+difference); 

    } 

    public double computePI (double numThrows){ 

      Random randomGen = new Random(); 
      double hits = 0; 
      double PI = 0;    

      double width = radius * 2; 
      //radius * 2 is the width of the square so we are stretching the random number generated 
      //to apply across width, since by default it only generates between 0 and 1. 
     for (int i = 0; i <= numThrows; i++){ 

        double xPos = (randomGen.nextDouble()) * width; 
     double yPos = (randomGen.nextDouble()) * width; 
      if (isInside(xPos, yPos)){ 
      hits++;   
         } 

         PI = (4 * (hits/numThrows));      
        } 

        System.out.println("Throws = "+numThrows); 
        System.out.println("Hits = "+hits); 


      return PI; 
     } 


     public static void main(String [] args){ 

      PICalcDistributedMaster pim = new PICalcDistributedMaster(10); 
      pim.go(); 

     } 

}

答えて

0

s = ss.accept();がブロックされている接続を待って続けています。誰かがあなたに接続して何かを送るまで、あなたは接続を待っています。私が理解する限り、これはマネージャーです。したがって、ソケットを使用してManagerに接続し、タスクを要求するWorkerを実装します。

関連する問題