示例#1
0
文件: Fabrik.java 项目: pmuehl/XVSM
  /** Erstellt den Core ohne Space und erzeugt das Capi fuer den Zugriff */
  public void xvsmSetup() {

    MzsCore core = null;
    try {
      core = DefaultMzsCore.newInstanceWithoutSpace();
    } catch (MzsCoreRuntimeException me) {
      me.printStackTrace();
    }
    capi = new Capi(core);
  }
  public static void main(String[] args) {
    Runtime.getRuntime()
        .addShutdownHook(
            new Thread() {
              @Override
              public void run() {
                try {
                  capi.take(
                      cRef,
                      KeyCoordinator.newSelector(id.toString()),
                      RequestTimeout.INFINITE,
                      null);
                } catch (MzsCoreException e) {
                  System.out.println("this should never happen :S");
                }
              }
            });

    Logistician logistician = new Logistician();

    // do some command checking
    int firstArg = -1;

    if (args.length == 1) {
      try {
        firstArg = Integer.parseInt(args[0]);
      } catch (NumberFormatException e) {
        System.err.println("Argument 1 must be an positive integer of WorkerID");
        System.exit(1);
      }
    } else {
      System.err.println("Usage: java Logistician 'workerId'");
      System.exit(1);
    }

    MzsCore core = DefaultMzsCore.newInstance();
    capi = new Capi(core);

    try {
      uri = new URI("xvsm://localhost:9877");
    } catch (URISyntaxException e1) {
      // TODO Auto-generated catch block
      e1.printStackTrace();
    }

    try {
      crefPc = App.getPcContainer(uri, capi);
      crefPcDefect = App.getPcDefectContainer(uri, capi);
      crefStorage = App.getStorageContainer(uri, capi);

      // create Notifications
      notification.createNotification(crefPc, logistician, Operation.WRITE);
    } catch (MzsCoreException e1) {
      // TODO Auto-generated catch block
      e1.printStackTrace();
    } catch (InterruptedException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }

    //
    //		try {
    //			cRef = capi.lookupContainer(
    //					containerName,
    //					uri,
    //					MzsConstants.RequestTimeout.INFINITE,
    //					null);
    //		} catch (MzsCoreException e1) {
    //			// TODO Auto-generated catch block
    //			e1.printStackTrace();
    //		}
    //
    //		try {
    //			shittyRef = capi.lookupContainer(
    //					shittyContainerName,
    //					uri,
    //					MzsConstants.RequestTimeout.INFINITE,
    //					null);
    //		} catch (MzsCoreException e1) {
    //			// TODO Auto-generated catch block
    //			e1.printStackTrace();
    //		}
    //
    //		try {
    //			sellRef = capi.lookupContainer(
    //					sellContainerName,
    //					uri,
    //					MzsConstants.RequestTimeout.INFINITE,
    //					null);
    //		} catch (MzsCoreException e1) {
    //			// TODO Auto-generated catch block
    //			e1.printStackTrace();
    //		}
    //
    //		// check if arguments are correct
    //		// try to insert worker id into space, exit if not unique
    id = firstArg;
    //
    //		Entry entry = new Entry(logistician.getClass(),
    // KeyCoordinator.newCoordinationData(id.toString()));
    //
    //    	try {
    //			capi.write(cRef, MzsConstants.RequestTimeout.TRY_ONCE, null, entry);
    //    	} catch (DuplicateKeyException dup) {
    //    		System.out.println("ERROR: A Worker with this key already exists, take another one!");
    //    		//TODO: cleanup!
    //    		return;
    //		} catch (MzsCoreException e) {
    //			// TODO Auto-generated catch block
    //			e.printStackTrace();
    //		}

    ArrayList<Computer> computerList = null;
    ArrayList<Computer> readComputerList = null;

    try {
      readComputerList =
          capi.read(
              crefPc,
              Arrays.asList(FifoCoordinator.newSelector(MzsConstants.Selecting.COUNT_MAX)),
              MzsConstants.RequestTimeout.INFINITE,
              null);
    } catch (MzsCoreException e3) {
      // TODO Auto-generated catch block
      e3.printStackTrace();
    }

    for (int i = 0; i <= readComputerList.size(); i++) {
      try {
        computerList =
            capi.take(
                crefPc,
                LabelCoordinator.newSelector("testedComputer", 1),
                RequestTimeout.INFINITE,
                null);
      } catch (MzsCoreException e) {
        System.out.println("this should never happen :S");
      }

      if (!computerList.isEmpty()) {
        Computer computer = computerList.get(0);

        System.out.println(
            "DEBUG: GOT COMPUTERLIST WITH COMPUTER CREATOR: "
                + computer.getMakerID()
                + " and Tester "
                + computer.getTesterID()
                + " AND ISDEFECT:"
                + computer.isDefect());

        // keep track of Logistician that processed it;
        computer.setLogisticianID(id);

        if (computer.isDefect()) {
          TransactionReference tx = null;
          try {
            tx = capi.createTransaction(5000, uri);
          } catch (MzsCoreException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
          }
          /**
           * Entry compEntry = new Entry(computer);
           *
           * <p>try { transaction = capi.createTransaction(MzsConstants.RequestTimeout.INFINITE,
           * uri); capi.write(compEntry, shittyRef, RequestTimeout.INFINITE, transaction);
           * capi.commitTransaction(transaction);
           */
          Entry compEntry = new Entry(computer, LabelCoordinator.newCoordinationData("shitty"));

          try {
            capi.write(compEntry, crefPcDefect, RequestTimeout.INFINITE, null);

          } catch (MzsCoreException e) {
            System.out.println("this should never happen :S");
          }
          try {
            capi.commitTransaction(tx);
          } catch (MzsCoreException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
          }
          System.out.println("DEBUG: WROTE DEFECT TRUE TO COMPUTER NR: " + computer.getMakerID());
        } else {
          TransactionReference tx = null;
          try {
            tx = capi.createTransaction(5000, uri);
          } catch (MzsCoreException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
          }
          /**
           * Entry compEntry = new Entry(computer);
           *
           * <p>try { transaction = capi.createTransaction(MzsConstants.RequestTimeout.INFINITE,
           * uri); capi.write(compEntry, sellRef, RequestTimeout.INFINITE, transaction);
           * capi.commitTransaction(transaction);
           */
          Entry compEntry = new Entry(computer, LabelCoordinator.newCoordinationData("sell"));

          try {
            capi.write(compEntry, crefStorage, RequestTimeout.INFINITE, null);

          } catch (MzsCoreException e) {
            System.out.println("this should never happen :S");
          }
          try {
            capi.commitTransaction(tx);
          } catch (MzsCoreException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
          }
          System.out.println("DEBUG: WROTE DEFECT FALSE TO COMPUTER NR: " + computer.getMakerID());
        }
      } else {
        System.out.println("DEBUG: Computerlist is Empty, retrying ");
      }
    }
  }