All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.javanut.pronghorn.pipe.PipeMonitor Maven / Gradle / Ivy

Go to download

Ring buffer based queuing utility for applications that require high performance and/or a small footprint. Well suited for embedded and stream based processing.

There is a newer version: 1.1.27
Show newest version
package com.javanut.pronghorn.pipe;

public class PipeMonitor {

	private static Pipe[] targetPipes = new Pipe[0];
	
	public static synchronized > Pipe addMonitor(Pipe sourcePipe) {
		//can only have 1 monitoring pipe, that pipe however could feed a replicator stage
		if (isMonitored(sourcePipe.id)) {
			throw new UnsupportedOperationException("This pipe is already configured to be monitored "+sourcePipe);
		}
		/////////////
		if (targetPipes.length <= sourcePipe.id) {
		   Pipe[] newTargetPipes = new Pipe[sourcePipe.id+1];
		   System.arraycopy(targetPipes, 0, 
				            newTargetPipes, 0, targetPipes.length);
		   targetPipes = newTargetPipes;
		}
		
		//this monitor pipe is the same size as the original
		//NOTE we could make this bigger or smaller in the future as needed ...
		Pipe pipe = new Pipe(sourcePipe.config());
		targetPipes[sourcePipe.id] = pipe;
		pipe.initBuffers();
		return pipe;
	}
	
	
	
	public static > boolean monitor(Pipe sourcePipe,
			                                      long sourceSlabPos, int sourceBlobPos) {
		
		if (isMonitored(sourcePipe.id)) {
			Pipe localTarget = targetPipes[sourcePipe.id];
			
			//will report errors if the logger does not keep this pipe clear 
			//but it also blocks to ensure nothing is ever lost
			Pipe.presumeRoomForWrite(localTarget);			
			
			Pipe.copyFragment(sourcePipe, sourceSlabPos, sourceBlobPos, localTarget);
						
		}
		
		return true;
	}



	public static > boolean isMonitored(Pipe p) {
		return isMonitored(p.id);
	}
	
	private static boolean isMonitored(int id) {
		return id>=0 && id