1 24 25 package org.objectweb.tribe.channel.tcp; 26 27 import java.util.HashMap ; 28 29 import org.objectweb.tribe.channel.AbstractReliableFifoChannel; 30 import org.objectweb.tribe.channel.AbstractServerChannel; 31 import org.objectweb.tribe.common.IpAddress; 32 import org.objectweb.tribe.common.log.Trace; 33 import org.objectweb.tribe.exceptions.ChannelException; 34 35 43 public class TcpServerAccepterThread extends Thread 44 { 45 private AbstractServerChannel serverSocket; 46 private HashMap channels; 48 private HashMap readerThreads; 50 private HashMap keyBuffers; 51 private boolean isKilled = false; 52 53 private static Trace logger = Trace 54 .getLogger("org.objectweb.tribe.channel"); 55 56 63 public TcpServerAccepterThread(AbstractServerChannel serverSocket, 64 HashMap channels, HashMap readerThreads, HashMap keyBuffers) 65 { 66 super("TcpServerAccepterThread"); 67 this.serverSocket = serverSocket; 68 this.channels = channels; 69 this.keyBuffers = keyBuffers; 70 this.readerThreads = readerThreads; 71 } 72 73 76 public void run() 77 { 78 if (logger.isDebugEnabled()) 79 logger.debug("TcpServerAccepterThread started"); 80 81 while (!isKilled) 82 { 83 try 84 { 85 AbstractReliableFifoChannel socket = serverSocket.accept(); 86 IpAddress ip = (IpAddress) socket.getSourceAddress(); 87 synchronized (channels) 88 { 89 if (logger.isDebugEnabled()) 90 logger.debug("Accepting new connection from " + ip); 91 channels.put(ip, socket); 93 TcpReaderThread thread = new TcpReaderThread((TcpChannel) socket, 94 keyBuffers); 95 thread.start(); 96 readerThreads.put(socket, thread); 97 } 98 } 99 catch (ChannelException e) 100 { 101 logger.info("Channel error, exiting TcpServerAccepterThread", e); 102 isKilled = true; 103 } 104 } 105 106 if (logger.isDebugEnabled()) 107 logger.debug("TcpServerAccepterThread terminated."); 108 } 109 110 113 public void kill() 114 { 115 isKilled = true; 116 try 117 { 118 serverSocket.close(); 119 } 120 catch (ChannelException ignore) 121 { 122 } 123 } 124 } | Popular Tags |