Exemplo n.º 1
0
  public void run() {
    List<UCFTestPathData> testData = m_testData.getTestPaths();
    int len = testData.size();
    // System.out.println("about to start "+ len + " users");
    String docbase = m_testData.getDocbase();
    int thinkTime = m_testData.getThinktime();

    // generating specified number of user threads
    WorkerThread[] workers = new WorkerThread[len];

    // should new a thread to serve every user
    for (int i = 0; i < len; i++) {
      UCFTestPathData data = testData.get(i);
      List<String> items = DocbaseHelper.buildWorkItems(data, docbase);
      workers[i] = new WorkerThread(docbase, m_url, data, thinkTime, items);
    }

    int rampup = m_testData.getRmpup() * 1000;
    // start workers
    try {
      for (int i = 0; i < len; i++) {
        workers[i].start();

        // add rampup time
        Thread.sleep(rampup);
      }

      // wait for all works done
      for (int i = 0; i < len; i++) workers[i].join();
    } catch (InterruptedException ignored) {
    }
  }
Exemplo n.º 2
0
 /**
  * Generates new simulated observations and adds them to the dialogue state. The method returns
  * true when a new user input has been generated, and false otherwise.
  *
  * @return whether a user input has been generated @
  */
 private boolean addNewObservations() {
   List<String> newObsVars = new ArrayList<String>();
   for (String var : simulatorState.getChanceNodeIds()) {
     if (var.contains("^o'")) {
       newObsVars.add(var);
     }
   }
   if (!newObsVars.isEmpty()) {
     MultivariateDistribution newObs = simulatorState.queryProb(newObsVars);
     for (String newObsVar : newObsVars) {
       newObs.modifyVariableId(newObsVar, newObsVar.replace("^o'", ""));
     }
     while (system.isPaused()) {
       try {
         Thread.sleep(50);
       } catch (InterruptedException e) {
       }
     }
     if (!newObs.getValues().isEmpty()) {
       if (newObs.getVariables().contains(system.getSettings().userInput)) {
         log.fine("Simulator output: " + newObs + "\n --------------");
         system.addContent(newObs);
         return true;
       } else {
         log.fine("Contextual variables: " + newObs);
         system.addContent(newObs);
       }
     }
   }
   return false;
 }
Exemplo n.º 3
0
    public void run() {
      while (!_closed) {
        try {
          updateAll();

          long now = System.currentTimeMillis();
          if (inetAddrCacheMS > 0 && _nextResolveTime < now) {
            _nextResolveTime = now + inetAddrCacheMS;
            for (Node node : _all) {
              node.updateAddr();
            }
          }

          // force check on master
          // otherwise master change may go unnoticed for a while if no write concern
          _mongo.getConnector().checkMaster(true, false);
        } catch (Exception e) {
          _logger.log(Level.WARNING, "couldn't do update pass", e);
        }

        try {
          Thread.sleep(updaterIntervalMS);
        } catch (InterruptedException ie) {
        }
      }
    }
 private void rest() {
   try {
     Thread.sleep(100); // do nothing for 1000 miliseconds (1 second)
   } catch (InterruptedException e) {
     e.printStackTrace();
   }
 }
Exemplo n.º 5
0
    public void run() {
      int i;
      String cmdToLookFor = getServerProperty("pidHint");
      if (cmdToLookFor == null) {
        StringBuffer buf = new StringBuffer();
        for (i = 0; i < mExecArgs.length; i++) {
          if (i > 0) {
            buf.append(" ");
          }
          buf.append(mExecArgs[i]);
        }
        cmdToLookFor = buf.toString();
      }

      mLogger.finer("Looking for pid for command: " + cmdToLookFor);

      i = 0;
      do {
        try {
          Thread.sleep(1000);
        } catch (InterruptedException ie) {
        }
        mPid = UnixStdlib.getProcessID(cmdToLookFor);
      } while (mPid == 0 && ++i < 300); // up to 5 minutes

      if (mPid == 0) {
        mLogger.warning("Giving up on trying to identify pid");
      } else {
        mLogger.finer("Serverpid server=" + mName + " pid=" + mPid);
      }
    }
Exemplo n.º 6
0
  /**
   * Creates a new DAG-based graph layout for the given Bayesian Network. The nodes are identified
   * by a string label, and the edges by a number.
   *
   * @param bn the Bayesian network
   * @return the generated layout
   */
  private Layout<String, Integer> getGraphLayout(DialogueState ds, boolean showParameters) {
    Forest<String, Integer> f = new DelegateForest<String, Integer>();

    // adding the nodes and edges
    int counter = 0;
    try {
      for (BNode node : new ArrayList<BNode>(ds.getNodes())) {
        if (showParameters || !ds.getParameterIds().contains(node.getId())) {
          String nodeName = getVerticeId(node);

          f.addVertex(nodeName);
          for (BNode inputNode : new ArrayList<BNode>(node.getInputNodes())) {
            if (ds.getNode(inputNode.getId()) != null) {
              String inputNodeName = getVerticeId(inputNode);
              f.addEdge(counter, inputNodeName, nodeName);
              counter++;
            }
          }
        }
      }

      CustomLayoutTransformer transformer = new CustomLayoutTransformer(ds);
      StaticLayout<String, Integer> layout = new StaticLayout<String, Integer>(f, transformer);

      layout.setSize(new Dimension(600, 600));

      return layout;
    } catch (ConcurrentModificationException | NullPointerException e) {
      try {
        Thread.sleep(50);
      } catch (InterruptedException e1) {
      }
      return getGraphLayout(ds, showParameters);
    }
  }
Exemplo n.º 7
0
  /**
   * Execute Task locally and wait
   *
   * @param cmd command
   * @return execution info
   */
  public String executeLocal(String cmd) {
    log.config(cmd);
    if (m_task != null && m_task.isAlive()) m_task.interrupt();

    m_task = new Task(cmd);
    m_task.start();

    StringBuffer sb = new StringBuffer();
    while (true) {
      //  Give it a bit of time
      try {
        Thread.sleep(500);
      } catch (InterruptedException ioe) {
        log.log(Level.SEVERE, cmd, ioe);
      }
      //  Info to user
      sb.append(m_task.getOut())
          .append("\n-----------\n")
          .append(m_task.getErr())
          .append("\n-----------");

      //  Are we done?
      if (!m_task.isAlive()) break;
    }
    log.config("done");
    return sb.toString();
  } //	executeLocal
Exemplo n.º 8
0
    /**
     * The run method lives for the life of the JobTracker, and removes Jobs that are not still
     * running, but which finished a long time ago.
     */
    public void run() {
      while (shouldRun) {
        try {
          Thread.sleep(RETIRE_JOB_CHECK_INTERVAL);
        } catch (InterruptedException ie) {
        }

        synchronized (jobs) {
          synchronized (jobInitQueue) {
            synchronized (jobsByArrival) {
              for (Iterator it = jobs.keySet().iterator(); it.hasNext(); ) {
                String jobid = (String) it.next();
                JobInProgress job = (JobInProgress) jobs.get(jobid);

                if (job.getStatus().getRunState() != JobStatus.RUNNING
                    && job.getStatus().getRunState() != JobStatus.PREP
                    && (job.getFinishTime() + RETIRE_JOB_INTERVAL < System.currentTimeMillis())) {
                  it.remove();

                  jobInitQueue.remove(job);
                  jobsByArrival.remove(job);
                }
              }
            }
          }
        }
      }
    }
Exemplo n.º 9
0
 protected void runEcho() {
   Scanner socketScanner = null;
   String newLine;
   try {
     socketScanner = new Scanner(socket.getInputStream());
   } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
   }
   while (true) {
     try {
       Thread.sleep(CLERK_DELAY_MS);
     } catch (InterruptedException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
     }
     if (socket.isClosed()) return;
     if (socketScanner.hasNextLine()) {
       newLine = socketScanner.nextLine() + END_OF_LINE;
       System.out.println("echoing: " + newLine);
       try {
         socket.getOutputStream().write(newLine.getBytes(encoding));
       } catch (IOException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
       }
     }
   }
 }
Exemplo n.º 10
0
    @Override
    @SuppressWarnings("SleepWhileHoldingLock")
    public void run() {
      try {
        // initialize the statusbar
        status.removeAll();
        JProgressBar progress = new JProgressBar();
        progress.setMinimum(0);
        progress.setMaximum(doc.getLength());
        status.add(progress);
        status.revalidate();

        // start writing
        Writer out = new FileWriter(f);
        Segment text = new Segment();
        text.setPartialReturn(true);
        int charsLeft = doc.getLength();
        int offset = 0;
        while (charsLeft > 0) {
          doc.getText(offset, Math.min(4096, charsLeft), text);
          out.write(text.array, text.offset, text.count);
          charsLeft -= text.count;
          offset += text.count;
          progress.setValue(offset);
          try {
            Thread.sleep(10);
          } catch (InterruptedException e) {
            Logger.getLogger(FileSaver.class.getName()).log(Level.SEVERE, null, e);
          }
        }
        out.flush();
        out.close();
      } catch (IOException e) {
        final String msg = e.getMessage();
        SwingUtilities.invokeLater(
            new Runnable() {

              public void run() {
                JOptionPane.showMessageDialog(
                    getFrame(),
                    "Could not save file: " + msg,
                    "Error saving file",
                    JOptionPane.ERROR_MESSAGE);
              }
            });
      } catch (BadLocationException e) {
        System.err.println(e.getMessage());
      }
      // we are done... get rid of progressbar
      status.removeAll();
      status.revalidate();
    }
Exemplo n.º 11
0
    /**
     * The run method lives for the life of the JobTracker, and removes TaskTrackers that have not
     * checked in for some time.
     */
    public void run() {
      while (shouldRun) {
        //
        // Thread runs periodically to check whether trackers should be expired.
        // The sleep interval must be no more than half the maximum expiry time
        // for a task tracker.
        //
        try {
          Thread.sleep(TASKTRACKER_EXPIRY_INTERVAL / 3);
        } catch (InterruptedException ie) {
        }

        //
        // Loop through all expired items in the queue
        //
        synchronized (taskTrackers) {
          synchronized (trackerExpiryQueue) {
            long now = System.currentTimeMillis();
            TaskTrackerStatus leastRecent = null;
            while ((trackerExpiryQueue.size() > 0)
                && ((leastRecent = (TaskTrackerStatus) trackerExpiryQueue.first()) != null)
                && (now - leastRecent.getLastSeen() > TASKTRACKER_EXPIRY_INTERVAL)) {

              // Remove profile from head of queue
              trackerExpiryQueue.remove(leastRecent);
              String trackerName = leastRecent.getTrackerName();

              // Figure out if last-seen time should be updated, or if tracker is dead
              TaskTrackerStatus newProfile =
                  (TaskTrackerStatus) taskTrackers.get(leastRecent.getTrackerName());
              // Items might leave the taskTracker set through other means; the
              // status stored in 'taskTrackers' might be null, which means the
              // tracker has already been destroyed.
              if (newProfile != null) {
                if (now - newProfile.getLastSeen() > TASKTRACKER_EXPIRY_INTERVAL) {
                  // Remove completely
                  updateTaskTrackerStatus(trackerName, null);
                  lostTaskTracker(leastRecent.getTrackerName());
                } else {
                  // Update time by inserting latest profile
                  trackerExpiryQueue.add(newProfile);
                }
              }
            }
          }
        }
      }
    }
Exemplo n.º 12
0
 public static void startTracker(Configuration conf) throws IOException {
   if (tracker != null) throw new IOException("JobTracker already running.");
   while (true) {
     try {
       tracker = new JobTracker(conf);
       break;
     } catch (IOException e) {
       LOG.log(Level.WARNING, "Starting tracker", e);
     }
     try {
       Thread.sleep(1000);
     } catch (InterruptedException e) {
     }
   }
   tracker.offerService();
 }
Exemplo n.º 13
0
  public void run() {

    if (infosMotCles == null && infosSociete == null && maBD == null) return;

    String pageCourante = url.substring(infosSociete.get("url").toString().length(), url.length());

    try {
      // Si on a pas vu la page -> On fouille la page
      if (maBD.estDejaVu(infosSociete.get("url").toString(), pageCourante).equalsIgnoreCase("0")) {
        logger.log(Level.INFO, "Parsing - {0}", url);
        Document doc = Jsoup.connect(url).get();
        // Si c'est une page 404 on skip
        if (doc.title().indexOf("404") != -1) return;

        parser(doc, pageCourante);

        if (infosSociete.get("url").toString().equalsIgnoreCase(url)) {

          // On regarde si il y a des page en plus
          for (String link : getLinks(doc)) {
            pageCourante =
                link.substring(infosSociete.get("url").toString().length(), link.length());
            if (maBD.estDejaVu(infosSociete.get("url").toString(), pageCourante)
                .equalsIgnoreCase("0")) {
              // On lance un nouveau thread pour cette page
              // System.out.println("!"+pageCourante+" a plusieurs pages");
              new Thread(new Parser(infosSociete, infosMotCles, maBD, link, logger)).start();

              try {
                Thread.sleep(3500); // On wait 3.5 sec
              } catch (Exception e) {
              }
            }
          }
        }
      }
    } catch (Exception ex) {
      logger.log(Level.WARNING, "Parsing error - {0}", url + " - " + ex.toString());
    } finally {
      return;
    }
  }
Exemplo n.º 14
0
  public static void main(String args[]) throws Exception {
    List<ServerAddress> addrs = new LinkedList<ServerAddress>();
    addrs.add(new ServerAddress("127.0.0.1", 27017));
    addrs.add(new ServerAddress("127.0.0.1", 27018));

    Mongo m = new Mongo(addrs);

    ReplicaSetStatus status = new ReplicaSetStatus(m, addrs);
    System.out.println(status.ensureMaster()._addr);

    while (true) {
      System.out.println(status.ready());
      if (status.ready()) {
        status.printStatus();
        System.out.println(
            "master: " + status.getMaster() + "\t secondary: " + status.getASecondary());
      }
      System.out.println("-----------------------");
      Thread.sleep(5000);
    }
  }
Exemplo n.º 15
0
  // ----This function getting nodes from mongo's collection tree_nodes----
  // ---------the function insert nodes to neo4j and deletes it from mongo-----------
  // ----------------------------------------------------------------------------------------
  public void update_tree() {
    while (true) {
      DBObject obj = new BasicDBObject();
      obj.put("in_process", 0); // when other thread processing, 'in_process'=1
      DBCursor cursor = this.colltree.find(obj); // find only documents not in use of other thread
      // log4j.info(cursor.count() + " documents to process in collection tree nodes");
      if (!cursor.hasNext()) { // no documents to process
        try {
          log4j.info("there is no nodes to process at the moment, going to sleep for 10 seconds");
          Thread.currentThread();
          Thread.sleep(1000 * 10);
          log4j.info("update_tree woke up, continues");
        } catch (InterruptedException e) {
          log4j.error("InterruptedException caught, at update_all_tweets");
          e.printStackTrace();
          log4j.error(e);
        }
      } else // there are documents to process
      {
        try {
          while (cursor.hasNext()) {
            DBObject tr = cursor.next();
            try {
              String parent = tr.get("parent").toString();
              String[] sons = tr.get("son").toString().split(","); // make array of sons
              neo4j.addNode(
                  parent, sons, this.log4j); // create nodes and relationships if not exists
              this.colltree.remove(tr); // remove document from collection

            } catch (ConcurrentModificationException e) {
              // log4j.error(e);
              log4j.warn(e);
            }
          }
        } catch (MongoException e) {
          log4j.error(e);
        }
      }
    }
  }
Exemplo n.º 16
0
    public CustomLayoutTransformer(DialogueState network) {
      positions = new HashMap<String, Point2D>();
      Point current = new Point(0, 0);

      // trying to avoid nasty concurrent modifications
      List<String> allNodes = new ArrayList<String>();
      List<String> ruleNodes = new ArrayList<String>();
      for (int i = 0; i < 3; i++) {
        try {
          allNodes.addAll(network.getNodeIds());
          ruleNodes.addAll(network.getNodeIds(AnchoredRule.class));
          break;
        } catch (ConcurrentModificationException | NullPointerException e) {
          try {
            Thread.sleep(50);
          } catch (InterruptedException e1) {
          }
        }
      }
      for (String node : allNodes) {
        if (!node.contains("'") && !node.contains("=") && !ruleNodes.contains(node)) {
          positions.put(node, current);
          current = incrementPoint(current);
        }
      }
      current = new Point(current.x + 200, 0);
      for (String node : ruleNodes) {
        positions.put(node, current);
        current = incrementPoint(current);
      }

      current = new Point(current.x + 200, 0);
      for (String node : allNodes) {
        if (!positions.containsKey(node)) {
          positions.put(node, current);
          current = incrementPoint(current);
        }
      }
    }
Exemplo n.º 17
0
  @Override
  public void run() {
    try {
      socketInput =
          new BufferedReader(
              new InputStreamReader(
                  connectionSocket.getInputStream())); // get the inputstream from other peer
      pieceReader =
          connectionSocket
              .getInputStream(); // return an input stream for reading bytes from this socket.
      socketOutput =
          new DataOutputStream(
              connectionSocket.getOutputStream()); // get the outputstream to others
      // return an output stream for writing bytes to this socket.
    } catch (IOException ex) {
      Logger.getLogger(SocketThread.class.getName()).log(Level.SEVERE, null, ex);
    }

    // TCP handshake
    if (OtherPeerID != -1) {
      try {
        socketOutput.writeBytes(handshakeMsg);
      } catch (IOException ex) {
        Logger.getLogger(SocketThread.class.getName()).log(Level.SEVERE, null, ex);
      }

      try {
        while (socketInput.ready() == false) {}

      } catch (Exception e) {
      }
      try {
        checkHandShakeMsg = socketInput.readLine() + "";
      } catch (IOException ex) {
        Logger.getLogger(SocketThread.class.getName()).log(Level.SEVERE, null, ex);
      }
      checkHandShakeHeader = checkHandShakeMsg.substring(0, 18);
      OtherPeerID = Integer.parseInt(checkHandShakeMsg.substring(28, 32));

    } else // ackhandshake
    {

      try {
        while (socketInput.ready() == false) {}

      } catch (Exception e) {
      }

      try {
        checkHandShakeMsg = socketInput.readLine() + "";
      } catch (IOException ex) {
        Logger.getLogger(SocketThread.class.getName()).log(Level.SEVERE, null, ex);
      }
      checkHandShakeHeader = checkHandShakeMsg.substring(0, 18);
      OtherPeerID = Integer.parseInt(checkHandShakeMsg.substring(28, 32));

      if (checkHandShakeHeader.equals(handshakeheader)) {

        try { // if it is the right neighbour, write log that peer1 is connected from peer2
          peerProcess.logfile.write(LogWriter.logmsg(1, peerProcess.myPeerID, OtherPeerID));
        } catch (IOException ex) {
          Logger.getLogger(SocketThread.class.getName()).log(Level.SEVERE, null, ex);
        }

        try {

          socketOutput.writeBytes(handshakeMsg);
        } catch (IOException ex) {
          Logger.getLogger(SocketThread.class.getName()).log(Level.SEVERE, null, ex);
        }
      }
    }

    // start transferring
    try {
      while (quited == false
          && (peerProcess.Peers.elementAt(peerProcess.myPeerID % 1000 - 1).obtainAll() == false
              || peerProcess.Peers.elementAt(OtherPeerID % 1000 - 1).obtainAll() == false)) {

        try {
          socketOutput.writeBytes(peerNotExitMsg);
          System.out.println(
              "Peer "
                  + peerProcess.myPeerID
                  + " sent not quit message to peer "
                  + OtherPeerID
                  + ".");
        } catch (Exception e) {
        }
        try {
          while (socketInput.ready() == false) {
            //
          }
        } catch (Exception e) {
        }
        try {
          MsgFromOtherPeer = socketInput.readLine() + "";
        } catch (IOException ex) {
          Logger.getLogger(SocketThread.class.getName()).log(Level.SEVERE, null, ex);
        }
        // get the corresponding message length, message type and message payload
        RecvdMsg = MsgFromOtherPeer.split("#");
        msgLen = Integer.parseInt(RecvdMsg[0]);
        msgType = Integer.parseInt(RecvdMsg[1]);
        msgPayload = RecvdMsg[2];
        if (msgType == 8) {

          System.out.println(
              "Peer "
                  + peerProcess.myPeerID
                  + " received quit message from peer "
                  + OtherPeerID
                  + ".");
          quited = true;
          break;
        } else if (msgType != 9) {
          System.out.println(
              "Received message type " + msgType + ", expecting quit/not quit message.");
        } else {
          System.out.println(
              "Peer "
                  + peerProcess.myPeerID
                  + " received not quit message from peer "
                  + OtherPeerID
                  + ".");
        }

        // bitField exchange
        try {
          mybitfield =
              peerProcess.Peers.elementAt(peerProcess.myPeerID % 1000 - 1).obtainBitField();
          haveMsg = mybitfield.length() + "#" + 4 + "#" + mybitfield + "\n";
          socketOutput.writeBytes(haveMsg);
          System.out.println(
              "Peer "
                  + peerProcess.myPeerID
                  + " sent bitfield message to peer "
                  + OtherPeerID
                  + ".");
        } catch (Exception e) {
        }
        try {
          while (socketInput.ready() == false) {}

        } catch (Exception e) {
        }
        try {
          MsgFromOtherPeer = socketInput.readLine() + "";
        } catch (IOException ex) {
          Logger.getLogger(SocketThread.class.getName()).log(Level.SEVERE, null, ex);
        }
        RecvdMsg = MsgFromOtherPeer.split("#");
        msgLen = Integer.parseInt(RecvdMsg[0]);
        msgType = Integer.parseInt(RecvdMsg[1]);
        msgPayload = RecvdMsg[2];
        if (msgType == 4) {

          System.out.println(
              "Peer "
                  + peerProcess.myPeerID
                  + " received bitField message from peer "
                  + OtherPeerID
                  + ".");
          peerProcess.Peers.elementAt(OtherPeerID % 1000 - 1).setTheBitField(msgPayload);
        } else if (msgType == 8) {

          System.out.println(
              "Peer "
                  + peerProcess.myPeerID
                  + " received quit message from peer "
                  + OtherPeerID
                  + ".");
          quited = true;
          break;
        } else {
          System.out.println(" received message type " + msgType + ", expecting bitField message.");
        }

        if (peerProcess.choked[OtherPeerID % 1000 - 1] == false) {
          try {

            socketOutput.writeBytes(unchokedMsg);
          } catch (IOException ex) {
            Logger.getLogger(SocketThread.class.getName()).log(Level.SEVERE, null, ex);
          }
          mybitfield =
              peerProcess.Peers.elementAt(peerProcess.myPeerID % 1000 - 1).obtainBitField();
          haveMsg = mybitfield.length() + "#" + 4 + "#" + mybitfield + "\n";
          try {

            socketOutput.writeBytes(haveMsg);
          } catch (IOException ex) {
            Logger.getLogger(SocketThread.class.getName()).log(Level.SEVERE, null, ex);
          }

          try {
            while (socketInput.ready() == false) {}

          } catch (Exception e) {
          }
          try {
            MsgFromOtherPeer = socketInput.readLine();
          } catch (IOException ex) {
            Logger.getLogger(SocketThread.class.getName()).log(Level.SEVERE, null, ex);
          }
          RecvdMsg = MsgFromOtherPeer.split("#");
          msgLen = Integer.parseInt(RecvdMsg[0]);
          msgType = Integer.parseInt(RecvdMsg[1]);
          msgPayload = RecvdMsg[2];
          if (msgType == 1) {
            try {
              peerProcess.logfile.write(LogWriter.logmsg(3, peerProcess.myPeerID, OtherPeerID));
            } catch (IOException ex) {
              Logger.getLogger(SocketThread.class.getName()).log(Level.SEVERE, null, ex);
            }
            try {
              while (socketInput.ready() == false) {}
            } catch (Exception e) {
            }
            try {
              MsgFromOtherPeer = socketInput.readLine();
            } catch (IOException ex) {
              Logger.getLogger(SocketThread.class.getName()).log(Level.SEVERE, null, ex);
            }
            RecvdMsg = MsgFromOtherPeer.split("#");
            msgLen = Integer.parseInt(RecvdMsg[0]);
            msgType = Integer.parseInt(RecvdMsg[1]);
            msgPayload = RecvdMsg[2];
            if (msgType == 4) {
              peerProcess.Peers.elementAt(OtherPeerID % 1000 - 1).setTheBitField(msgPayload);
              interestedPieceID =
                  peerProcess
                      .Peers
                      .elementAt(peerProcess.myPeerID % 1000 - 1)
                      .obtainInterestedPiece(OtherPeerID);
              try {
                if (interestedPieceID != -1)
                  peerProcess.logfile.write(
                      LogWriter.logmsg(5, peerProcess.myPeerID, OtherPeerID, interestedPieceID));
              } catch (IOException ex) {
                Logger.getLogger(SocketThread.class.getName()).log(Level.SEVERE, null, ex);
              }
              if (interestedPieceID == -1) {
                try {
                  socketOutput.writeBytes(1 + "#" + 3 + "#" + "-1\n");
                } catch (IOException ex) {
                  Logger.getLogger(SocketThread.class.getName()).log(Level.SEVERE, null, ex);
                }

                try {
                  while (socketInput.ready() == false) {}
                } catch (Exception e) {
                }
                try {
                  MsgFromOtherPeer = socketInput.readLine() + "";
                } catch (IOException ex) {
                  Logger.getLogger(SocketThread.class.getName()).log(Level.SEVERE, null, ex);
                }
                RecvdMsg = MsgFromOtherPeer.split("#");
                msgLen = Integer.parseInt(RecvdMsg[0]);
                msgType = Integer.parseInt(RecvdMsg[1]);
                msgPayload = RecvdMsg[2];
                if (msgType == 2) {
                  try {
                    peerProcess.logfile.write(
                        LogWriter.logmsg(6, peerProcess.myPeerID, OtherPeerID));
                  } catch (IOException ex) {
                    Logger.getLogger(SocketThread.class.getName()).log(Level.SEVERE, null, ex);
                  }
                  byte[] piececontent = peerProcess.obtainPiece(Integer.parseInt(msgPayload));
                  piecemsg = msgPayload + "#" + 7 + "#" + piececontent.length + "\n";
                  try {
                    socketOutput.writeBytes(piecemsg);

                    while (socketInput.ready() == false) {}

                    try {
                      MsgFromOtherPeer = socketInput.readLine();
                    } catch (Exception e) {
                    }
                    RecvdMsg = MsgFromOtherPeer.split("#");
                    msgLen = Integer.parseInt(RecvdMsg[0]);
                    msgType = Integer.parseInt(RecvdMsg[1]);
                    msgPayload = RecvdMsg[2];
                    if (msgType == 10) {

                      socketOutput.write(piececontent);

                      while (socketInput.ready() == false) {}

                      try {
                        MsgFromOtherPeer = socketInput.readLine();
                      } catch (Exception e) {
                      }
                      RecvdMsg = MsgFromOtherPeer.split("#");
                      msgLen = Integer.parseInt(RecvdMsg[0]);
                      msgType = Integer.parseInt(RecvdMsg[1]);
                      msgPayload = RecvdMsg[2];
                      if (msgType != 10) {
                        System.out.println(
                            "received message type "
                                + msgType
                                + ", expecting channel clear message.");
                      }
                      continue;
                    } else {
                      System.out.println(
                          "received message type "
                              + msgType
                              + ", expecting channel clear message.");
                    }
                  } catch (IOException ex) {
                    Logger.getLogger(SocketThread.class.getName()).log(Level.SEVERE, null, ex);
                  }
                } else if (msgType == 3) {
                  try {

                    peerProcess.logfile.write(
                        LogWriter.logmsg(7, peerProcess.myPeerID, OtherPeerID));
                  } catch (IOException ex) {
                    Logger.getLogger(SocketThread.class.getName()).log(Level.SEVERE, null, ex);
                  }
                }
              } else {
                try {
                  socketOutput.writeBytes(1 + "#" + 2 + "#" + interestedPieceID + "\n");
                } catch (IOException ex) {
                  Logger.getLogger(SocketThread.class.getName()).log(Level.SEVERE, null, ex);
                }

                try {
                  while (socketInput.ready() == false) {}
                } catch (Exception e) {
                }
                try {
                  MsgFromOtherPeer = socketInput.readLine() + "";
                } catch (IOException ex) {
                  Logger.getLogger(SocketThread.class.getName()).log(Level.SEVERE, null, ex);
                }
                RecvdMsg = MsgFromOtherPeer.split("#");
                msgLen = Integer.parseInt(RecvdMsg[0]);
                msgType = Integer.parseInt(RecvdMsg[1]);
                msgPayload = RecvdMsg[2];
                if (msgType == 2) {
                  byte[] piececontent = peerProcess.obtainPiece(Integer.parseInt(msgPayload));
                  piecemsg = msgPayload + "#" + 7 + "#" + piececontent.length + "\n";
                  try {
                    try {
                      peerProcess.logfile.write(
                          LogWriter.logmsg(6, peerProcess.myPeerID, OtherPeerID));
                    } catch (IOException ex) {
                      Logger.getLogger(SocketThread.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    socketOutput.writeBytes(piecemsg);

                    while (socketInput.ready() == false) {}
                    try {
                      MsgFromOtherPeer = socketInput.readLine();
                    } catch (Exception e) {
                    }
                    RecvdMsg = MsgFromOtherPeer.split("#");
                    msgLen = Integer.parseInt(RecvdMsg[0]);
                    int indexFromOther = msgLen;
                    msgType = Integer.parseInt(RecvdMsg[1]);
                    msgPayload = RecvdMsg[2];
                    int loadsize = Integer.parseInt(msgPayload);
                    byte[] mspiececon = new byte[loadsize];
                    if (msgType == 7) {
                      if (peerProcess.myPeerID < OtherPeerID) {

                        socketOutput.writeBytes(channelClearMsg);

                        while (pieceReader.available() <= 0) {}

                        int totallen = 0;
                        byte[] c = new byte[1];
                        while (totallen < loadsize) {
                          try {
                            pieceReader.read(c);
                          } catch (Exception e) {
                          }
                          mspiececon[totallen] = c[0];
                          totallen++;
                        }
                        socketOutput.write(piececontent);
                        try {

                          peerProcess.writebackPiece(indexFromOther, mspiececon);
                          peerProcess.logfile.write(
                              LogWriter.logmsg(
                                  8,
                                  peerProcess.myPeerID,
                                  OtherPeerID,
                                  interestedPieceID,
                                  peerProcess
                                      .Peers
                                      .elementAt(peerProcess.myPeerID % 1000 - 1)
                                      .hasHowManyPieces()));

                          if (peerProcess
                                  .Peers
                                  .elementAt(peerProcess.myPeerID % peerIDBase - 1)
                                  .obtainAll()
                              == true) {
                            try {
                              peerProcess.logfile.write(LogWriter.logmsg(9, peerProcess.myPeerID));
                            } catch (Exception e) {
                            }
                          }
                        } catch (IOException ex) {
                          Logger.getLogger(SocketThread.class.getName())
                              .log(Level.SEVERE, null, ex);
                        }

                        while (socketInput.ready() == false) {}

                        try {
                          MsgFromOtherPeer = socketInput.readLine();
                        } catch (Exception e) {
                        }
                        RecvdMsg = MsgFromOtherPeer.split("#");
                        msgLen = Integer.parseInt(RecvdMsg[0]);
                        msgType = Integer.parseInt(RecvdMsg[1]);
                        msgPayload = RecvdMsg[2];
                        if (msgType != 10) {
                          System.out.println(
                              "Expecting channel clear message, received message type " + msgType);
                        }
                        continue;
                      } else {

                        while (socketInput.ready() == false) {}

                        try {
                          MsgFromOtherPeer = socketInput.readLine();
                        } catch (Exception e) {
                        }
                        RecvdMsg = MsgFromOtherPeer.split("#");
                        msgLen = Integer.parseInt(RecvdMsg[0]);
                        msgType = Integer.parseInt(RecvdMsg[1]);
                        msgPayload = RecvdMsg[2];
                        if (msgType == 10) {

                          socketOutput.write(piececontent);

                          while (pieceReader.available() <= 0) {}

                          int totallen = 0;
                          byte[] c = new byte[1];
                          while (totallen < loadsize) {
                            try {
                              pieceReader.read(c);
                            } catch (Exception e) {
                            }
                            mspiececon[totallen] = c[0];
                            totallen++;
                          }
                          socketOutput.writeBytes(channelClearMsg);
                          try {

                            peerProcess.writebackPiece(indexFromOther, mspiececon);
                            peerProcess.logfile.write(
                                LogWriter.logmsg(
                                    8,
                                    peerProcess.myPeerID,
                                    OtherPeerID,
                                    interestedPieceID,
                                    peerProcess
                                        .Peers
                                        .elementAt(peerProcess.myPeerID % 1000 - 1)
                                        .hasHowManyPieces()));

                            if (peerProcess
                                    .Peers
                                    .elementAt(peerProcess.myPeerID % peerIDBase - 1)
                                    .obtainAll()
                                == true) {
                              try {
                                peerProcess.logfile.write(
                                    LogWriter.logmsg(9, peerProcess.myPeerID));
                              } catch (Exception e) {
                              }
                            }
                          } catch (IOException ex) {
                            Logger.getLogger(SocketThread.class.getName())
                                .log(Level.SEVERE, null, ex);
                          }
                          continue;
                        } else {
                          System.out.println(
                              "Expecting channel clear message, received message type " + msgType);
                        }
                      }
                    } else {
                      System.out.println(
                          "Expecting piecemessage, received message type " + msgType);
                    }

                  } catch (IOException ex) {
                    Logger.getLogger(SocketThread.class.getName()).log(Level.SEVERE, null, ex);
                  }
                } else if (msgType == 3) {
                  try {

                    peerProcess.logfile.write(
                        LogWriter.logmsg(7, peerProcess.myPeerID, OtherPeerID));
                  } catch (IOException ex) {
                    Logger.getLogger(SocketThread.class.getName()).log(Level.SEVERE, null, ex);
                  }
                  try {

                    MsgFromOtherPeer = socketInput.readLine() + "";
                  } catch (IOException ex) {
                    Logger.getLogger(SocketThread.class.getName()).log(Level.SEVERE, null, ex);
                  }
                  RecvdMsg = MsgFromOtherPeer.split("#");
                  msgLen = Integer.parseInt(RecvdMsg[0]);
                  int indexFromOther = msgLen;
                  msgType = Integer.parseInt(RecvdMsg[1]);
                  msgPayload = RecvdMsg[2];
                  int loadsize = Integer.parseInt(msgPayload);
                  byte[] mspiececon = new byte[loadsize];
                  if (msgType == 7) {
                    socketOutput.writeBytes(channelClearMsg);

                    while (pieceReader.available() <= 0) {}
                    int totallen = 0;
                    byte[] c = new byte[1];
                    while (totallen < loadsize) {
                      try {
                        pieceReader.read(c);
                      } catch (Exception e) {
                      }
                      mspiececon[totallen] = c[0];
                      totallen++;
                    }
                    socketOutput.writeBytes(channelClearMsg);
                    try {

                      peerProcess.writebackPiece(indexFromOther, mspiececon);
                      peerProcess.logfile.write(
                          LogWriter.logmsg(
                              8,
                              peerProcess.myPeerID,
                              OtherPeerID,
                              interestedPieceID,
                              peerProcess
                                  .Peers
                                  .elementAt(peerProcess.myPeerID % 1000 - 1)
                                  .hasHowManyPieces()));

                      if (peerProcess
                              .Peers
                              .elementAt(peerProcess.myPeerID % peerIDBase - 1)
                              .obtainAll()
                          == true) {
                        try {
                          peerProcess.logfile.write(LogWriter.logmsg(9, peerProcess.myPeerID));
                        } catch (Exception e) {
                        }
                      }
                    } catch (IOException ex) {
                      Logger.getLogger(SocketThread.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    continue;
                  } else {
                    System.out.println(
                        "Expecting piece data, received message type" + msgType + ".");
                  }
                }
              }
            }
          } else if (msgType == 0) {

            try {
              peerProcess.logfile.write(LogWriter.logmsg(4, peerProcess.myPeerID, OtherPeerID));
            } catch (IOException ex) {
              Logger.getLogger(SocketThread.class.getName()).log(Level.SEVERE, null, ex);
            }

            try {
              while (socketInput.ready() == false) {}

            } catch (Exception e) {
            }
            try {
              MsgFromOtherPeer = socketInput.readLine();
            } catch (IOException ex) {
              Logger.getLogger(SocketThread.class.getName()).log(Level.SEVERE, null, ex);
            }
            RecvdMsg = MsgFromOtherPeer.split("#");
            msgLen = Integer.parseInt(RecvdMsg[0]);
            msgType = Integer.parseInt(RecvdMsg[1]);
            msgPayload = RecvdMsg[2];
            if (msgType == 2) {
              byte[] piececontent = peerProcess.obtainPiece(Integer.parseInt(msgPayload));
              piecemsg = msgPayload + "#" + 7 + "#" + piececontent.length + "\n";
              try {
                socketOutput.writeBytes(piecemsg);

                while (socketInput.ready() == false) {}

                try {
                  MsgFromOtherPeer = socketInput.readLine();
                } catch (Exception e) {
                }
                RecvdMsg = MsgFromOtherPeer.split("#");
                msgLen = Integer.parseInt(RecvdMsg[0]);
                msgType = Integer.parseInt(RecvdMsg[1]);
                msgPayload = RecvdMsg[2];
                if (msgType == 10) {

                  socketOutput.write(piececontent);

                  while (socketInput.ready() == false) {}

                  try {
                    MsgFromOtherPeer = socketInput.readLine();
                  } catch (Exception e) {
                  }
                  RecvdMsg = MsgFromOtherPeer.split("#");
                  msgLen = Integer.parseInt(RecvdMsg[0]);
                  msgType = Integer.parseInt(RecvdMsg[1]);
                  msgPayload = RecvdMsg[2];
                  if (msgType != 10) {
                    System.out.println(
                        "Expecting channel clear message, received message type " + msgType);
                  }
                  continue;
                } else {
                  System.out.println(
                      "Expecting channel clear message, received message type " + msgType);
                }
              } catch (IOException ex) {
                Logger.getLogger(SocketThread.class.getName()).log(Level.SEVERE, null, ex);
              }
              try {
                peerProcess.logfile.write(LogWriter.logmsg(6, peerProcess.myPeerID, OtherPeerID));
              } catch (IOException ex) {
                Logger.getLogger(SocketThread.class.getName()).log(Level.SEVERE, null, ex);
              }
            } else if (msgType == 3) {
              try {

                peerProcess.logfile.write(LogWriter.logmsg(7, peerProcess.myPeerID, OtherPeerID));
              } catch (IOException ex) {
                Logger.getLogger(SocketThread.class.getName()).log(Level.SEVERE, null, ex);
              }
            }
          } else {
            continue;
          }
        } else {
          try {
            socketOutput.writeBytes(chokedMsg);
          } catch (IOException ex) {
            Logger.getLogger(SocketThread.class.getName()).log(Level.SEVERE, null, ex);
          }

          try {
            while (socketInput.ready() == false) {}

          } catch (Exception e) {
          }
          try {
            MsgFromOtherPeer = socketInput.readLine() + "";
          } catch (IOException ex) {
            Logger.getLogger(SocketThread.class.getName()).log(Level.SEVERE, null, ex);
          }
          RecvdMsg = MsgFromOtherPeer.split("#");
          msgLen = Integer.parseInt(RecvdMsg[0]);
          msgType = Integer.parseInt(RecvdMsg[1]);
          msgPayload = RecvdMsg[2];
          if (msgType == 0) {

            try {
              peerProcess.logfile.write(LogWriter.logmsg(4, peerProcess.myPeerID, OtherPeerID));
            } catch (Exception e) {
            }

            continue;
          } else if (msgType == 1) {

            try {
              peerProcess.logfile.write(LogWriter.logmsg(3, peerProcess.myPeerID, OtherPeerID));
            } catch (Exception e) {
            }
            try {
              while (socketInput.ready() == false) {}

            } catch (Exception e) {
            }
            try {
              MsgFromOtherPeer = socketInput.readLine() + "";
            } catch (IOException ex) {
              Logger.getLogger(SocketThread.class.getName()).log(Level.SEVERE, null, ex);
            }
            RecvdMsg = MsgFromOtherPeer.split("#");
            msgLen = Integer.parseInt(RecvdMsg[0]);
            msgType = Integer.parseInt(RecvdMsg[1]);
            msgPayload = RecvdMsg[2];
            if (msgType == 4) {

              peerProcess.Peers.elementAt(OtherPeerID % 1000 - 1).setTheBitField(msgPayload);
              interestedPieceID =
                  peerProcess
                      .Peers
                      .elementAt(peerProcess.myPeerID % 1000 - 1)
                      .obtainInterestedPiece(OtherPeerID);
              try {

                if (interestedPieceID != -1)
                  peerProcess.logfile.write(
                      LogWriter.logmsg(5, peerProcess.myPeerID, OtherPeerID, interestedPieceID));
              } catch (Exception e) {
              }
              if (interestedPieceID == -1) {
                try {
                  socketOutput.writeBytes(1 + "#" + 3 + "#" + "-1\n");
                } catch (IOException ex) {
                  Logger.getLogger(SocketThread.class.getName()).log(Level.SEVERE, null, ex);
                }
              } else {
                try {
                  socketOutput.writeBytes(1 + "#" + 2 + "#" + interestedPieceID + "\n");
                } catch (IOException ex) {
                  Logger.getLogger(SocketThread.class.getName()).log(Level.SEVERE, null, ex);
                }

                try {
                  while (socketInput.ready() == false) {}

                } catch (Exception e) {
                }
                try {
                  MsgFromOtherPeer = socketInput.readLine() + "";
                } catch (IOException ex) {
                  Logger.getLogger(SocketThread.class.getName()).log(Level.SEVERE, null, ex);
                }
                RecvdMsg = MsgFromOtherPeer.split("#");
                msgLen = Integer.parseInt(RecvdMsg[0]);
                int indexFromOther = msgLen;
                msgType = Integer.parseInt(RecvdMsg[1]);
                msgPayload = RecvdMsg[2];
                int loadsize = Integer.parseInt(msgPayload);
                byte[] mspiececon = new byte[loadsize];
                if (msgType == 7) {
                  socketOutput.writeBytes(channelClearMsg);

                  while (pieceReader.available() <= 0) {}
                  int totallen = 0;
                  byte[] c = new byte[1];
                  while (totallen < loadsize) {
                    try {
                      pieceReader.read(c);
                    } catch (Exception e) {
                    }
                    mspiececon[totallen] = c[0];
                    totallen++;
                  }
                  socketOutput.writeBytes(channelClearMsg);
                  try {

                    peerProcess.writebackPiece(indexFromOther, mspiececon);
                    peerProcess.logfile.write(
                        LogWriter.logmsg(
                            8,
                            peerProcess.myPeerID,
                            OtherPeerID,
                            interestedPieceID,
                            peerProcess
                                .Peers
                                .elementAt(peerProcess.myPeerID % 1000 - 1)
                                .hasHowManyPieces()));

                    if (peerProcess
                            .Peers
                            .elementAt(peerProcess.myPeerID % peerIDBase - 1)
                            .obtainAll()
                        == true) {
                      try {
                        peerProcess.logfile.write(LogWriter.logmsg(9, peerProcess.myPeerID));
                      } catch (Exception e) {
                      }
                    }
                  } catch (IOException ex) {
                    Logger.getLogger(SocketThread.class.getName()).log(Level.SEVERE, null, ex);
                  }
                  continue;
                } else {
                  System.out.println("received msg type " + msgType + ", expecting piece data.");
                }
              }

            } else {
              System.out.println("received msg type " + msgType + ", expecting have msg.");
            }
          } else {
            System.out.println("received msg type " + msgType + ", expecting choke/unchoke msg.");
          }
        }
      }
    } catch (Exception ite) {
    }

    if (quited == false) {

      try {
        socketOutput.writeBytes(peerExitMsg);
        System.out.println(
            "Peer " + peerProcess.myPeerID + " sent quit message to peer " + OtherPeerID + ".");
        try {
          Thread.sleep(100);
        } catch (Exception e) {
        }
      } catch (IOException ex) {
        Logger.getLogger(SocketThread.class.getName()).log(Level.SEVERE, null, ex);
      }
    }

    System.out.println(
        "The socket between peer"
            + peerProcess.myPeerID
            + "and peer "
            + OtherPeerID
            + " will disconnect now.\n");
    peerProcess.Peers.elementAt(OtherPeerID % 1000 - 1).setHasAllPieces();
    peerProcess.Peers.elementAt(peerProcess.myPeerID % 1000 - 1).setHasAllPieces();
  }
Exemplo n.º 18
0
  public void runExample(String deviceName) {
    info("Analog voice resource: " + deviceName);
    try {

      // Open the device.  All access to dialogic hardware is done through
      // "device handles".
      info("Opening " + deviceName);
      int dev = dx.open(deviceName, 0);

      // Make sure that the device is "onhook" for at least 2 seconds.  It
      // is always possible that the line is off hook right now.  Never assume
      // that the device is in any particular state.  Closing a device does
      // not reset it in any way.
      info("Setting ONHOOK to make sure no calls are active.");
      dx.sethook(dev, dx.DX_ONHOOK, dx.EV_SYNC);
      try {
        Thread.sleep(2000);
      } catch (Exception ignore) {
      }

      // Each device name "dxxxB1C1" is actually two devices in one.
      // 1) A voice resource.  This resource does the dialing, DTMF detection,
      //    etc etc etc.  While it is named "dxxxB1C1", it's better to think
      //    of it as a virtual resource.  It can be used in conjunction with
      //    any "real" interface.
      // 2) A real interface - in analog loop-start cases, this is an actual
      //    RJ-11 port.  While we commonly use the "matching" voice and analog
      //    pairs together, that is not actually forced.
      // Just like we won't assume that the device is ONHOOK right now, we
      // won't assume that the voice resource and the analog network resource
      // can "hear" eachother.  Make them listen to eachother now
      info("Routing analog and voice resources to eachother.");
      dx.listen(dev, ag.getxmitslot(dev));
      ag.listen(dev, dx.getxmitslot(dev));

      // Wait for the line to ring once, then automatically go OFFHOOK.
      info("Waiting for a call to come in.");
      dx.wtring(dev, 1, dx.DX_OFFHOOK, -1);

      // DV_TPT tells the API "how, when and why" a certain operation
      // should terminate.  In this case, we're going to give two
      // termination clauses.
      DV_TPT tpt[] = DV_TPT.newArray(2);
      // 1) Stop recording if 60 seconds has elapsed.  I like to always
      // 		put in MAXTIME conditions because you can never trust that
      // 		the person is going to stay on the line and press they key.
      // 		If they hang up, the API will never "hear" a keypress, and
      // 		will go on, and on, and on, recording whatever it hears.
      tpt[0].tp_type = dx.IO_CONT;
      tpt[0].tp_termno = dx.DX_MAXTIME;
      tpt[0].tp_length = 600;
      tpt[0].tp_flags = dx.TF_MAXTIME;
      // 2) Stop recording when we hear 1 DTMF key.  This is the user's
      // 		method of stopping the recording.
      tpt[1].tp_type = dx.IO_EOT;
      tpt[1].tp_termno = dx.DX_MAXDTMF;
      tpt[1].tp_length = 1;
      tpt[1].tp_flags = dx.TF_MAXDTMF;

      // Start the recording.  RM_TONE tells Dialogic to "beep" before
      // the recording starts - a convenient way to let the user know
      // that something usefull is going on.
      info("Recording...");
      dx.recf(dev, "Example2.vox", tpt, dx.RM_TONE | dx.EV_SYNC);

      // The recording has finished, but we don't know _why_ it finished.
      // ATDX_TERMMSK will tell us the "cause of termination".  Note that
      // several termination conditions can be true at the same time, so
      // we have to check for bit-masks, not equality.
      long termmsk = dx.ATDX_TERMMSK(dev);
      // The function "completed normally".
      if ((termmsk & dx.TM_NORMTERM) == dx.TM_NORMTERM) {
        info("dx.TM_NORMTERM");
      }
      // They user pressed a key.
      if ((termmsk & dx.TM_MAXDTMF) == dx.TM_MAXDTMF) {
        info("dx.TM_MAXDTMF");
      }
      // The maximum amount of time as elapsed.
      if ((termmsk & dx.TM_MAXTIME) == dx.TM_MAXTIME) {
        info("dx.TM_MAXTIME");
      }
      // This happens if another thread has called dx.stopch() on the
      // device handle that was doing the recording.
      if ((termmsk & dx.TM_USRSTOP) == dx.TM_USRSTOP) {
        info("dx.TM_USRSTOP");
      }
      // An error occurred.
      if ((termmsk & dx.TM_ERROR) == dx.TM_ERROR) {
        info("dx.TM_ERROR");
      }
      // The first of the termination types should not be applicable in
      // this condition, but are listed for completeness.
      if ((termmsk & dx.TM_MAXSIL) == dx.TM_MAXSIL) {
        info("dx.TM_MAXSIL");
      }
      if ((termmsk & dx.TM_MAXNOSIL) == dx.TM_MAXNOSIL) {
        info("dx.TM_MAXNOSIL");
      }
      if ((termmsk & dx.TM_LCOFF) == dx.TM_LCOFF) {
        info("dx.TM_LCOFF");
      }
      if ((termmsk & dx.TM_IDDTIME) == dx.TM_IDDTIME) {
        info("dx.TM_IDDTIME");
      }
      if ((termmsk & dx.TM_DIGIT) == dx.TM_DIGIT) {
        info("dx.TM_DIGIT");
      }
      if ((termmsk & dx.TM_PATTERN) == dx.TM_PATTERN) {
        info("dx.TM_PATTERN");
      }
      if ((termmsk & dx.TM_EOD) == dx.TM_EOD) {
        info("dx.TM_EOD");
      }
      if ((termmsk & dx.TM_TONE) == dx.TM_TONE) {
        info("dx.TM_TONE");
      }

      // Only do the playback if the termination condition was DTMF.
      // Otherwise we have to assume that the caller has hung up.
      if ((termmsk & dx.TM_MAXDTMF) == dx.TM_MAXDTMF) {

        // When a user hits a DTMF key, only the fact that the key
        // was pressed is reported.  *What* key is actually in the
        // voice resources digit buffer.  In this example, we don't
        // care _what_ key was pressed.  However, before we can start
        // another I/O operation that has a DTMF termination clause,
        // we have to clear the buffer.  Otherwise, it will terminate
        // *right away*.
        dx.clrdigbuf(dev);

        // Play back the same file.  Since recf() uses the default
        // VOX file format, we do not need to specify a DX_XPB
        // structure.
        //
        // We will also give the user the option to terminate the
        // playback early by again hitting a DTMF key.
        tpt = DV_TPT.newArray(1);
        tpt[0].tp_type = dx.IO_EOT;
        tpt[0].tp_termno = dx.DX_MAXDTMF;
        tpt[0].tp_length = 1;
        tpt[0].tp_flags = dx.TF_MAXDTMF;
        info("Playing...");
        dx.playvox(dev, "Example2.vox", tpt, null, dx.EV_SYNC);

        // And again, check the reason for function termination.
        termmsk = dx.ATDX_TERMMSK(dev);
        // The function "completed normally".
        if ((termmsk & dx.TM_NORMTERM) == dx.TM_NORMTERM) {
          info("dx.TM_NORMTERM");
        }
        // EOD means "end of data".  For playvox(), that means
        // "the whole file was played".
        if ((termmsk & dx.TM_EOD) == dx.TM_EOD) {
          info("dx.TM_EOD");
        }
        // They user pressed a key.
        if ((termmsk & dx.TM_MAXDTMF) == dx.TM_MAXDTMF) {
          info("dx.TM_MAXDTMF");
        }
        // This happens if another thread has called dx.stopch() on the
        // device handle that was doing the recording.
        if ((termmsk & dx.TM_USRSTOP) == dx.TM_USRSTOP) {
          info("dx.TM_USRSTOP");
        }
        // An error occurred.
        if ((termmsk & dx.TM_ERROR) == dx.TM_ERROR) {
          info("dx.TM_ERROR");
        }
        // The first of the termination types should not be applicable in
        // this condition, but are listed for completeness.
        if ((termmsk & dx.TM_MAXTIME) == dx.TM_MAXTIME) {
          info("dx.TM_MAXTIME");
        }
        if ((termmsk & dx.TM_MAXSIL) == dx.TM_MAXSIL) {
          info("dx.TM_MAXSIL");
        }
        if ((termmsk & dx.TM_MAXNOSIL) == dx.TM_MAXNOSIL) {
          info("dx.TM_MAXNOSIL");
        }
        if ((termmsk & dx.TM_LCOFF) == dx.TM_LCOFF) {
          info("dx.TM_LCOFF");
        }
        if ((termmsk & dx.TM_IDDTIME) == dx.TM_IDDTIME) {
          info("dx.TM_IDDTIME");
        }
        if ((termmsk & dx.TM_DIGIT) == dx.TM_DIGIT) {
          info("dx.TM_DIGIT");
        }
        if ((termmsk & dx.TM_PATTERN) == dx.TM_PATTERN) {
          info("dx.TM_PATTERN");
        }
        if ((termmsk & dx.TM_TONE) == dx.TM_TONE) {
          info("dx.TM_TONE");
        }
      }

      // Whether or not the call was connected or not, we have to hang up manually.
      // Also, guarantee that we _stay_ hung up for at least 2 seconds.  That way,
      // if some other code goes OFFHOOK, we don't accidentally hook flash.
      info("Hanging up");
      dx.sethook(dev, dx.DX_ONHOOK, dx.EV_SYNC);
      try {
        Thread.sleep(2000);
      } catch (Exception ignore) {
      }

      // We are done with the device, so close it.
      info("Closing");
      dx.close(dev);

      info("Done");
    } catch (JVRException e) {
      logger.throwing(Example1.class.getName(), "runExample", e);
    }
  }
  public boolean execute() {
    if (CDState.getCycle() % period != 0) return false;

    MycoCast mycocast = (MycoCast) Network.get(0).getProtocol(mycocastPid);

    int bio = mycocast.countBiomass();
    int ext = mycocast.countExtending();
    int bra = mycocast.countBranching();
    int imm = mycocast.countImmobile();

    // Update vertices
    Set<MycoNode> activeNodes = new HashSet<MycoNode>();
    for (int i = 0; i < Network.size(); i++) {
      MycoNode n = (MycoNode) Network.get(i);
      activeNodes.add(n);
      HyphaData data = n.getHyphaData();
      // if (data.isBiomass()) { continue; }
      if (graph.containsVertex(n)) {
        graph.removeVertex(n);
      }
      if (!graph.containsVertex(n)) {
        graph.addVertex(n);
      }
    }
    Set<MycoNode> jungNodes = new HashSet<MycoNode>(graph.getVertices());
    jungNodes.removeAll(activeNodes);

    for (MycoNode n : jungNodes) {
      graph.removeVertex(n);
    }

    // Update edges
    for (int i = 0; i < Network.size(); i++) {
      MycoNode n = (MycoNode) Network.get(i);
      HyphaData data = n.getHyphaData();
      HyphaLink link = n.getHyphaLink();

      synchronized (graph) {

        // We now add in all links and tune out display in Visualizer
        java.util.List<MycoNode> neighbors = (java.util.List<MycoNode>) link.getNeighbors();

        //// Adding only links to hypha thins out links to biomass
        //    (java.util.List<MycoNode>) link.getHyphae();

        Collection<MycoNode> jungNeighbors = graph.getNeighbors(n);

        // Remove edges from Jung graph that are not in peersim graph
        for (MycoNode o : jungNeighbors) {
          if (!neighbors.contains(o)) {
            MycoEdge edge = graph.findEdge(n, o);
            while (edge != null) {
              graph.removeEdge(edge);
              edge = graph.findEdge(n, o);
            }
          }
        }

        // Add missing edges to Jung graph that are in peersim graph
        for (MycoNode o : neighbors) {
          if (graph.findEdge(n, o) == null) {
            MycoEdge edge = new MycoEdge();
            graph.addEdge(edge, n, o, EdgeType.DIRECTED);
          }
        }
      }

      // log.finest("VERTICES: " + graph.getVertices());
      // log.finest("EDGES: " + graph.getEdges());
    }

    for (ChangeListener cl : changeListeners) {
      cl.stateChanged(new ChangeEvent(graph));
    }
    if (walking) {
      try {
        Thread.sleep(walkDelay);
      } catch (InterruptedException e) {
      }
      stepBlocked = false;
    }

    try {
      while (stepBlocked && !noBlock) {
        synchronized (JungGraphObserver.class) {
          JungGraphObserver.class.wait();
        }
      }
    } catch (InterruptedException e) {
      stepBlocked = true;
    }
    stepBlocked = true;
    // System.out.println(graph.toString());
    return false;
  }
Exemplo n.º 20
0
  // ----This function gets the raw json from raw_data collection----
  // ---------the function separate the tweets and inserts it to all_tweets collection and calling
  // all other data processing methods-----------
  // ----------------------------------------------------------------------------------------
  public void update_all_tweets(final double num_of_slots, final double max_time_frame_hours)
      throws MongoException, ParseException {
    log4j.info("starting update_all_tweets function");
    String res = new String();
    Integer countelements = 0;
    while (true) {
      // DBCursor cursor = this.collrd.find();
      while (this.collrd.count() < 1) { // no documents to process
        try {
          log4j.info("there is no raw data at the moment, going to sleep for 10 seconds");
          Thread.currentThread();
          Thread.sleep(1000 * 10);
          log4j.info("woke up, continues");
          // cursor = this.collrd.find();
        } catch (InterruptedException e) {
          log4j.error("InterruptedException caught, at update_all_tweets");
          log4j.error(e);
        }
      }
      DBCursor cursor = this.collrd.find(); // get all documents from raw_data collection
      try {
        while (cursor.hasNext()) {
          DBObject currdoc = cursor.next();
          log4j.info("getting a document from the raw data db");
          Object results = currdoc.get("results"); // result - json array of tweets
          try {
            res = results.toString();
          } catch (NullPointerException e) {
            res = "";
          }
          Object obj = JSONValue.parse(res);
          log4j.info("making an array from the jsons tweets");
          JSONArray array = (JSONArray) obj; // make an array of tweets
          // JSONParser parser = new JSONParser();
          try {
            if (res != "") { // if there are tweets
              @SuppressWarnings("rawtypes")
              Iterator iterArray = array.iterator();
              log4j.info("iterating over array tweets");
              try {
                while (iterArray.hasNext()) {
                  Object current = iterArray.next();
                  final DBObject dbObject =
                      (DBObject) JSON.parse(current.toString()); // parse all tweet data to json
                  countelements++;
                  // System.out.println("element number" + countelements.toString());
                  dbObject.put("max_id", currdoc.get("max_id")); // add max_id to tweet data
                  dbObject.put("query", currdoc.get("query")); // add query word to tweet data
                  dbObject.put(
                      "query_time", currdoc.get("query_time")); // add query time to tweet data
                  dbObject.put("query_time_string", currdoc.get("query_time_string"));
                  dbObject.put(
                      "text",
                      "@"
                          + dbObject.get("from_user").toString()
                          + ": "
                          + dbObject.get("text").toString()); // add user_name to beginning of text
                  dbObject.put("count", 1L); // add appearance counter to tweet data
                  log4j.info("inserting tweet id: " + dbObject.get("id").toString());
                  try {
                    DBObject object = new BasicDBObject();
                    object.put(
                        "id", Long.parseLong(dbObject.get("id").toString())); // object to search
                    DBObject newobject = collat.findOne(object);
                    if (newobject != null) {
                      newobject.put(
                          "count",
                          Long.parseLong(newobject.get("count").toString())
                              + 1); // update counter if id already exists
                      collat.update(object, newobject);
                    }
                  } catch (NullPointerException e) {

                  }
                  collat.insert(dbObject);
                  // collrd.findAndRemove(currdoc);
                  // log4j.info("calling function update_search_terms");
                  // final String text = "@" + dbObject.get("from_user").toString() + ": " +
                  // dbObject.get("text").toString();

                  /*Thread t10=new Thread(new Runnable(){
                  	public void run(){
                  		UpdateTweetCounterId(Long.parseLong(dbObject.get("id").toString()));
                  	}
                  });*/

                  Thread t11 =
                      new Thread(
                          new Runnable() {
                            public void run() {
                              update_search_terms(
                                  dbObject.get("text").toString(),
                                  num_of_slots,
                                  max_time_frame_hours,
                                  dbObject.get("query").toString());
                            }
                          });

                  Thread t12 =
                      new Thread(
                          new Runnable() {
                            public void run() {
                              rate_user(
                                  Long.parseLong(dbObject.get("from_user_id").toString()),
                                  dbObject.get("from_user").toString(),
                                  max_time_frame_hours);
                              // UpdateUserRate((long)num_of_slots,slot_time_millis,Long.parseLong(dbObject.get("from_user_id").toString()) , dbObject.get("from_user").toString() ,(long)0);
                            }
                          });

                  Thread t13 =
                      new Thread(
                          new Runnable() {
                            public void run() {
                              String quer = dbObject.get("query").toString();
                              quer = quer.replaceAll("%40", "@");
                              quer = quer.replaceAll("%23", "#");
                              long id =
                                  (long)
                                      (Double.parseDouble(dbObject.get("query_time").toString())
                                          * 1000);
                              String idplus = dbObject.get("id").toString() + "," + id;
                              SearchResultId(quer, idplus);
                            }
                          });
                  // t10.start();
                  t11.start();
                  t12.start();
                  t13.start();
                  try {
                    log4j.info("Waiting for threads to finish.");
                    // t10.join();
                    t11.join();
                    t12.join();
                    t13.join();
                  } catch (InterruptedException e) {
                    log4j.error("Main thread (update_all_tweets) Interrupted");
                  }
                }
              } catch (Exception e) {
                log4j.error(e);
                e.printStackTrace();
              }
            }
          } catch (NullPointerException e) {
            log4j.error(e);
            log4j.info("NullPointerException caught, at update_all_tweets");
          }
          log4j.info("removing processed document from raw_data collection");
          try {
            this.collrd.remove(currdoc);
          } catch (Exception e) {
            log4j.debug(e);
          }
        }
      } catch (MongoException e) {
        log4j.error(e);
      }
    }
  }