コード例 #1
0
ファイル: Task1.java プロジェクト: KristinaPupavac/homeworks
  public static void main(String[] args) {
    long time = System.currentTimeMillis();
    queue = new LinkedBlockingQueue<>();
    File file = new File("text.txt");

    try {
      reader = new BufferedReader(new FileReader(file));
      String line = "";
      line = reader.readLine();
      firstLetter = line.charAt(0) + "";
      queue.add(new Task(line));
      while ((line = reader.readLine()) != null) {
        queue.add(new Task(line));
      }

      Worker w = new Worker();
      w.start();

      try {
        w.join();
      } catch (InterruptedException e1) {
        e1.printStackTrace();
      }

      System.out.println("Number of first letters in file: " + letterCounter);
      System.out.println(System.currentTimeMillis() - time + " [ms]");

    } catch (IOException e) {
      e.printStackTrace();
    }
  }
コード例 #2
0
  @Test
  public void testReceiveMessage() {

    ESFEventMessage msg1 = new ESFEventMessage();
    ESFEventMessage msg2 = new ESFEventMessage();

    messageQueue.add(msg1);
    messageQueue.add(msg2);

    messageReceiver.setMessageQueue(messageQueue);
    messageProcessor.setMessageQueue(messageQueue);

    ESFEventMessage msg3 = new ESFEventMessage();
    ESFFetchMissedEventsResponse msg4 = new ESFFetchMissedEventsResponse();
    ESFSubscriptionResponse msg5 = new ESFSubscriptionResponse();
    ESFUnsubscriptionResponse msg6 = new ESFUnsubscriptionResponse();

    messageQueue.add(msg3);
    // messageQueue.add(msg4);
    // messageQueue.add(msg5);
    // messageQueue.add(msg6);

    messageReceiver.receivedMessage(msg3, comm);
    messageReceiver.receivedMessage(msg4, comm);
    messageReceiver.receivedMessage(msg5, comm);
    messageReceiver.receivedMessage(msg6, comm);
  }
コード例 #3
0
  /**
   * Called in case of emergency after deserialization. Deserialization doesn't save listeners
   * because they are transient because listeners are mostly windows which cannot be serialized.
   * After Deserialization we have to find instances of classes implementing IChangesListener and
   * reconstruct listeners list once again to get everything working smooth.
   */
  public synchronized void recoverChangesListeners() {
    if (listeners == null || listeners.size() < 2) {
      listeners = new java.util.concurrent.LinkedBlockingQueue<IChangesListener>();

      listeners.add(VehicleInspector.getInstance());
      listeners.add(BuildingInspector.getInstance());
      listeners.add(DrawPanel.getInstance());
    }
  }
コード例 #4
0
  public void replace(FilterBypass fb, int offset, int length, String str, AttributeSet a)
      throws BadLocationException {

    /* Queue a copy of the event and then modify the text */
    if (length > 0) {
      eventHistory.add(new TextRemoveEvent(offset, length));
    }
    eventHistory.add(new TextInsertEvent(offset, str));
    super.replace(fb, offset, length, str, a);
  }
コード例 #5
0
  /** Returns a new sink when the roller asks for a new one. */
  public synchronized EventSink newWritingSink(final Tagger tagger) throws IOException {
    File dir = getDir(State.WRITING);
    final String tag = tagger.newTag();
    EventSink curSink = new SeqfileEventSink(new File(dir, tag).getAbsoluteFile());
    writingQ.add(tag);
    DFOData data = new DFOData(tag);
    table.put(tag, data);
    writingCount.incrementAndGet();

    return new EventSinkDecorator<EventSink>(curSink) {
      @Override
      public synchronized void append(Event e) throws IOException, InterruptedException {
        synchronized (NaiveFileFailoverManager.this) {
          getSink().append(e);
          writingEvtCount.incrementAndGet();
        }
      }

      @Override
      public synchronized void close() throws IOException, InterruptedException {
        synchronized (NaiveFileFailoverManager.this) {
          super.close();
          if (!writingQ.contains(tag)) {
            LOG.warn("Already changed tag " + tag + " out of WRITING state");
            return;
          }
          LOG.info("File lives in " + getFile(tag));

          changeState(tag, State.WRITING, State.LOGGED);
          loggedCount.incrementAndGet();
        }
      }
    };
  }
  public static void event(Call call, String event, Object data) {
    if (call == null) {
      Log.i(TAG, "Non-call EVENT: %s, %s", event, data);
      return;
    }
    synchronized (mCallEventRecords) {
      if (!mCallEventRecordMap.containsKey(call)) {
        // First remove the oldest entry if no new ones exist.
        if (mCallEventRecords.remainingCapacity() == 0) {
          CallEventRecord record = mCallEventRecords.poll();
          if (record != null) {
            mCallEventRecordMap.remove(record.getCall());
          }
        }

        // Now add a new entry
        CallEventRecord newRecord = new CallEventRecord(call);
        mCallEventRecords.add(newRecord);
        mCallEventRecordMap.put(call, newRecord);
      }

      CallEventRecord record = mCallEventRecordMap.get(call);
      record.addEvent(event, data);
    }
  }
コード例 #7
0
  public void run() {
    long curWindowEnd = 0;

    long now = System.currentTimeMillis();
    curWindowEnd =
        (long) Math.ceil(now / params.timeWindow) * params.timeWindow + params.syncAtMilSec;

    while (isRunning) {
      Emage e = getTheEmage();
      e.endTime = new Date(curWindowEnd);
      e.startTime = new Date(curWindowEnd - params.timeWindow);
      queue.add(e);
      curWindowEnd += params.timeWindow;

      now = System.currentTimeMillis();
      while (now < curWindowEnd) {
        if (!isRunning) break;

        try {

          Thread.sleep(curWindowEnd - now + 1);

          now = System.currentTimeMillis();
        } catch (InterruptedException e1) {
          log.error(e1.getMessage());
        }
      }
    }
  }
コード例 #8
0
  /**
   * Subscribes listener to the list of objects notified when World Changes
   *
   * @param listener IChangesListener to be added to subscribents list
   */
  public void addWorldChangesListener(IChangesListener listener) {
    if (listeners == null) listeners = new LinkedBlockingQueue<IChangesListener>();

    if (!listeners.contains(listener)) {
      listeners.add(listener);
    }
  }
コード例 #9
0
  public ServerThread() {
    pool.submit(new Listener());
    pool.submit(new Listener());
    pool.submit(new Listener());
    pool.submit(new Listener());
    pool.submit(new Listener());
    pool.submit(new Sender());
    pool.submit(new Sender());
    pool.submit(new Sender());
    try {

      server = new ServerSocket(PORT);
      for (; ; ) {
        client = server.accept();
        clients.add(new Client(client));
        Thread job = new Thread(new Listener());
        Thread worker = new Thread(new Sender());
        job.start();
        worker.start();
      }

    } catch (IOException e) {
      e.printStackTrace();
    }
  }
コード例 #10
0
ファイル: FileUtils.java プロジェクト: daulet/Ormekshi
  /*
   * Loads up the queue assuming each URL is on separate line.
   */
  public static int loadQueue(String queueFileName, LinkedBlockingQueue<String> urlQueue) {
    File file = null;
    Scanner scanner = null;
    try {
      file = new File(queueFileName);

      scanner = new Scanner(file);
      while (scanner.hasNextLine()) {
        String line = scanner.nextLine();
        Scanner scanner2 = new Scanner(line);
        while (scanner2.hasNext()) {
          String url = scanner2.next();
          urlQueue.add(url);
        }
      }
    } catch (FileNotFoundException e) {
      e.printStackTrace();
      return -1;
    } finally {
      if (scanner != null) {
        scanner.close();
      }
    }
    return 0;
  }
コード例 #11
0
 public void addChild(Node child) {
   if (viewportAdapter == null) {
     getGraphics3DAdapter().getRenderer().getZUpNode().attachChild(child);
   } else {
     nodesToAddPostFrame.add(child);
   }
 }
コード例 #12
0
 /** Shutdown the processor. */
 public void shutdown() {
   LOG.info("Shutting down");
   finished = true;
   queuedRequests.clear();
   queuedRequests.add(Request.requestOfDeath);
   nextProcessor.shutdown();
 }
コード例 #13
0
  public void insertString(FilterBypass fb, int offset, String str, AttributeSet a)
      throws BadLocationException {

    /* Queue a copy of the event and then modify the textarea */
    eventHistory.add(new TextInsertEvent(offset, str));
    super.insertString(fb, offset, str, a);
  }
コード例 #14
0
  @Override
  public Iterable<Message> getNewMessages(String remoteNodeId) throws RemoteException {
    logger.debug("Requesting the new messages from node [" + remoteNodeId + "]");

    LinkedBlockingQueue<Message> newMessages = new LinkedBlockingQueue<Message>();

    for (MessageContainer messageContainer : broadcastedMessagesQueue) {
      if (!synchronizationTime.containsKey(remoteNodeId)
          || synchronizationTime.get(remoteNodeId) < messageContainer.getTimeStamp()) {
        newMessages.add(messageContainer.getMessage());
        logger.debug(
            "Message [" + messageContainer.getMessage() + "] added to the new messages to send");
      }
    }

    // set the synchronization time of the node
    long timeStamp = new DateTime().getMillis();
    if (synchronizationTime.containsKey(remoteNodeId)) {
      synchronizationTime.replace(remoteNodeId, timeStamp);
      logger.debug("Synchronization time replaced by new one");
    } else {
      synchronizationTime.put(remoteNodeId, timeStamp);
      logger.debug("Synchronization time setted by first time");
    }
    logger.debug("Node [" + remoteNodeId + "] synchronized at [" + timeStamp + "]");

    return newMessages;
  }
コード例 #15
0
ファイル: HttpReader.java プロジェクト: rastabrane/eucalyptus
 private void getResponseToQueue() {
   byte[] bytes = new byte[StorageProperties.TRANSFER_CHUNK_SIZE];
   try {
     httpClient.executeMethod(method);
     InputStream httpIn = method.getResponseBodyAsStream();
     int bytesRead;
     getQueue.add(ObjectStorageDataMessage.StartOfData(0));
     while ((bytesRead = httpIn.read(bytes)) > 0) {
       getQueue.add(ObjectStorageDataMessage.DataMessage(bytes, bytesRead));
     }
     getQueue.add(ObjectStorageDataMessage.EOF());
   } catch (Exception ex) {
     LOG.error(ex, ex);
   } finally {
     method.releaseConnection();
   }
 }
コード例 #16
0
 @Override
 public void processResult(int rc, String path, Object ctx) {
   if (rc != successRc) {
     LOG.error("Error in mutil callback : " + rc);
     exceptions.add(rc);
   }
   tick();
 }
コード例 #17
0
 /**
  * Adds a loaded byte source to the sink queue. The source is merged if specified by the loader.
  */
 MemoryByteSource add(MemoryByteSource source) {
   checkNotNull(source);
   if (loader.isMerge()) {
     source = source.merge();
   }
   queue.add(source);
   return source;
 }
コード例 #18
0
ファイル: DataDistributer.java プロジェクト: robayet/openiot
 private void addListenerToCandidates(DistributionRequest listener) {
   /** Locker variable should be modified EXACTLY like candidateListeners variable. */
   logger.debug("Adding the listener: " + listener.toString() + " to the candidates.");
   DataEnumerator dataEnum = makeDataEnum(listener);
   if (dataEnum.hasMoreElements()) {
     candidateListeners.put(listener, dataEnum);
     locker.add(listener);
   }
 }
コード例 #19
0
ファイル: AsyncStore.java プロジェクト: maasvdberg/infinispan
 private void enqueue(Modification mod) {
   try {
     checkNotStopped();
     if (trace) log.tracef("Enqueuing modification %s", mod);
     changesDeque.add(mod);
   } catch (Exception e) {
     throw new CacheException("Unable to enqueue asynchronous task", e);
   }
 }
コード例 #20
0
ファイル: DnsMain.java プロジェクト: chinahuangxin/prjs
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {
      String host = req.getParameter("host");
      if (!isLegalHost(host)) {
        LOG.error("from=" + req.getRemoteAddr() + "errHost=" + host);
        return;
      }

      String action = req.getParameter("action");
      if ("reload".equals(action)) {
        resp.getWriter().println("old size=" + dns.size() + " reloading...");
        resp.flushBuffer();

        loadIPs();

        resp.getWriter().println("new size=" + dns.size());
        resp.flushBuffer();
        return;
      } else if ("count".equals(action)) {
        resp.getWriter().println("dns=" + dns.size());
        resp.getWriter().println("unknownHosts=" + unknownHosts.size());
        resp.getWriter().println("querying=" + querying.size());
        resp.flushBuffer();
        return;
      } else if ("cleanUnknown".equals(action)) { // ���� unknownhost����
        resp.getWriter().println("unknownHosts=" + unknownHosts.size());
        resp.flushBuffer();
        unknownHosts = new HashMap(1000000);
        return;
      }

      try {
        if (dns.containsKey(host)) {
          resp.getWriter().print(dns.get(host));
          resp.flushBuffer();
          return;
        } else if (unknownHosts.containsKey(host)) {
          resp.getWriter().print(ipUnknown);
          resp.flushBuffer();
          return;
        } else { // ֱ�ӷ��أ�����
          queryQueue.add(host); // doing

          resp.getWriter().print(ipPend); // ��ȷ��
          resp.flushBuffer();
          return;
        }
      } catch (Exception e) {
        LOG.error("from=" + e.toString());

        resp.getWriter().print(ipPend);
        resp.flushBuffer();
        return;
      }
    }
  @Override
  public void updateState(PCEPMessage message, ModuleEnum sourceModule) {
    logger.debug("Entering: updateState(PCEPMessage message, ModuleEnum targetLayer)");
    logger.debug("| message: " + message.contentInformation());
    // Client module should have a Buffer for messages not coming from the network (Client or
    // Computation Module) that shoudl be sent once the
    // State Machine comes to the session up state. This is done so that the client
    // module/computation module does not need to buffer
    // Path Computation Requests or Notification messages before the state machine reaches the
    // session up state

    if ((this.state != 4) && (sourceModule.compareTo(ModuleEnum.NETWORK_MODULE) != 0)) {
      sendingQueue.add(message);
    } else {
      // Process the messages normally

      switch (state) {
        case 0:
          {
            enterIdleState();
            break;
          }
        case 1:
          {
            // enterTCPPendingState();
            logger.info("You should not see me!");
            System.out.println("Message Arrived Before State Was Updated");
            break;
          }
        case 2:
          {
            enterOpenWaitState(message);
            break;
          }
        case 3:
          {
            enterKeepWaitState(message);
            // After the keep wait state, if the connection goes into the session up state
            // the buffer with additional connections should be flushed
            if (state == 4) {
              flushBuffer();
            }
            break;
          }
        case 4:
          {
            enterSessionUPState(message, sourceModule);
            break;
          }
        default:
          {
            logger.info("Lost in a completely not reachable state. wtf?");
          }
      }
    }
  }
コード例 #22
0
ファイル: P2P.java プロジェクト: BlueMeanie/supernode
 private void process(ByteBuffer buffer, int len) {
   if (len > 0) {
     byte[] b = new byte[len];
     System.arraycopy(buffer.array(), 0, b, 0, len);
     reads.add(b);
     if (notListened.tryAcquire()) {
       listen();
     }
   }
 }
コード例 #23
0
ファイル: P2P.java プロジェクト: BlueMeanie/supernode
 public void send(Message m) {
   writes.add(m.toByteArray());
   selectorChanges.add(
       new ChangeRequest(
           channel,
           ChangeRequest.CHANGEOPS,
           SelectionKey.OP_WRITE | SelectionKey.OP_READ,
           null));
   selector.wakeup();
 }
コード例 #24
0
  @Override
  public void broadcast(Message message) throws RemoteException {
    logger.debug("Broadcasting message [" + message + "]");

    // instance the gossip probability with the maximum
    double gossipProbability = 1;
    logger.debug("Start broadcasting with a gossip probability of " + gossipProbability);

    // create the random generator for the gossip probability
    Random rand = new Random();

    for (String nodeId :
        ((ClusterAdministrationImpl)
                nodeManagement.getConnectionManager().getClusterAdmimnistration())
            .getGroupNodes()) {
      if (rand.nextDouble() < gossipProbability) {
        logger.debug("Sending the message to node [" + nodeId + "]");
        if (!nodeManagement
            .getConnectionManager()
            .getConnectionManager(nodeId)
            .getGroupCommunication()
            .getListener()
            .onMessageArrive(message)) {
          // lowering the gossip probability
          gossipProbability =
              gossipProbability
                  - 1
                      / ((ClusterAdministrationImpl)
                              nodeManagement.getConnectionManager().getClusterAdmimnistration())
                          .getGroupNodes()
                          .size();
          logger.debug("Gossip probability lowered to " + gossipProbability);
        } else {
          // set the synchronization time of the node
          long timeStamp = new DateTime().getMillis();
          if (synchronizationTime.containsKey(nodeId)) {
            synchronizationTime.replace(nodeId, timeStamp);
          } else {
            synchronizationTime.put(nodeId, timeStamp);
          }
          logger.debug("Node [" + nodeId + "] synchronized at [" + timeStamp + "]");
        }
      }
    }

    // add the message to the broadcasted messages queue
    try {
      broadcastedMessagesQueue.add(new MessageContainer(message));
    } catch (Exception e) {
      logger.info("Queue full. Error message: " + e.getMessage());
    }
    logger.debug("Message added to the broadcasted messages queue");

    logger.debug("Broadcasting ended");
  }
コード例 #25
0
  @Override
  public void add(RunnableCallback event_fn) {
    if (!this.isRunning()) {
      // throw new RuntimeException(
      // "Cannot add events to a shutdown event manager");

      return;
    }
    added.incrementAndGet();
    queue.add(event_fn);
  }
コード例 #26
0
  @Test
  public void testMessageProcessor() {

    ESFEventMessage msg1 = new ESFEventMessage();
    ESFEventMessage msg2 = new ESFEventMessage();
    messageQueue.add(msg1);
    messageQueue.add(msg2);
    messageProcessor.setMessageQueue(messageQueue);
    Assert.assertTrue(messageProcessor.isWaiting());
    // messageProcessor.run();
    // messageProcessor.stop();
  }
コード例 #27
0
 /** Simply queue the request, which will be processed in FIFO order. */
 public void processRequest(Request request) {
   if (!finished) {
     Request upgradeRequest = null;
     try {
       upgradeRequest = zks.checkUpgradeSession(request);
     } catch (KeeperException ke) {
       if (request.getHdr() != null) {
         request.getHdr().setType(OpCode.error);
         request.setTxn(new ErrorTxn(ke.code().intValue()));
       }
       request.setException(ke);
       LOG.info("Error creating upgrade request", ke);
     } catch (IOException ie) {
       LOG.error("Unexpected error in upgrade", ie);
     }
     if (upgradeRequest != null) {
       queuedRequests.add(upgradeRequest);
     }
     queuedRequests.add(request);
   }
 }
コード例 #28
0
 private void processDelegationTokenRenewerEvent(DelegationTokenRenewerEvent evt) {
   serviceStateLock.readLock().lock();
   try {
     if (isServiceStarted) {
       renewerService.execute(new DelegationTokenRenewerRunnable(evt));
     } else {
       pendingEventQueue.add(evt);
     }
   } finally {
     serviceStateLock.readLock().unlock();
   }
 }
コード例 #29
0
 /** This is a hook that imports external files to the dfo bypassing the default append */
 public synchronized void importData() throws IOException {
   // move all writing into the logged dir.
   for (String fn : importDir.list()) {
     // add to logging queue
     DFOData data = DFOData.recovered(fn);
     synchronized (this) {
       table.put(fn, data);
       loggedQ.add(fn);
       importedCount.incrementAndGet();
     }
   }
 }
コード例 #30
0
  /**
   * Processes a packet to see if it meets the criteria for this packet collector. If so, the packet
   * is added to the result queue.
   *
   * <p>If the result queue's capacity {@link #MAX_PACKETS} is reached, the collector is canceled
   * using the proved {@link CancelHook}.
   *
   * @param packet the packet to process
   * @singleThreaded should only be accessed by the single threaded executer of {@link
   *     de.fu_berlin.inf.dpp.net.business.DispatchThreadContext}
   */
  public void processPacket(Packet packet) {
    if (packet == null) return;

    if (packetFilter == null || packetFilter.accept(packet)) {
      try {
        resultQueue.add(packet);
      } catch (IllegalStateException e) {
        log.warn("Queue has reached capacity, collector canceled.");
        cancel();
      }
    }
  }