/*
 * Script ?? Exercise 
 *
 * implement network channel
 *
 */
/* output for t=5, w=10.

   output on server:
   
   java ex10
   Listening for connection on port = 4711
   Server started
   pw = 20
   pw = 20
   pw = 20
   pw = 20
   pw = 20
           v  = 100000000000000000000
           pd = 20
           v  = 100000000000000000000
           pd = 20
           v  = 100000000000000000000
           pd = 20
           v  = 100000000000000000000
           pd = 20
           v  = 100000000000000000000
           pd = 20
   termination


   output on client:

   java ex10 -1 localhost
   Client started
   cw = 20
   cw = 20
   cw = 20
   cw = 20
   cw = 20
           v  = 100000000000000000000
           cd = 20
           v  = 100000000000000000000
           cd = 20
           v  = 100000000000000000000
           cd = 20
           v  = 100000000000000000000
           cd = 20
           v  = 100000000000000000000
           cd = 20
   termination
*/

import java.math.*;

class ex10prod implements Runnable {

  int l;

  public ex10prod(int li) {
    l = li; 
  }

  public void run() {

    BigInteger v, z;
    v = new BigInteger("1");
    z = new BigInteger("10");

    System.out.println("pw = " + l);

    for(int i=0; i<l; i++) {
          v = v.multiply(z);
          ex10.chan.send(v);
    }
    System.out.println("         v  = " + v);
    System.out.println("         pd = " + l);

  }
}

class ex10cons implements Runnable {

  int l;

  public ex10cons(int li) {
    l = li; 
  }

  public void run() {

    Object x = new Object();
    BigInteger v = new BigInteger("0");
 
    System.out.println("cw = " + l);

    for(int i=0; i<l; i++) {
          x = ex10.chan.receive();
          if (x instanceof BigInteger) {
             v = (BigInteger)x;
          }
    }
    System.out.println("         v  = " + v);
    System.out.println("         cd = " + l);

  }
}


public class ex10 {
  
  /*parameters */
  public static final int t = 5, w = 1000;

  /* channel */
  public static RemoteChannel chan;

  public static String host = ""; 
  public static int port = -1; 
 
  public static void main(String[] args) throws InterruptedException {

    if (args.length >= 1) { 
       try { port = Integer.parseInt(args[0]); } 
       catch (NumberFormatException e) { } 
    } 
    if (args.length >= 2) { 
       host = args[1]; 
    } 

    /* init channel */
    chan = new RemoteChannel(host, port);

    if (host == "") { spart(); }
               else { cpart(); }

    System.out.println("termination");
  }


  public static void spart() throws InterruptedException {
    /* con statement */
    Thread pt[] = new Thread[t]; 
    for (int i = 0; i<t; i++) { 
        pt[i] = new Thread(new ex10prod(w/t));
        pt[i].start();
    }
    System.out.println("Server started");
    for (int i = 0; i<t; i++) { pt[i].join(); }
  }


  public static void cpart() throws InterruptedException {
    /* con statement */
    Thread ct[] = new Thread[t]; 
    for (int i = 0; i<t; i++) { 
        ct[i] = new Thread(new ex10cons(w/t));
        ct[i].start();
    }
    System.out.println("Client started");
    for (int i = 0; i<t; i++) { ct[i].join(); }
  }
 
}




