001package edu.wpi.first.wpilibj.networktables2.server;
002
003import java.io.*;
004
005import edu.wpi.first.wpilibj.networktables2.stream.*;
006import edu.wpi.first.wpilibj.networktables2.thread.*;
007import edu.wpi.first.wpilibj.networktables2.type.*;
008
009/**
010 * Thread that monitors for incoming connections
011 * 
012 * @author Mitchell
013 *
014 */
015public class ServerIncomingStreamMonitor implements PeriodicRunnable{
016
017        private final IOStreamProvider streamProvider;
018        private final ServerIncomingConnectionListener incomingListener;
019        private final ServerNetworkTableEntryStore entryStore;
020        private final ServerAdapterManager adapterListener;
021
022        private NTThread monitorThread;
023        private NTThreadManager threadManager;
024        private final NetworkTableEntryTypeManager typeManager;
025        
026        /**
027         * Create a new incoming stream monitor
028         * @param streamProvider the stream provider to retrieve streams from
029         * @param entryStore the entry store for the server
030         * @param transactionPool transaction pool for the server
031         * @param incomingListener the listener that is notified of new connections
032         * @param adapterListener the listener that will listen to adapter events
033         * @param threadManager the thread manager used to create the incoming thread and provided to the Connection Adapters
034         */
035        public ServerIncomingStreamMonitor(final IOStreamProvider streamProvider, final ServerNetworkTableEntryStore entryStore,
036                        final ServerIncomingConnectionListener incomingListener,
037                        final ServerAdapterManager adapterListener,
038                        final NetworkTableEntryTypeManager typeManager, final NTThreadManager threadManager) {
039                this.streamProvider = streamProvider;
040                this.entryStore = entryStore;
041                this.incomingListener = incomingListener;
042                this.adapterListener = adapterListener;
043                this.typeManager = typeManager;
044                this.threadManager = threadManager;
045        }
046        
047        /**
048         * Start the monitor thread
049         */
050        public void start(){
051                if(monitorThread!=null)
052                        stop();
053                monitorThread = threadManager.newBlockingPeriodicThread(this, "Server Incoming Stream Monitor Thread");
054        }
055        /**
056         * Stop the monitor thread
057         */
058        public void stop(){
059                if(monitorThread!=null)
060                        monitorThread.stop();
061        }
062        
063        public void run(){
064                IOStream newStream = null;
065                try {
066                        newStream = streamProvider.accept();
067                        if(newStream!=null){
068                                ServerConnectionAdapter connectionAdapter = new ServerConnectionAdapter(newStream, entryStore, entryStore, adapterListener, typeManager, threadManager);
069                                incomingListener.onNewConnection(connectionAdapter);
070                        }
071                } catch (IOException e) {
072                        //could not get a new stream for some reason. ignore and continue
073                }
074        }
075        
076        
077        
078}