1 package org.testng.internal.remote; 2 3 import org.testng.collections.Maps; 4 import org.testng.remote.ConnectionInfo; 5 6 import java.io.IOException; 7 import java.net.Socket; 8 import java.util.Map; 9 10 11 /** 12 * This class maintains a pool of slaves (represented by sockets). 13 * 14 * @author cbeust 15 */ 16 public class SlavePool { 17 private static SocketLinkedBlockingQueue m_hosts = new SocketLinkedBlockingQueue(); 18 private static Map<Socket, ConnectionInfo> m_connectionInfos = Maps.newHashMap(); 19 addSlaves(Socket[] slaves)20 public void addSlaves(Socket[] slaves) throws IOException { 21 for (Socket s : slaves) { 22 addSlave(s); 23 } 24 } 25 addSlave(Socket s)26 public void addSlave(Socket s) { 27 if( s==null) { 28 return; 29 } 30 ConnectionInfo ci = new ConnectionInfo(); 31 ci.setSocket(s); 32 addSlave(s, ci); 33 } 34 addSlave(Socket s, ConnectionInfo ci)35 private void addSlave(Socket s, ConnectionInfo ci) { 36 m_hosts.add(s); 37 m_connectionInfos.put(s, ci); 38 } 39 getSlave()40 public ConnectionInfo getSlave() { 41 ConnectionInfo result = null; 42 Socket host = null; 43 44 try { 45 host = m_hosts.take(); 46 result = m_connectionInfos.get(host); 47 } 48 catch (InterruptedException handled) { 49 handled.printStackTrace(); 50 Thread.currentThread().interrupt(); 51 } 52 53 return result; 54 } 55 returnSlave(ConnectionInfo slave)56 public void returnSlave(ConnectionInfo slave) throws IOException { 57 m_hosts.add(slave.getSocket()); 58 // ConnectionInfo ci = m_connectionInfos.remove(slave.socket); 59 // ci.oos.close(); 60 // ci.ois.close(); 61 // addSlave(slave.socket); 62 } 63 64 } 65