List of usage examples for java.net DatagramPacket setAddress
public synchronized void setAddress(InetAddress iaddr)
From source file:org.eredlab.g4.ccl.net.tftp.TFTPDataPacket.java
/*** * This is a method only available within the package for * implementing efficient datagram transport by elminating buffering. * It takes a datagram as an argument, and a byte buffer in which * to store the raw datagram data. Inside the method, the data * is set as the datagram's data and the datagram returned. * <p>// w ww.j a v a 2 s. c o m * @param datagram The datagram to create. * @param data The buffer to store the packet and to use in the datagram. * @return The datagram argument. ***/ DatagramPacket _newDatagram(DatagramPacket datagram, byte[] data) { data[0] = 0; data[1] = (byte) _type; data[2] = (byte) ((_blockNumber & 0xffff) >> 8); data[3] = (byte) (_blockNumber & 0xff); // Doublecheck we're not the same if (data != _data) System.arraycopy(_data, _offset, data, 4, _length); datagram.setAddress(_address); datagram.setPort(_port); datagram.setData(data); datagram.setLength(_length + 4); return datagram; }
From source file:nl.mindef.c2sc.nbs.olsr.pud.uplink.server.distributor.impl.DistributorWorkerImpl.java
@Override @Transactional(readOnly = true)//from w ww . j a va 2 s . c o m public void distribute() { try { this.txChecker.checkInTx("DistributorWorker::distribute"); } catch (Throwable e) { e.printStackTrace(); } long currentTime = System.currentTimeMillis(); if (this.logger.isDebugEnabled()) { this.logger.debug("*** Have to distribute <" + this.lastDistributionTime + ", " + currentTime + "]"); } RelayServer me = this.relayServers.getMe(); List<List<Node>> clusters = this.nodes.getClusters(me); if (clusters != null) { for (List<Node> cluster : clusters) { for (Node clusterLeaderNode : cluster) { Sender clusterLeaderNodeSender = clusterLeaderNode.getSender(); if (clusterLeaderNodeSender == null) { this.logger.debug("Cluster leader " + clusterLeaderNode.getMainIp() + " has no sender: skipped to choose a different one"); continue; } RelayServer clusterLeaderNodeRelayServer = clusterLeaderNodeSender.getRelayServer(); if (clusterLeaderNodeRelayServer != me) { this.logger.debug("Cluster leader " + clusterLeaderNode.getMainIp() + " did not report to me: cluster skipped"); break; } if (this.logger.isDebugEnabled()) { this.logger.debug("*** cluster leader " + clusterLeaderNode.getMainIp().getHostAddress() + " (sender=" + clusterLeaderNodeSender.getIp().getHostAddress() + ":" + clusterLeaderNodeSender.getPort() + ")"); } if ((this.myIPAddresses.isMe(clusterLeaderNodeSender.getIp()) || this.myIPAddresses.isMe(clusterLeaderNode.getMainIp())) && (clusterLeaderNodeSender.getPort().intValue() == this.uplinkUdpPort)) { /* do not distribute to ourselves */ if (this.logger.isDebugEnabled()) { this.logger.debug("this is me: skipping"); } break; } List<PositionUpdateMsg> p4ds = this.positions .getPositionUpdateMsgForDistribution(this.lastDistributionTime, currentTime, cluster); if ((p4ds == null) || (p4ds.size() == 0)) { if (this.logger.isDebugEnabled()) { this.logger.debug("p4ds EMPTY"); } break; } if (this.logger.isDebugEnabled()) { StringBuilder s = new StringBuilder(); s.append("p4ds(" + p4ds.size() + ")="); for (PositionUpdateMsg p4d : p4ds) { s.append(" " + p4d.getId()); } this.logger.debug(s.toString()); } List<DatagramPacket> packets = positionUpdateMsgsToPackets(p4ds); if ((packets != null) && (packets.size() > 0)) { StringBuilder s = new StringBuilder(); if (this.logger.isDebugEnabled()) { s.setLength(0); s.append("tx " + packets.size() + " packet(s) to " + clusterLeaderNode.getMainIp().getHostAddress() + " (sender=" + clusterLeaderNodeSender.getIp().getHostAddress() + ":" + clusterLeaderNodeSender.getPort() + "), sizes="); } for (DatagramPacket packet : packets) { if (this.logger.isDebugEnabled()) { s.append(" " + packet.getLength()); } packet.setAddress(clusterLeaderNodeSender.getIp()); packet.setPort(clusterLeaderNodeSender.getPort().intValue()); try { this.sock.send(packet); } catch (IOException e) { if (this.logger.isDebugEnabled()) { s.append(" ERROR:" + e.getLocalizedMessage()); this.logger.debug(s.toString()); } this.logger.error("Could not send to cluster leader " + clusterLeaderNode.getMainIp().getHostAddress() + " (sender=" + clusterLeaderNodeSender.getIp().getHostAddress() + ":" + clusterLeaderNodeSender.getPort() + ") : " + e.getLocalizedMessage()); } } if (this.logger.isDebugEnabled()) { this.logger.debug(s.toString()); } } break; } } } this.lastDistributionTime = currentTime; }
From source file:org.eredlab.g4.ccl.net.tftp.TFTPRequestPacket.java
/*** * This is a method only available within the package for * implementing efficient datagram transport by elminating buffering. * It takes a datagram as an argument, and a byte buffer in which * to store the raw datagram data. Inside the method, the data * is set as the datagram's data and the datagram returned. * <p>/*ww w. ja v a2 s. c o m*/ * @param datagram The datagram to create. * @param data The buffer to store the packet and to use in the datagram. * @return The datagram argument. ***/ final DatagramPacket _newDatagram(DatagramPacket datagram, byte[] data) { int fileLength, modeLength; fileLength = _filename.length(); modeLength = _modeBytes[_mode].length; data[0] = 0; data[1] = (byte) _type; System.arraycopy(_filename.getBytes(), 0, data, 2, fileLength); data[fileLength + 2] = 0; System.arraycopy(_modeBytes[_mode], 0, data, fileLength + 3, modeLength); datagram.setAddress(_address); datagram.setPort(_port); datagram.setData(data); datagram.setLength(fileLength + modeLength + 3); return datagram; }
From source file:org.restcomm.sbc.media.MediaZone.java
public void sendRtcp(DatagramPacket rdgram) throws IOException { if (rdgram == null) return;//from w w w . j a va 2s.c o m rdgram.setAddress(mediaZonePeer.getOriginalAddress()); rdgram.setPort(mediaZonePeer.getOriginalRtcpPort()); //LOG.trace("--->RTCP("+this.mediaType+", "+this.direction+") LocalProxy "+proxyHost+":"+(proxyPort+1)+"/"+rdgram.getAddress()+":"+rdgram.getPort()+"["+rdgram.getLength()+"]"); //LOG.trace("--->RTCP via socket "+toPrint(rtcpSocket)); rtcpSocket.send(rdgram); }
From source file:org.restcomm.sbc.media.MediaZone.java
public void send(DatagramPacket dgram) throws IOException { if (dgram == null) return;//from w w w .j a va 2 s. c om dgram.setAddress(mediaZonePeer.getOriginalAddress()); dgram.setPort(mediaZonePeer.getOriginalRtpPort()); //dgram.setData(mediaZonePeer.encodeRTP(dgram.getData(), 0, dgram.getLength()), 0, dgram.getLength() ); //LOG.trace("--->("+this.mediaType+", "+this.direction+") LocalProxy "+proxyHost+":"+proxyPort+"/"+dgram.getAddress()+":"+dgram.getPort()+"["+dgram.getLength()+"]"); //LOG.trace("---> via socket "+toPrint(socket)); if (dgram.getData().length > 8) { if (logCounter == rtpCountLog) { RawPacket rtp = new RawPacket(dgram.getData(), 0, dgram.getLength()); LOG.trace("--->[PayloadType " + rtp.getPayloadType() + "](" + this.mediaType + ", " + this.direction + ") LocalProxy " + proxyHost + ":" + proxyPort + "/" + dgram.getAddress() + ":" + dgram.getPort() + "[" + dgram.getLength() + "]"); logCounter = 0; } } else { LOG.warn("--->[PayloadType ?](" + this.mediaType + ", " + this.direction + ") LocalProxy " + proxyHost + ":" + proxyPort + "/" + dgram.getAddress() + ":" + dgram.getPort() + "[" + dgram.getLength() + "]"); } packetsSentCounter++; socket.send(dgram); }
From source file:Reflector.java
public void packetReceived(DatagramPacket packet) { // An incoming packet has been received. Override // the old packet addressing to the new outgoing // addressing, send it and log it. try {//from w ww . jav a 2s. com packet.setAddress(sendAddr); packet.setPort(sendPort); ds.send(packet); logger.log("Packet forwarded to " + packet.getAddress().getHostAddress() + "/" + packet.getPort() + ", " + packet.getLength() + " bytes"); } catch (IOException e) { System.err.println("Error sending packet"); e.printStackTrace(); } }
From source file:cai.flow.collector.Collector.java
/** * UDP//w w w . j av a 2 s. c om * * @param p */ void put_to_queue(final DatagramPacket p) { InetAddress router = p.getAddress(); InetAddress group = (InetAddress) routers.get(router); if (group == null) { logger.warn("A packet from an unauthorized device is ignored. Device: " + router); return; } logger.debug("Packet from device " + router + " is moved to group " + group); p.setAddress(group);// routergroup if (data_queue.size() > max_queue_length) { logger.warn("Reader - the queue is bigger than max_queue_length: " + data_queue.size() + "/" + max_queue_length); } synchronized (data_queue) { data_queue.addLast(p); queued++; if (p == null) data_queue.notifyAll();// else data_queue.notify();// } }
From source file:eu.stratosphere.nephele.discovery.DiscoveryService.java
/** * Server side implementation of Discovery Service. *//*from ww w . ja va 2 s . c om*/ @Override public void run() { final DatagramPacket requestPacket = new DatagramPacket(new byte[64], 64); final Map<Integer, Long> packetIDMap = new HashMap<Integer, Long>(); while (this.isRunning) { try { this.serverSocket.receive(requestPacket); if (!isPacketForUs(requestPacket, this.magicNumber)) { LOG.debug("Received request packet which is not destined to this Nephele setup"); continue; } final Integer packetID = Integer.valueOf(extractPacketID(requestPacket)); if (packetIDMap.containsKey(packetID)) { LOG.debug("Request with ID " + packetID.intValue() + " already answered, discarding..."); continue; } else { final long currentTime = System.currentTimeMillis(); // Remove old entries final Iterator<Map.Entry<Integer, Long>> it = packetIDMap.entrySet().iterator(); while (it.hasNext()) { final Map.Entry<Integer, Long> entry = it.next(); if ((entry.getValue().longValue() + 5000L) < currentTime) { it.remove(); } } packetIDMap.put(packetID, Long.valueOf(currentTime)); } final int packetTypeID = getPacketTypeID(requestPacket); if (packetTypeID == JM_LOOKUP_REQUEST_ID) { LOG.debug("Received job manager lookup request from " + requestPacket.getSocketAddress()); final DatagramPacket responsePacket = createJobManagerLookupReplyPacket(this.ipcPort, this.magicNumber); responsePacket.setAddress(requestPacket.getAddress()); responsePacket.setPort(requestPacket.getPort()); this.serverSocket.send(responsePacket); } else if (packetTypeID == TM_ADDRESS_REQUEST_ID) { LOG.debug("Received task manager address request from " + requestPacket.getSocketAddress()); final DatagramPacket responsePacket = createTaskManagerAddressReplyPacket( requestPacket.getAddress(), this.magicNumber); responsePacket.setAddress(requestPacket.getAddress()); responsePacket.setPort(requestPacket.getPort()); this.serverSocket.send(responsePacket); } else { LOG.debug("Received packet of unknown type " + packetTypeID + ", discarding..."); } } catch (SocketTimeoutException ste) { LOG.debug("Discovery service: socket timeout"); } catch (IOException ioe) { if (this.isRunning) { // Ignore exception when service has been stopped LOG.error("Discovery service stopped working with IOException:\n" + ioe.toString()); } break; } } // Close the socket finally this.serverSocket.close(); }
From source file:nl.mindef.c2sc.nbs.olsr.pud.uplink.server.cluster.impl.RelayClusterSenderImpl.java
private void distribute(List<RelayClusterMessage> relayClusterMessages) { List<RelayServer> otherRelayServers = this.relayServers.getOtherRelayServers(); if ((otherRelayServers == null) || (otherRelayServers.size() == 0)) { return;//from w w w .ja v a2s .com } if (this.logger.isDebugEnabled()) { this.logger.debug("*** distribute " + relayClusterMessages.size() + " packet(s) to " + otherRelayServers.size() + " other relay servers"); } RelayServer me = this.relayServers.getMe(); InetAddress otherRelayServerIp = null; int otherRelayServerPort = -1; DatagramPacket p2s = null; for (RelayClusterMessage relayClusterMessage : relayClusterMessages) { p2s = relayClusterMessage.toWireFormat(); if (p2s == null) { this.logger.error("Could not convert RelayClusterMessage to wire format"); continue; } RelayServer relayServerOriginator = relayClusterMessage.getRelayServer(); for (RelayServer otherRelayServer : otherRelayServers) { otherRelayServerIp = otherRelayServer.getIp(); otherRelayServerPort = otherRelayServer.getPort().intValue(); /* do not send to relay server from which the message came and also not to myself */ if (!(otherRelayServerIp.equals(relayServerOriginator.getIp()) && (otherRelayServerPort == relayServerOriginator.getPort().intValue())) && !(otherRelayServerIp.equals(me.getIp()) && (otherRelayServerPort == this.relayClusterUdpPort))) { p2s.setAddress(otherRelayServerIp); p2s.setPort(otherRelayServerPort); try { if (this.logger.isDebugEnabled()) { this.logger.debug("sending packet to RelayServer " + otherRelayServerIp.getHostAddress() + ":" + otherRelayServerPort); } this.sock.send(p2s); } catch (IOException e) { this.logger.error("Could not send to relay server " + otherRelayServerIp.getHostAddress() + ":" + otherRelayServerPort, e); } } else { if (this.logger.isDebugEnabled()) { this.logger.debug("skip sending packet to RelayServer " + relayServerOriginator.getIp().getHostAddress() + ":" + relayServerOriginator.getPort() + " since the packet came from there, or it's me"); } } } } }
From source file:org.echocat.jomon.net.dns.DnsServer.java
public void serveUDP(InetSocketAddress address) { try {/* w w w. j a v a 2 s .co m*/ final DatagramSocket sock = new DatagramSocket(address.getPort(), address.getAddress()); synchronized (_closeables) { _closeables.add(sock); } final short udpLength = 512; final byte[] in = new byte[udpLength]; final DatagramPacket indp = new DatagramPacket(in, in.length); DatagramPacket outdp = null; while (!currentThread().isInterrupted()) { indp.setLength(in.length); receive(sock, indp); final Message query; byte[] response; try { query = new Message(in); response = generateReply(query, in, indp.getLength(), null); if (response == null) { continue; } } catch (final IOException ignored) { response = formerrMessage(in); } if (outdp == null) { outdp = new DatagramPacket(response, response.length, indp.getAddress(), indp.getPort()); } else { outdp.setData(response); outdp.setLength(response.length); outdp.setAddress(indp.getAddress()); outdp.setPort(indp.getPort()); } sock.send(outdp); } } catch (final InterruptedIOException ignored) { currentThread().interrupt(); } catch (final IOException e) { LOG.warn("serveUDP(" + addrport(address.getAddress(), address.getPort()) + ")", e); } }