001package edu.wpi.first.wpilibj.networktables2.server;
002
003import edu.wpi.first.wpilibj.networktables2.*;
004import edu.wpi.first.wpilibj.networktables2.util.*;
005
006/**
007 * A list of connections that the server currently has
008 * 
009 * @author Mitchell
010 *
011 */
012public class ServerConnectionList implements FlushableOutgoingEntryReceiver, ServerAdapterManager{
013        private List connections = new List();
014        private final Object connectionsLock = new Object();
015        
016        /**
017         * Add a connection to the list
018         * @param connection
019         */
020        public void add(final ServerConnectionAdapter connection){
021                synchronized(connectionsLock){
022                        connections.add(connection);
023                }
024        }
025        
026        
027        public void close(ServerConnectionAdapter connectionAdapter, boolean closeStream) {
028                synchronized(connectionsLock){
029                        if(connections.remove(connectionAdapter)){
030                                System.out.println("Close: "+connectionAdapter);
031                                connectionAdapter.shutdown(closeStream);
032                        }
033                }
034        }
035        /**
036         * close all connections and remove them
037         */
038        public void closeAll() {
039                synchronized(connectionsLock){
040                        while(connections.size()>0){
041                                close((ServerConnectionAdapter)connections.get(0), true);
042                        }
043                }
044        }
045        
046        public void offerOutgoingAssignment(NetworkTableEntry entry) {
047                synchronized(connectionsLock){
048                        for(int i = 0; i<connections.size(); ++i){
049                                ((ServerConnectionAdapter)connections.get(i)).offerOutgoingAssignment(entry);
050                        }
051                }
052        }
053        public void offerOutgoingUpdate(NetworkTableEntry entry) {
054                synchronized(connectionsLock){
055                        for(int i = 0; i<connections.size(); ++i){
056                                ((ServerConnectionAdapter)connections.get(i)).offerOutgoingUpdate(entry);
057                        }
058                }
059        }
060        public void flush() {
061                synchronized(connectionsLock){
062                        for(int i = 0; i<connections.size(); ++i){//TODO iterate over as array
063                                ((ServerConnectionAdapter)connections.get(i)).flush();
064                        }
065                }
066        }
067        public void ensureAlive() {
068                synchronized(connectionsLock){
069                        for(int i = 0; i<connections.size(); ++i){//TODO iterate over as array
070                                ((ServerConnectionAdapter)connections.get(i)).ensureAlive();
071                        }
072                }
073        }
074}