Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (C) 2014-2015 Regents of the University of California.

Author(s):
Jeff Thompson <jefft0@remap.ucla.edu> This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. A copy of the GNU Lesser General Public License is in the file COPYING.
 
 
 package net.named_data.jndn.transport;
 
 
 public class UdpTransport extends Transport {
  
A UdpTransport.ConnectionInfo extends Transport.ConnectionInfo to hold the host and port info for the UDP connection.
 
   public static class ConnectionInfo extends Transport.ConnectionInfo {
    
Create a ConnectionInfo with the given host and port.

Parameters:
host The host for the connection.
port The port number for the connection.
 
     public
     ConnectionInfo(String hostint port)
     {
        = host;
        = port;
     }

    
Create a ConnectionInfo with the given host and default port 6363.

Parameters:
host The host for the connection.
 
     public
     ConnectionInfo(String host)
     {
        = host;
        = 6363;
     }

    
Get the host given to the constructor.

Returns:
The host.
 
     public final String
     getHost() { return ; }

    
Get the port given to the constructor.

Returns:
The port number.
 
     public final int
     getPort() { return ; }
 
     private final String host_;
     private final int port_;
   }

  
Determine whether this transport connecting according to connectionInfo is to a node on the current machine. According to http://redmine.named-data.net/issues/2532#note-8, UDP transports are always non-local.

Parameters:
connectionInfo This is ignored.
Returns:
False because UDP transports are always non-local.
 
   public boolean isLocal(Transport.ConnectionInfo connectionInfo) {
     return false;
   }

  
Connect according to the info in ConnectionInfo, and use elementListener.

Parameters:
connectionInfo A UdpTransport.ConnectionInfo.
elementListener The ElementListener must remain valid during the life of this object.
Throws:
java.io.IOException For I/O error.
 
   public void
   connect
     (Transport.ConnectionInfo connectionInfoElementListener elementListener)
    throws IOException
  {
    close();
     = DatagramChannel.open();
      (((ConnectionInfo)connectionInfo).getHost(),
       ((ConnectionInfo)connectionInfo).getPort()));
     = new ElementReader(elementListener);
  }

  
Set data to the host

Parameters:
data The buffer of data to send. This reads from position() to limit(), but does not change the position.
Throws:
java.io.IOException For I/O error.
  public void
  send(ByteBuffer datathrows IOException
  {
    if ( == null)
      throw new IOException
        ("Cannot send because the socket is not open.  Use connect.");
    // Save and restore the position.
    int savePosition = data.position();
    try {
      while(data.hasRemaining())
        .write(data);
    }
    finally {
      data.position(savePosition);
    }
  }

  
Process any data to receive. For each element received, call elementListener.onReceivedElement. This is non-blocking and will return immediately if there is no data to receive. You should normally not call this directly since it is called by Face.processEvents. If you call this from an main event loop, you may want to catch and log/disregard all exceptions.

Throws:
java.io.IOException For I/O error.
net.named_data.jndn.encoding.EncodingException For invalid encoding.
  public void
  {
    if (!getIsConnected())
      return;
    while (true) {
      .position(0);
      int bytesRead = .read();
      if (bytesRead <= 0)
        return;
      .flip();
    }
  }

  
Check if the transport is connected.

Returns:
True if connected.
  public boolean
  {
    if ( == null)
      return false;
    return .isConnected();
  }

  
Close the connection. If not connected, this does nothing.

Throws:
java.io.IOException For I/O error.
  public void
  close() throws IOException
  {
    if ( != null) {
      if (.isConnected())
        .close();
       = null;
    }
  }
  // TODO: This belongs in the socket listener.
New to GrepCode? Check out our FAQ X