private void handleSendable(SendableEvent e) { log.debug(":handleSendable: " + e); if (e.getDir() == Direction.DOWN) { formatAndSend(e); } /* Now that the packet is sent, event follows his way */ if (e.getChannel().isStarted()) { try { e.go(); } catch (AppiaEventException ex) { System.err.println("Event not initialized but tried to be " + "sent in UdpSimpleSession"); } } }
/* Event deserialization. Returns the event or null if something wrong * happened. */ private void receiveFormatSend(DatagramPacket p) { byte[] data = new byte[p.getLength()]; System.arraycopy(p.getData(), p.getOffset(), data, 0, p.getLength()); SendableEvent e = null; Message msg = null; try { /* Extract event class name */ // size of class name int sLength = ParseUtils.byteArrayToInt(data, 0); // the class name String className = new String(data, 4, sLength, "ISO-8859-1"); /* Create event */ Class c = Class.forName(className); if (debugFull) { logReader.debug(":receiveAndFormat: Reader, creating " + className + " event."); } e = (SendableEvent) c.newInstance(); msg = e.getMessage(); msg.setByteArray(data, 4 + sLength, data.length - (4 + sLength)); /* Extract channel hash and put event in it*/ mbuf.len = 4; msg.pop(mbuf); int channelHash = ParseUtils.byteArrayToInt(mbuf.data, mbuf.off); Channel msgChannel = (Channel) parentSession.channels.get(new Integer(channelHash)); /* If channel does not exist, discard message */ if (msgChannel == null) { if (debugFull) logReader.debug( this.getClass().getName() + ": channel does not exist. message will be discarded. " + "hash=" + channelHash + " " + e); return; } if (debugFull) logReader.debug( ":receiveAndFormat: " + msgChannel.getChannelID() + " (" + channelHash + ")"); /* Extract the addresses and put them on the event */ // msg's source, futurely change udpsimple to common InetSocketAddress addr = new InetSocketAddress(p.getAddress(), p.getPort()); e.source = addr; // msg's destination e.dest = dest; // send event e.asyncGo(msgChannel, Direction.UP); } catch (Exception ex) { if (logReader.isDebugEnabled()) { ex.printStackTrace(); logReader.debug( "Exception catched while processing message from " + p.getAddress().getHostName() + ":" + p.getPort() + ". Continued operation."); } } }
private void formatAndSend(SendableEvent e) { /* Event Class name */ try { if (sock == null) { if (!newSock(RegisterSocketEvent.FIRST_AVAILABLE, null, e.getChannel().getThreadFactory())) throw new IOException("Impossible to create new socket."); } Message msg = e.getMessage(); MsgBuffer mbuf = new MsgBuffer(); byte[] eventType = e.getClass().getName().getBytes("ISO-8859-1"); int channelHash = e.getChannel().getChannelID().hashCode(); mbuf.len = 4; msg.push(mbuf); ParseUtils.intToByteArray(channelHash, mbuf.data, mbuf.off); mbuf.len = eventType.length; msg.push(mbuf); System.arraycopy(eventType, 0, mbuf.data, mbuf.off, mbuf.len); mbuf.len = 4; msg.push(mbuf); ParseUtils.intToByteArray(eventType.length, mbuf.data, mbuf.off); if (msg.length() > param_MAX_UDPMSG_SIZE) throw new IOException("Message length to great, may be truncated"); /* Create the packet and send it */ byte[] bytes = msg.toByteArray(); if ((e.dest instanceof AppiaMulticast) && (((AppiaMulticast) e.dest).getMulticastAddress() == null)) { Object[] dests = ((AppiaMulticast) e.dest).getDestinations(); if (dests == null) { System.err.println( "UdpSimpleSession: Destinations field of AppiaMulticast empty. Not sending event " + e); return; } DatagramPacket dp = new DatagramPacket(bytes, bytes.length); for (int i = 0; i < dests.length; i++) { if (dests[i] instanceof InetSocketAddress) { dp.setAddress(((InetSocketAddress) dests[i]).getAddress()); dp.setPort(((InetSocketAddress) dests[i]).getPort()); sock.send(dp); if (debugFull) log.debug( ":formatAndSend: Multicast emulation: " + dp.getLength() + " bytes datagram sent to " + dp.getAddress().getHostAddress() + " (port " + dp.getPort() + ")"); } else log.error("UdpSimpleSession: Wrong destination address type in event " + e); } } else { InetSocketAddress dest = null; if (e.dest instanceof InetSocketAddress) { dest = (InetSocketAddress) e.dest; } else if (e.dest instanceof AppiaMulticast) { Object aux = ((AppiaMulticast) e.dest).getMulticastAddress(); if (aux instanceof InetSocketAddress) { dest = (InetSocketAddress) aux; if (!dest.getAddress().isMulticastAddress()) { System.err.println( "UdpSimpleSession: Not a multicast address in AppiaMulticast of event " + e); return; } } else { System.err.println("UdpSimpleSession: Wrong multicast address type in event " + e); return; } } else { System.err.println("UdpSimpleSession: Wrong destination address type in event " + e); return; } DatagramPacket dp = new DatagramPacket(bytes, bytes.length, dest.getAddress(), dest.getPort()); sock.send(dp); if (debugFull) log.debug( ":formatAndSend: " + dp.getLength() + " bytes datagram sent to " + dp.getAddress().getHostAddress() + " (port " + dp.getPort() + ")"); } } catch (IOException ex) { if (log.isDebugEnabled()) ex.printStackTrace(); /* Couldn't send message to socket. */ try { SendableNotDeliveredEvent snd = new SendableNotDeliveredEvent(e.getChannel(), this, e); snd.go(); log.debug( ":formatAndSend: IOException when sending Datagram to socket. " + "Inserting SendableNotDeliveredEvent in the channel."); } catch (AppiaEventException ex1) { /* Possible exception: Unwanted Event */ ex.printStackTrace(); } } }