Пример #1
0
 /** @syncpriority 200 */
 public void notifyDeadConnection(ConnectionPair pair) {
   synchronized (this) {
     streamConns.removeConnectionPair(pair);
     if (pair instanceof LCPair) {
       prm.notifyDeadConnection(pair.getCC().getNodeId());
       if (streamConns.getNumListenConns() < mina.getConfig().getMaxSources())
         requestCachedSources();
       bidStrategy.cleanup(pair.getCC().getNodeId());
       // Make note of them in case they come back
       LCPair lcp = (LCPair) pair;
       SourceStatus sourceStat = lcp.getLastSourceStatus();
       if (sourceStat != null) {
         StreamStatus streamStat = lcp.getLastStreamStatus();
         if (streamStat == null) throw new SeekInnerCalmException();
         mina.getSourceMgr().cachePossiblyDeadSource(sourceStat, streamStat);
       }
     }
     if (streamConns.getNumBroadcastConns() == 0 && streamConns.getNumListenConns() == 0) {
       try {
         pageBuf.sleep();
       } catch (IOException e) {
         log.error("Error sleeping page buffer", e);
         stop();
       }
     }
   }
   if (isReceiving()) mina.getEventMgr().fireReceptionConnsChanged(streamId);
 }
Пример #2
0
 private void deadvertiseStream() {
   // Don't deadvertise if we're shutting down
   if (!mina.getCCM().isShuttingDown()) {
     List<String> streamIds = new ArrayList<String>();
     streamIds.add(streamId);
     UnAdvSource uas = UnAdvSource.newBuilder().addAllStreamId(streamIds).build();
     mina.getCCM().sendMessageToNetwork("UnAdvSource", uas);
   }
 }
Пример #3
0
 /**
  * Checks to see if we still want sources, and if we don't, notifies the network
  *
  * @syncpriority 200
  */
 private boolean checkWantingSources() {
   // Don't do anything if we are shutting down, when we disconnect the
   // supernode will figure it out
   if (mina.getCCM().isShuttingDown()) return false;
   boolean wantingSources = false;
   synchronized (this) {
     wantingSources = ((listeners.size() != 0) || receiving);
   }
   if (!wantingSources) mina.getSourceMgr().dontWantSources(streamId);
   return wantingSources;
 }
Пример #4
0
 public Set<String> getSourceNodeIds() {
   Set<String> result = mina.getSourceMgr().getReadyNodeIds(streamId);
   for (ConnectionPair lcp : streamConns.getAllListenConns()) {
     result.add(lcp.getCC().getNodeDescriptor().getId());
   }
   return result;
 }
Пример #5
0
 /** Creates the bid strategy based on the class specified in the config file */
 private void createBidStrategy() {
   try {
     bidStrategy =
         (BidStrategy) Class.forName(mina.getConfig().getBidStrategyClass()).newInstance();
   } catch (Exception e) {
     throw new SeekInnerCalmException(e);
   }
   bidStrategy.setStreamMgr(this);
 }
Пример #6
0
 private void receptionCompleted() {
   // Huzzah
   log.info("Completed reception of " + getStreamId());
   // If we want to become a broadcaster when reception is completed
   // (probable), then there needs to be a receptionlistener that calls
   // startbroadcast in response to this event
   mina.getEventMgr().fireReceptionCompleted(getStreamId());
   stopReception();
 }
Пример #7
0
 /** Don't use this ctor - use SMRegister.getOrCreateCM() instead */
 public StreamMgr(MinaInstance mina, String streamId, PageBuffer pageBuf) {
   this.mina = mina;
   log = mina.getLogger(getClass());
   this.streamId = streamId;
   this.pageBuf = pageBuf;
   streamConns = new StreamConnsMgr(this);
   prm = new PageRequestMgr(this);
   createBidStrategy();
 }
Пример #8
0
 /** @syncpriority 200 */
 public synchronized void startReception() {
   if (receiving) throw new SeekInnerCalmException();
   // If we're already finished, just start rebroadcasting
   if (pageBuf.isComplete()) startRebroadcast();
   else {
     receiving = true;
     requestCachedSources();
     mina.getSourceMgr().wantSources(streamId, tolerateDelay());
   }
 }
Пример #9
0
 /** @syncpriority 200 */
 public synchronized void startBroadcast() {
   if (broadcasting) throw new SeekInnerCalmException();
   log.debug("Starting pagebuffer for " + streamId);
   pageBuf.start();
   broadcasting = true;
   log.info("Starting broadcast of " + pageBuf.getTotalPages() + " pages for " + streamId);
   // If we're already rebroadcasting, this means that we've completed
   // reception and are now a broadcaster - so we don't need to announce
   // ourselves again
   if (rebroadcasting) rebroadcasting = false;
   else mina.getStreamAdvertiser().advertiseStream(streamId);
 }
Пример #10
0
 public StreamStatus buildStreamStatus(String toNodeId) {
   StreamStatus.Builder bldr = StreamStatus.newBuilder();
   if (toNodeId != null) {
     bldr.setToNodeId(toNodeId);
     bldr.setFromNodeId(mina.getMyNodeId());
   }
   bldr.setStreamId(streamId);
   if (pageBuf.getTotalPages() > 0) bldr.setTotalPages(pageBuf.getTotalPages());
   StreamPosition sp = pageBuf.getStreamPosition();
   bldr.setLastContiguousPage(sp.getLastContiguousPage());
   if (sp.getPageMap() > 0) bldr.setPageMap(sp.getPageMap());
   return bldr.build();
 }
Пример #11
0
 /** @syncpriority 200 */
 public synchronized void stopReception() {
   log.info("Stopping reception for stream " + streamId);
   if (!receiving) // Leftover thread
   return;
   receiving = false;
   // If we're still wanting to hear about sources, keep track of the ones we're receiving from
   if (checkWantingSources()) {
     for (LCPair lcp : streamConns.getAllListenConns()) {
       mina.getSourceMgr()
           .cacheSourceUntilReady(lcp.getLastSourceStatus(), lcp.getLastStreamStatus());
     }
   }
   streamConns.closeAllListenConns();
 }
Пример #12
0
 /** @syncpriority 200 */
 public synchronized void stop() {
   log.debug("SM for " + streamId + " stopping");
   if (broadcasting) stopBroadcast();
   if (rebroadcasting) stopRebroadcast();
   if (receiving) stopReception();
   listeners.clear();
   checkWantingSources();
   streamConns.closeAll();
   mina.getSmRegister().unregisterSM(streamId);
   if (pageBuf != null) {
     pageBuf.stop();
     pageBuf.close();
   }
 }
Пример #13
0
 /** @syncpriority 200 */
 public void requestCachedSources() {
   if (!receiving) return;
   Set<SourceStatus> sources = mina.getSourceMgr().getReadySources(streamId);
   for (SourceStatus sourceStat : sources) {
     // This is a bit kludgy - get the streamstatus that applies to us
     StreamStatus streamStat = null;
     for (StreamStatus testSs : sourceStat.getSsList()) {
       if (testSs.getStreamId().equals(streamId)) {
         streamStat = testSs;
         break;
       }
     }
     if (streamStat == null) throw new SeekInnerCalmException();
     foundSource(sourceStat, streamStat);
   }
 }
Пример #14
0
 private void notifyListenersOfSource(final String sourceId) {
   final FoundSourceListener[] lisArr;
   synchronized (this) {
     lisArr = new FoundSourceListener[listeners.size()];
     listeners.toArray(lisArr);
   }
   mina.getExecutor()
       .execute(
           new CatchingRunnable() {
             public void doRun() throws Exception {
               for (FoundSourceListener listener : lisArr) {
                 listener.foundBroadcaster(streamId, sourceId);
               }
             }
           });
 }
Пример #15
0
 /** @syncpriority 160 */
 public int numSources() {
   int result = mina.getSourceMgr().numReadySources(streamId);
   result += streamConns.getNumListenConns();
   return result;
 }
Пример #16
0
 /** @syncpriority 200 */
 public synchronized void abort() {
   streamConns.abort();
   mina.getSmRegister().unregisterSM(streamId);
 }
Пример #17
0
  public void foundSource(SourceStatus sourceStat, StreamStatus streamStat) {
    String fromNodeId = sourceStat.getFromNode().getId();
    String sourceId = fromNodeId;
    if (streamStat.getTotalPages() != 0) {
      long totalPages = streamStat.getTotalPages();
      if (pageBuf == null) this.totalPages = totalPages;
      else {
        if (pageBuf.getTotalPages() <= 0) pageBuf.setTotalPages(totalPages);
        else {
          if (pageBuf.getTotalPages() != totalPages) {
            log.error(
                "Node "
                    + sourceId
                    + " advertised total pages "
                    + totalPages
                    + " for stream "
                    + streamId
                    + ", but I have already recorded "
                    + pageBuf.getTotalPages());
            return;
          }
        }
      }
    }

    if (streamConns.haveListenConnWithId(sourceId)) return;

    // If we're not receiving, then we're just caching sources to pass to an external listener - so
    // cache it
    if (!receiving) {
      mina.getSourceMgr().cacheSourceUntilReady(sourceStat, streamStat);
      notifyListenersOfSource(sourceId);
      return;
    }

    // Not listening to this node - let's see if we should
    if (mina.getConfig().isAgoric()) {
      if (!bidStrategy.worthConnectingTo(new AuctionState(sourceStat.getAuctionState()))) {
        log.debug(
            "Not connecting to node " + sourceId + " for " + streamId + " - bid strategy says no");
        mina.getSourceMgr().cacheSourceUntilAgoricsAcceptable(sourceStat, streamStat);
        return;
      }
    }

    // Check that they have data useful to us
    if (pageBuf != null) {
      StreamPosition sp =
          new StreamPosition(streamStat.getLastContiguousPage(), streamStat.getPageMap());
      if (sp.highestIncludedPage() <= pageBuf.getLastContiguousPage()) {
        // Useless at the moment - cache them and ask again later
        mina.getSourceMgr().cacheSourceUntilDataAvailable(sourceStat, streamStat);
        return;
      }
    }

    // This is a useful source - let our listeners know
    notifyListenersOfSource(sourceId);

    if (streamConns.getNumListenConns() >= mina.getConfig().getMaxSources()) {
      mina.getSourceMgr().cacheSourceUntilReady(sourceStat, streamStat);
      return;
    }
    try {
      streamConns.makeListenConnectionTo(sourceStat);
    } catch (MinaConnectionException e) {
      log.error("Caught MinaConnectionException when attempting to connect to " + sourceId);
    }
  }
Пример #18
0
 /** @syncpriority 200 */
 public synchronized void startRebroadcast() {
   if (rebroadcasting) throw new SeekInnerCalmException();
   rebroadcasting = true;
   log.info("Beginning rebroadcast for stream " + streamId);
   mina.getStreamAdvertiser().advertiseStream(streamId);
 }
Пример #19
0
 /** @syncpriority 200 */
 public synchronized void addFoundSourceListener(FoundSourceListener listener) {
   boolean sendWantSources = (listeners.size() == 0);
   listeners.add(listener);
   if (sendWantSources) mina.getSourceMgr().wantSources(streamId, tolerateDelay());
 }