/** Main method. */
  public void run() {
    logger.debug("reducer server running: "); // + rank);
    // try {
    //     pairChannel = new MPIChannel(engine, rank);
    // } catch (IOException e) {
    //     e.printStackTrace();
    //     return;
    // } catch (MPIException e) {
    //     e.printStackTrace();
    //     return;
    // }
    if (logger.isInfoEnabled()) {
      logger.info("reducer server running: pairChannel = " + pairChannel);
    }
    Pair<C> pair;
    GenPolynomial<C> H = null;
    boolean set = false;
    boolean goon = true;
    int polIndex = -1;
    int red = 0;
    int sleeps = 0;

    // while more requests
    while (goon) {
      // receive request
      logger.debug("receive request");
      Object req = null;
      try {
        req = pairChannel.receive();
      } catch (IOException e) {
        goon = false;
        e.printStackTrace();
      } catch (MPIException e) {
        goon = false;
        e.printStackTrace();
      } catch (ClassNotFoundException e) {
        goon = false;
        e.printStackTrace();
      }
      // logger.debug("received request, req = " + req);
      if (req == null) {
        goon = false;
        break;
      }
      if (!(req instanceof GBTransportMessReq)) {
        goon = false;
        break;
      }

      // find pair
      logger.debug("find pair");
      while (!pairlist.hasNext()) { // wait
        if (!set) {
          finaler.beIdle();
          set = true;
        }
        if (!finaler.hasJobs() && !pairlist.hasNext()) {
          goon = false;
          break;
        }
        try {
          sleeps++;
          if (sleeps % 10 == 0) {
            logger.info(" reducer is sleeping");
          }
          Thread.sleep(100);
        } catch (InterruptedException e) {
          goon = false;
          break;
        }
      }
      if (!pairlist.hasNext() && !finaler.hasJobs()) {
        goon = false;
        break; // continue; //break?
      }
      if (set) {
        set = false;
        finaler.notIdle();
      }

      pair = pairlist.removeNext();
      /*
       * send pair to client, receive H
       */
      logger.debug("send pair = " + pair);
      GBTransportMess msg = null;
      if (pair != null) {
        msg = new GBTransportMessPairIndex(pair);
      } else {
        msg = new GBTransportMess(); // End();
        // goon ?= false;
      }
      try {
        pairChannel.send(msg);
      } catch (IOException e) {
        e.printStackTrace();
        goon = false;
        break;
      } catch (MPIException e) {
        e.printStackTrace();
        goon = false;
        break;
      }
      logger.debug("#distributed list = " + theList.size());
      Object rh = null;
      try {
        rh = pairChannel.receive();
      } catch (IOException e) {
        e.printStackTrace();
        goon = false;
        break;
      } catch (MPIException e) {
        e.printStackTrace();
        goon = false;
        break;
      } catch (ClassNotFoundException e) {
        e.printStackTrace();
        goon = false;
        break;
      }
      // logger.debug("received H polynomial");
      if (rh == null) {
        if (pair != null) {
          pair.setZero();
        }
      } else if (rh instanceof GBTransportMessPoly) {
        // update pair list
        red++;
        H = ((GBTransportMessPoly<C>) rh).pol;
        if (logger.isDebugEnabled()) {
          logger.debug("H = " + H);
        }
        if (H == null) {
          if (pair != null) {
            pair.setZero();
          }
        } else {
          if (H.isZERO()) {
            pair.setZero();
          } else {
            if (H.isONE()) {
              polIndex = pairlist.putOne();
              // GenPolynomial<C> nn =
              theList.putWait(Integer.valueOf(polIndex), H);
              goon = false;
              break;
            }
            polIndex = pairlist.put(H);
            // use putWait ? but still not all distributed
            // GenPolynomial<C> nn =
            theList.putWait(Integer.valueOf(polIndex), H);
          }
        }
      }
    }
    logger.info("terminated, done " + red + " reductions");

    /*
     * send end mark to client
     */
    logger.debug("send end");
    try {
      pairChannel.send(new GBTransportMessEnd());
    } catch (IOException e) {
      if (logger.isDebugEnabled()) {
        e.printStackTrace();
      }
    } catch (MPIException e) {
      if (logger.isDebugEnabled()) {
        e.printStackTrace();
      }
    }
    finaler.beIdle();
    pairChannel.close();
  }