import java.util.*;
import java.io.*;
import java.net.*;
import java.lang.*;

public class fftworker
{
   public static void main(String args[]) throws IOException
   {
      int job_id = 1;
      int task_id = Integer.parseInt(args[0]);

      String main_pt_name;
      int main_pt_no;
      String gp_name;
      int gp_no;
      String pt_name;
      int pt_no;

      int i, status;
		int element1, element2;
		int no_of_ele = 8;

		float omega, omega_pwr, temp1, temp2;

      JMPF def_obj;
 
      gp_name = "gp1";
      pt_name = String.valueOf(task_id);
      main_pt_name = "main";
      def_obj = new JMPF(job_id, task_id);
      pt_no = def_obj.CreatePort(pt_name);
      gp_no = def_obj.LocatePortGroup(gp_name, true);
      System.out.println("The status of locating is : " + gp_no);

    	status = def_obj.AddMember(gp_name, pt_no);
      System.out.println("The status is : " + status);

// waits for the first set of data and calculcates the first stage calci
      String string = def_obj.BlockReceiveString(pt_no);
		System.out.println("First stage calci.");

      StringTokenizer st = new StringTokenizer(string);
      int j = Integer.parseInt(st.nextToken());
      element1 = Integer.parseInt(st.nextToken());
      element2 = Integer.parseInt(st.nextToken());


// calculation of OMEGA_PWR.
		temp1 = (float)(Math.sin(3.1415/no_of_ele));
		temp2 = (float)(Math.cos(3.1415/no_of_ele));
		omega = (float)(Math.sqrt(temp1*temp1 + temp2*temp2));

		omega_pwr = 1;
		if (pt_no%2 != 0)
			omega_pwr = omega*omega;
		System.out.println ("OMEGA POWER is calculated.");

// performing part of the fft calculation.
		int result = (int)(element1 + omega_pwr*element2);
		System.out.println("Main calculation performed.");

		System.out.println("Input elements are : "+element1+" and "+element2);
		System.out.println("result : "+result);
      main_pt_no = def_obj.LocatePort(main_pt_name);

      synchronized(def_obj)
      {
         String str = String.valueOf(result);
         status = def_obj.Send(main_pt_no, str);
			System.out.println("Sending back the data!");
      }

// waits for the second set of data and then calculates
      string = def_obj.BlockReceiveString(pt_no);
		System.out.println("Second stage calci.");

      st = new StringTokenizer(string);
      j = Integer.parseInt(st.nextToken());
      element1 = Integer.parseInt(st.nextToken());
      element2 = Integer.parseInt(st.nextToken());


// calculation of OMEGA_PWR.
		omega_pwr = 1;
		for ( i = pt_no; i != 0; i--)
			omega_pwr *= omega;
		System.out.println ("OMEGA POWER is calculated.");

// performing part of the fft calculation.
		result = (int)(element1 + omega_pwr*element2);
		System.out.println("Input elements are : "+element1+" and "+element2);
		System.out.println("result : "+result);
		System.out.println("Main calculation performed.");

//      main_pt_no = def_obj.LocatePort(main_pt_name);

      synchronized(def_obj)
      {
         String str = String.valueOf(result);
			System.out.println("String str is : "+ str);
         status = def_obj.Send(main_pt_no, str);
			System.out.println("Sending back the data!");
      }
      def_obj.ClosePorts();
   }
}


