/* * Remove the channel from the vector. */ private void handleChannelClose(ChannelClose e) { log.debug(":handleChannelClose: Channel " + e.getChannel().getChannelID() + " closed"); // Access to vectors is synchronized channels.remove(new Integer(e.getChannel().getChannelID().hashCode())); try { e.go(); } catch (AppiaEventException ex) { System.err.println("Unexpected exception when " + "forwarding ChannelClose event"); } if (channels.isEmpty()) { // Terminating sockReader.terminate(); Iterator iter = multicastReaders.values().iterator(); while (iter.hasNext()) { UdpSimpleReader reader = (UdpSimpleReader) iter.next(); reader.terminate(); } } }
private boolean newSock(int port, InetAddress addr, ThreadFactory threadFactory) { if (addr == null) { if (param_LOCAL_ADDRESS == null) addr = HostUtils.getLocalAddress(); else addr = param_LOCAL_ADDRESS; } if (port == RegisterSocketEvent.FIRST_AVAILABLE) { /*first available port*/ try { sock = new DatagramSocket(0, addr); } catch (SocketException ex) { ex.printStackTrace(); return false; } } else if (port == RegisterSocketEvent.RANDOMLY_AVAILABLE) { /*chooses a random port*/ Random random = new Random(); boolean sucess = false; /*verifies if the random port is a valid one*/ while (!sucess) { port = Math.abs(random.nextInt() % Short.MAX_VALUE); /* Open Socket with any port*/ try { sock = new DatagramSocket(port, addr); sucess = true; } catch (IllegalArgumentException ex) { } catch (SocketException se) { } } } else { /*Regular RegisterSocketEvent */ /* Open the specified socket (if possible) */ try { sock = new DatagramSocket(port, addr); } /* Socket exception. Possibly the socket is already bound. Return the event up to notify that the command could not be issued. */ catch (SocketException se) { return false; } catch (IllegalArgumentException ex) { return false; } } // Determine local address myAddress = new InetSocketAddress(sock.getLocalAddress(), sock.getLocalPort()); try { sock.setSoTimeout(param_SOTIMEOUT); } catch (SocketException e) { System.err.println( "Unable to set SoTimeout value on UdpSimpleSession. Using default OS value."); e.printStackTrace(); } /* The socket is binded. Launch reader*/ // FIXME sockReader = new UdpSimpleReader(this, sock, myAddress); Thread t = threadFactory.newThread(sockReader, "UdpSimpleReader [" + myAddress + "]"); sockReader.setParentThread(t); t.start(); return true; }
private void handleMulticastInit(MulticastInitEvent e) { log.debug(":handleAppiaMulticastInit"); if (!multicastReaders.containsKey(e.ipMulticast)) { /*creates a multicast socket and binds it to a specific port on the local host machine*/ try { MulticastSocket multicastSock = new MulticastSocket(((InetSocketAddress) e.ipMulticast).getPort()); log.debug(":handleAppiaMulticastInit: Socket Multicast created. Address: " + e.ipMulticast); /*joins a multicast group*/ multicastSock.joinGroup(((InetSocketAddress) e.ipMulticast).getAddress()); // keeping the multicast address... ipMulticast = new InetSocketAddress( ((InetSocketAddress) e.ipMulticast).getAddress(), ((InetSocketAddress) e.ipMulticast).getPort()); log.debug(":handleAppiaMulticastInit: Socket Multicast joined."); try { multicastSock.setSoTimeout(param_SOTIMEOUT); } catch (SocketException se) { System.err.println( "Unable to set SoTimeout value on UdpSimpleSession. Using default OS value."); se.printStackTrace(); } /* The socket is binded. Launch reader and return the event.*/ final UdpSimpleReader multicastReader = new UdpSimpleReader(this, multicastSock, ipMulticast, e.fullDuplex ? null : myAddress); final Thread thread = e.getChannel() .getThreadFactory() .newThread(multicastReader, "MulticastReaderThread [" + ipMulticast + "]"); multicastReader.setParentThread(thread); thread.start(); multicastReaders.put(ipMulticast, multicastReader); /*forwarding the event*/ e.error = false; } catch (IOException ex) { ex.printStackTrace(); System.err.println("Error creating/joining the multicast socket"); e.error = true; } } else { log.debug(":handleAppiaMulticastInit: Requested multicast socket already existed."); } try { e.setDir(Direction.invert(e.getDir())); e.setSource(this); e.init(); e.go(); log.debug(":handleAppiaMulticastInit: Returning multicastInit with error code: " + e.error); log.debug( ":handleAppiaMulticastInit: Direction is " + (e.getDir() == Direction.DOWN ? "DOWN" : "UP")); } catch (AppiaEventException ex) { ex.printStackTrace(); } }