org.apache.mina.transport.socket.nio.NioProcessor Java Examples

The following examples show how to use org.apache.mina.transport.socket.nio.NioProcessor. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SlaveHost.java    From neoscada with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Create a new slave host and bind to a single TCP port
 *
 * @param options
 *            optional protocol options
 * @param port
 *            the TCP port to bind to
 */
public SlaveHost ( final ProtocolOptions options, final int port ) throws IOException
{
    this.options = makeOptions ( options );

    this.connector = null;

    this.processor = new SimpleIoProcessorPool<> ( NioProcessor.class );
    final SocketAcceptor nioAcceptor = new NioSocketAcceptor ( this.processor );
    this.acceptor = nioAcceptor;

    nioAcceptor.setReuseAddress ( true );
    nioAcceptor.setBacklog ( 5 );

    this.disposeAcceptor = true;

    setupAcceptor ( null );

    this.acceptor.bind ( new InetSocketAddress ( port ) );
}
 
Example #2
Source File: SimpleIoProcessorPool.java    From jane with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public final void dispose() {
	if (disposing)
		return;

	synchronized (pool) {
		if (disposing)
			return;
		disposing = true;

		for (NioProcessor ioProcessor : pool) {
			if (ioProcessor != null)
				ioProcessor.dispose();
		}

		executor.shutdown();
		Arrays.fill(pool, null);
		disposed = true;
	}
}
 
Example #3
Source File: SimpleIoProcessorPool.java    From jane with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Creates a new instance of SimpleIoProcessorPool with a defined number of IoProcessors in the pool
 *
 * @param size The number of IoProcessor in the pool
 */
public SimpleIoProcessorPool(int size) {
	if (size <= 0)
		throw new IllegalArgumentException("size: " + size + " (expected: positive integer)");

	executor = Executors.newFixedThreadPool(size,
			r -> new Thread(r, NioProcessor.class.getSimpleName() + '-' + idGenerator.incrementAndGet()));

	pool = new NioProcessor[size];

	boolean success = false;
	try {
		for (int i = 0; i < pool.length; i++)
			pool[i] = new NioProcessor(executor);
		success = true;
	} catch (Exception e) {
		throw new RuntimeException(e);
	} finally {
		if (!success)
			dispose();
	}
}
 
Example #4
Source File: Activator.java    From neoscada with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public void start ( final BundleContext context ) throws Exception
{
    Activator.instance = this;

    if ( !Boolean.getBoolean ( "org.eclipse.scada.core.client.ngp.disableSharedProcessor" ) )
    {
        this.processor = new SimpleIoProcessorPool<> ( NioProcessor.class );
    }
    this.factory = new DriverFactoryImpl ( this.processor );

    final Dictionary<String, String> properties = new Hashtable<String, String> ();
    properties.put ( org.eclipse.scada.core.client.DriverFactory.INTERFACE_NAME, "ca" );
    properties.put ( org.eclipse.scada.core.client.DriverFactory.DRIVER_NAME, "ngp" );
    properties.put ( Constants.SERVICE_DESCRIPTION, "Eclipse SCADA CA NGP Adapter" );
    properties.put ( Constants.SERVICE_VENDOR, "Eclipse SCADA Project" );
    this.handle = context.registerService ( org.eclipse.scada.core.client.DriverFactory.class, this.factory, properties );

}
 
Example #5
Source File: Activator.java    From neoscada with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public void start ( final BundleContext context ) throws Exception
{
    Activator.instance = this;

    if ( !Boolean.getBoolean ( "org.eclipse.scada.core.client.ngp.disableSharedProcessor" ) )
    {
        this.processor = new SimpleIoProcessorPool<> ( NioProcessor.class );
    }
    this.factory = new DriverFactoryImpl ( this.processor );

    final Dictionary<String, String> properties = new Hashtable<String, String> ();
    properties.put ( org.eclipse.scada.core.client.DriverFactory.INTERFACE_NAME, "hd" );
    properties.put ( org.eclipse.scada.core.client.DriverFactory.DRIVER_NAME, "ngp" );
    properties.put ( Constants.SERVICE_DESCRIPTION, "Eclipse SCADA HD NGP Adapter" );
    properties.put ( Constants.SERVICE_VENDOR, "Eclipse SCADA Project" );
    this.handle = context.registerService ( org.eclipse.scada.core.client.DriverFactory.class, this.factory, properties );
}
 
Example #6
Source File: Activator.java    From neoscada with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public void start ( final BundleContext context ) throws Exception
{
    Activator.instance = this;

    if ( !Boolean.getBoolean ( "org.eclipse.scada.core.client.ngp.disableSharedProcessor" ) )
    {
        this.processor = new SimpleIoProcessorPool<> ( NioProcessor.class );
    }
    this.factory = new DriverFactoryImpl ( this.processor );

    final Dictionary<String, String> properties = new Hashtable<String, String> ();
    properties.put ( org.eclipse.scada.core.client.DriverFactory.INTERFACE_NAME, "ae" );
    properties.put ( org.eclipse.scada.core.client.DriverFactory.DRIVER_NAME, "ngp" );
    properties.put ( Constants.SERVICE_DESCRIPTION, "Eclipse SCADA AE NGP Adapter" );
    properties.put ( Constants.SERVICE_VENDOR, "Eclipse SCADA Project" );
    this.handle = context.registerService ( org.eclipse.scada.core.client.DriverFactory.class, this.factory, properties );
}
 
Example #7
Source File: ModbusMaster.java    From neoscada with Eclipse Public License 1.0 6 votes vote down vote up
public static ModbusMaster create ( final BundleContext context, final ScheduledExecutorService executor, final String id, final NioProcessor processor, final Map<String, String> parameters ) throws Exception
{
    final ModbusMaster device = new ModbusMaster ( context, id, executor, processor, "ModbusMaster", "modbus" );

    try
    {
        device.configure ( parameters );
    }
    catch ( final Exception e )
    {
        // dispose what was already created
        device.dispose ();
        throw e;
    }

    return device;
}
 
Example #8
Source File: Activator.java    From neoscada with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public void start ( final BundleContext context ) throws Exception
{
    Activator.instance = this;

    if ( !Boolean.getBoolean ( "org.eclipse.scada.core.client.ngp.disableSharedProcessor" ) )
    {
        this.processor = new SimpleIoProcessorPool<> ( NioProcessor.class );
    }
    this.factory = new DriverFactoryImpl ( this.processor );

    final Dictionary<String, String> properties = new Hashtable<String, String> ();
    properties.put ( org.eclipse.scada.core.client.DriverFactory.INTERFACE_NAME, "da" );
    properties.put ( org.eclipse.scada.core.client.DriverFactory.DRIVER_NAME, "ngp" );
    properties.put ( Constants.SERVICE_DESCRIPTION, "Eclipse SCADA DA NGP Adapter" );
    properties.put ( Constants.SERVICE_VENDOR, "Eclipse SCADA Project" );
    this.handle = context.registerService ( org.eclipse.scada.core.client.DriverFactory.class, this.factory, properties );
}
 
Example #9
Source File: DefaultIoFuture.java    From jane with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Check for a deadlock, ie look into the stack trace that we don't have already an instance of the caller.
 */
private void checkDeadLock() {
	// Only read / write / connect / write future can cause dead lock.
	if (!(this instanceof CloseFuture || this instanceof WriteFuture || this instanceof ConnectFuture))
		return;

	// Get the current thread stackTrace.
	// Using Thread.currentThread().getStackTrace() is the best solution,
	// even if slightly less efficient than doing a new Exception().getStackTrace(),
	// as internally, it does exactly the same thing. The advantage of using
	// this solution is that we may benefit some improvement with some future versions of Java.
	StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();

	// Simple and quick check.
	for (StackTraceElement stackElement : stackTrace) {
		if (NioProcessor.class.getName().equals(stackElement.getClassName())) {
			// new IllegalStateException("t").getStackTrace();
			throw new IllegalStateException("DEAD LOCK: " + IoFuture.class.getSimpleName()
					+ ".await() was invoked from an I/O processor thread. Please use "
					+ IoFutureListener.class.getSimpleName() + " or configure a proper thread model alternatively");
		}
	}

	// And then more precisely.
	for (StackTraceElement s : stackTrace) {
		try {
			Class<?> cls = DefaultIoFuture.class.getClassLoader().loadClass(s.getClassName());

			if (IoProcessor.class.isAssignableFrom(cls)) {
				throw new IllegalStateException("DEAD LOCK: " + IoFuture.class.getSimpleName()
						+ ".await() was invoked from an I/O processor thread. Please use "
						+ IoFutureListener.class.getSimpleName() + " or configure a proper thread model alternatively");
			}
		} catch (ClassNotFoundException e) {
		}
	}
}
 
Example #10
Source File: WrapperNioSocketConnector.java    From sailfish-core with Apache License 2.0 5 votes vote down vote up
public WrapperNioSocketConnector(ITaskExecutor taskExecutor) {
       this.taskExecutor = taskExecutor;
	if (taskExecutor != null) {
           Executor executor = taskExecutor.getThreadPool();
		this.ioProcessor = new SimpleIoProcessorPool<NioSession>(NioProcessor.class, executor);
		this.nioSocketConnector = new NioSocketConnector(executor, ioProcessor);
	} else {
		this.ioProcessor = null;
		this.nioSocketConnector = new NioSocketConnector();
	}
}
 
Example #11
Source File: SimpleIoProcessorPool.java    From jane with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Find the processor associated to a session.
 * If it hasn't be stored into the session's attributes, pick a new processor and stores it.
 */
private NioProcessor getProcessor(NioSession session) {
	NioProcessor processor = session.getNioProcessor();
	if (processor == null) {
		if (disposing)
			throw new IllegalStateException(getClass().getSimpleName() + " is disposed");

		processor = pool[(int)((session.getId() & Long.MAX_VALUE) % pool.length)];
		if (processor == null)
			throw new IllegalStateException("null processor in pool");
		session.setNioProcessor(processor);
	}

	return processor;
}
 
Example #12
Source File: AbstractConnectionDevice.java    From neoscada with Eclipse Public License 1.0 5 votes vote down vote up
public AbstractConnectionDevice ( final BundleContext context, final String id, final NioProcessor processor, final ScheduledExecutorService executor, final String itemPrefix )
{
    this.id = id;
    this.processor = processor;
    this.context = context;
    this.itemPrefix = itemPrefix;
    this.executor = executor;
    this.itemFactory = new DataItemFactory ( context, executor, getItemId ( null ) );

    this.stateItem = this.itemFactory.createInput ( "state", Collections.<String, Variant> emptyMap () );
    this.connectionItem = this.itemFactory.createInput ( "connection", Collections.<String, Variant> emptyMap () );
}
 
Example #13
Source File: ExporterFactoryImpl.java    From neoscada with Eclipse Public License 1.0 5 votes vote down vote up
public ExporterFactoryImpl ()
{
    super ( FrameworkUtil.getBundle ( ExporterFactoryImpl.class ).getBundleContext () );

    this.executor = new ScheduledExportedExecutorService ( FrameworkUtil.getBundle ( ExporterFactoryImpl.class ).getSymbolicName (), 1 );

    this.processor = new SimpleIoProcessorPool<> ( NioProcessor.class );

    this.hiveSource = new ServiceListenerHiveSource ( FrameworkUtil.getBundle ( ExporterFactoryImpl.class ).getBundleContext (), this.executor );
    this.hiveSource.open ();

    this.itemPool = new ObjectPoolImpl<> ();
    this.itemPoolHandler = ObjectPoolHelper.registerObjectPool ( FrameworkUtil.getBundle ( ExporterFactoryImpl.class ).getBundleContext (), this.itemPool, DataItem.class );
}
 
Example #14
Source File: SlaveHost.java    From neoscada with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Create a new slave host and bind to a list of socket addresses
 *
 * @param options
 *            optional protocol options
 * @param socketAddresses
 *            a list of socket addresses to bind to.
 *            <em>Note:<em> these socket addresses must be addresses of
 *            local interfaces, not remote addresses.
 */
public SlaveHost ( final ProtocolOptions options, final SlaveHostCustomizer slaveHostCustomizer, final SocketAddress... socketAddresses ) throws IOException
{
    this.options = makeOptions ( options );

    this.connector = null;

    this.processor = new SimpleIoProcessorPool<> ( NioProcessor.class );
    this.acceptor = new NioSocketAcceptor ( this.processor );
    this.disposeAcceptor = true;

    setupAcceptor ( slaveHostCustomizer );

    this.acceptor.bind ( socketAddresses );
}
 
Example #15
Source File: WrapperNioSocketAcceptor.java    From sailfish-core with Apache License 2.0 5 votes vote down vote up
public WrapperNioSocketAcceptor(ITaskExecutor taskExecutor) {
       this.taskExecutor = taskExecutor;
       if (taskExecutor != null) {
           Executor executor = taskExecutor.getThreadPool();
		this.ioProcessor = new SimpleIoProcessorPool<NioSession>(NioProcessor.class, executor);
		this.nioSocketAcceptor = new NioSocketAcceptor(executor, ioProcessor);
	} else {
		this.ioProcessor = null;
		this.nioSocketAcceptor = new NioSocketAcceptor();
	}
}
 
Example #16
Source File: MasterFactory.java    From neoscada with Eclipse Public License 1.0 4 votes vote down vote up
public MasterFactory ( final BundleContext context, final ScheduledExecutorService executor, final NioProcessor processor )
{
    super ( context, true );
    this.executor = executor;
    this.processor = processor;
}
 
Example #17
Source File: AbstractConnectionDevice.java    From neoscada with Eclipse Public License 1.0 4 votes vote down vote up
public AbstractConnectionDevice ( final BundleContext context, final String id, final NioProcessor processor, final String threadPrefix, final String itemPrefix )
{
    this ( context, id, processor, ScheduledExportedExecutorService.newSingleThreadExportedScheduledExecutor ( threadPrefix + "/" + id ), itemPrefix );
    this.createdExecutor = this.executor;
}
 
Example #18
Source File: ModbusMaster.java    From neoscada with Eclipse Public License 1.0 4 votes vote down vote up
public ModbusMaster ( final BundleContext context, final String id, final ScheduledExecutorService executor, final NioProcessor processor, final String threadPrefix, final String itemPrefix )
{
    super ( context, id, processor, executor, itemPrefix );
    this.jobManager = new JobManager ( executor );
}
 
Example #19
Source File: StaticModbusExport.java    From neoscada with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Build a new modbus export instance based on the current builder state
 * <br/>
 * <em>Note:</em> The call is responsible for disposing the created
 * instance using {@link ModbusExport#dispose()}.
 *
 * @return a newly created modbus instance
 */
public ModbusExport build ()
{
    final ScheduledExecutorService executor;
    if ( this.threadFactory == null )
    {
        executor = ScheduledExportedExecutorService.newSingleThreadExportedScheduledExecutor ( "StaticModubusExporter/" + COUNTER.incrementAndGet () );
    }
    else
    {
        executor = new ScheduledExportedExecutorService ( "StaticModubusExporter/" + COUNTER.incrementAndGet (), 1, this.threadFactory );
    }

    boolean disposeProcessor;
    final IoProcessor<NioSession> processor;
    if ( this.processor == null )
    {
        processor = new SimpleIoProcessorPool<> ( NioProcessor.class );
        disposeProcessor = true;
    }
    else
    {
        processor = this.processor;
        disposeProcessor = false;
    }

    final StaticModbusExport result = new StaticModbusExport ( executor, processor, this.hiveSource, null, disposeProcessor );

    try
    {
        result.setProperties ( this.hiveProperies );

        result.setReadTimeout ( this.readTimeout );
        result.setSlaveId ( this.slaveId );
        result.setPort ( this.port );

        // we must call this after "setProperties", since this method creates the block
        result.setBlockConfiguration ( this.definitions );

        return result;
    }
    catch ( final Throwable e )
    {
        result.dispose ();
        throw new RuntimeException ( "Failed to start exporter", e );
    }
}