org.jgroups.protocols.UDP类的使用及代码示例

x33g5p2x  于2022-02-01 转载在 其他  
字(9.3k)|赞(0)|评价(0)|浏览(264)

本文整理了Java中org.jgroups.protocols.UDP类的一些代码示例,展示了UDP类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。UDP类的具体详情如下:
包路径:org.jgroups.protocols.UDP
类名称:UDP

UDP介绍

[英]IP multicast transport based on UDP. Messages to the group (msg.dest == null) will be multicast (to all group members), whereas point-to-point messages (msg.dest != null) will be unicast to a single member. Uses a multicast and a unicast socket.

The following properties are read by the UDP protocol:

  • param mcast_addr - the multicast address to use; default is 228.8.8.8.
  • param mcast_port - (int) the port that the multicast is sent on; default is 7600
  • param ip_mcast - (boolean) flag whether to use IP multicast; default is true.
  • param ip_ttl - the default time-to-live for multicast packets sent out on this socket; default is 8.
  • param use_packet_handler - boolean, defaults to false. If set, the mcast and ucast receiver threads just put the datagram's payload (a byte buffer) into a queue, from where a separate thread will dequeue and handle them (unmarshal and pass up). This frees the receiver threads from having to do message unmarshalling; this time can now be spent receiving packets. If you have lots of retransmissions because of network input buffer overflow, consider setting this property to true.
    [中]基于UDP的IP组播传输。发送到组(msg.dest==null)的消息将多播(发送到所有组成员),而点对点消息(msg.dest!=null)将单播到单个成员。使用多播和单播套接字。
    UDP协议读取以下属性:
    *param mcast_addr——要使用的多播地址;默认值为228.8.8.8。
    *param mcast_port-(int)发送多播的端口;默认值为7600
    *param ip_mcast-(布尔)标志是否使用ip多播;默认是真的。
    *param ip_ttl——此套接字上发送的多播数据包的默认生存时间;默认值为8。
    *param use_packet_handler-boolean,默认为false。如果设置了,mcast和ucast接收器线程只需将数据报的有效负载(字节缓冲区)放入队列中,一个单独的线程将从队列中出列并处理它们(解组和上移)。这使接收方线程不必进行消息解组;这段时间现在可以用来接收数据包。如果由于网络输入缓冲区溢出而有大量重传,请考虑将此属性设置为true。

代码示例

代码示例来源:origin: apache/geode

@Override
public void receive(Address sender, byte[] data, int offset, int length) {
 if (data == null || length <= 0) { // GEODE-1596 - check for empty messages
  return;
 }
 // drop message from self; it has already been looped back up
 // (https://issues.jboss.org/browse/JGRP-1765)
 if (local_physical_addr != null && local_physical_addr.equals(sender))
  return;
 if (length - offset == 4 && data[offset] == 'p' && data[offset + 1] == 'i'
   && data[offset + 2] == 'n' && data[offset + 3] == 'g') {
  // AvailablePort check
  data[offset + 1] = 'o';
  try {
   sendToSingleMember(sender, data, offset, length);
  } catch (Exception e) {
   log.fatal("Unable to respond to available-port check", e);
  }
  return;
 }
 super.receive(sender, data, offset, length);
}

代码示例来源:origin: wildfly/wildfly

public void sendMulticast(byte[] data, int offset, int length) throws Exception {
  if(ip_mcast && mcast_addr != null)
    _send(mcast_addr.getIpAddress(), mcast_addr.getPort(), data, offset, length);
  else
    sendToMembers(members, data, offset, length);
}

代码示例来源:origin: wildfly/wildfly

throw new IllegalArgumentException("bind_addr cannot be null") ;
Util.checkIfValidAddress(bind_addr, getName());
if(log.isDebugEnabled()) log.debug("sockets will use interface " + bind_addr.getHostAddress());
  sock=createMulticastSocketWithBindPort();
else
  sock=createMulticastSocket("jgroups.udp.sock", 0);
setTimeToLive(ip_ttl, sock);
    mcast_sock=Util.createMulticastSocket(getSocketFactory(), "jgroups.udp.mcast_sock", mcast_group_addr, mcast_port, log);
  else
    mcast_sock=getSocketFactory().createMulticastSocket("jgroups.udp.mcast_sock", mcast_port);
    else
      interfaces=Util.getAllAvailableInterfaces();
    bindToInterfaces(interfaces, mcast_sock, mcast_addr.getIpAddress());
      setInterface(bind_addr, mcast_sock); // not strictly needed for receiving, only for sending of mcasts
     mcast_sock.joinGroup(mcast_group_addr);
setBufferSizes();
log.debug("socket information:\n%s", dumpSocketInfo());

代码示例来源:origin: wildfly/wildfly

protected void destroySockets() {
  closeMulticastSocket();
  closeUnicastSocket();
}

代码示例来源:origin: wildfly/wildfly

/**
 * Creates the unicast and multicast sockets and starts the unicast and multicast receiver threads
 */
public void start() throws Exception {
  try {
    createSockets();
    super.start();
  }
  catch(Exception ex) {
    destroySockets();
    throw ex;
  }
  ucast_receivers=createReceivers(unicast_receiver_threads, sock, UCAST_NAME);
  if(ip_mcast)
    mcast_receivers=createReceivers(multicast_receiver_threads, mcast_sock, MCAST_NAME);
}

代码示例来源:origin: org.jgroups/com.springsource.org.jgroups

sock=createDatagramSocketWithBindPort();
  sock=createEphemeralDatagramSocket();
    else
      interfaces=Util.getAllAvailableInterfaces();
    bindToInterfaces(interfaces, mcast_sock, mcast_addr.getIpAddress());
setBufferSizes();
if(log.isDebugEnabled()) log.debug("socket information:\n" + dumpSocketInfo());

代码示例来源:origin: org.jgroups/com.springsource.org.jgroups

/**
 * Creates the unicast and multicast sockets and starts the unicast and multicast receiver threads
 */
public void start() throws Exception {
  if(log.isDebugEnabled()) log.debug("creating sockets and starting threads");
  try {
    createSockets();
  }
  catch(Exception ex) {
    String tmp="problem creating sockets (bind_addr=" + bind_addr + ", mcast_addr=" + mcast_addr + ")";
    throw new Exception(tmp, ex);
  }
  super.start();
  startThreads();
}

代码示例来源:origin: wildfly/wildfly

public static void main(String[] args) throws Exception {
  Protocol[] prot_stack={
   new UDP().setValue("bind_addr", InetAddress.getByName("127.0.0.1")),
   new PING(),
   new MERGE3(),

代码示例来源:origin: wildfly/wildfly

public void sendUnicast(PhysicalAddress dest, byte[] data, int offset, int length) throws Exception {
  _send(((IpAddress)dest).getIpAddress(), ((IpAddress)dest).getPort(), data, offset, length);
}

代码示例来源:origin: org.jgroups/com.springsource.org.jgroups

/**
 * Closed UDP unicast and multicast sockets
 */
void closeSockets() {
  // 1. Close multicast socket
  closeMulticastSocket();
  // 2. Close socket
  closeSocket();
}

代码示例来源:origin: wildfly/wildfly

void setBufferSizes() {
  if(sock != null)
    setBufferSize(sock, ucast_send_buf_size, ucast_recv_buf_size);
  if(mcast_sock != null)
    setBufferSize(mcast_sock, mcast_send_buf_size, mcast_recv_buf_size);
}

代码示例来源:origin: wildfly/wildfly

protected void handleConfigEvent(Map<String,Object> map) {
  boolean set_buffers=false;
  if(map == null) return;
  if(map.containsKey("send_buf_size")) {
    mcast_send_buf_size=(Integer)map.get("send_buf_size");
    ucast_send_buf_size=mcast_send_buf_size;
    set_buffers=true;
  }
  if(map.containsKey("recv_buf_size")) {
    mcast_recv_buf_size=(Integer)map.get("recv_buf_size");
    ucast_recv_buf_size=mcast_recv_buf_size;
    set_buffers=true;
  }
  if(set_buffers)
    setBufferSizes();
}

代码示例来源:origin: wildfly/wildfly

protected void handleConnect() throws Exception {
  startThreads();
}

代码示例来源:origin: org.jgroups/com.springsource.org.jgroups

/**
 * Stops unicast and multicast receiver threads
 */
void stopThreads() {
  Thread tmp;
  // 1. Stop the multicast receiver thread
  if(mcast_receiver != null) {
    if(mcast_receiver.isAlive()) {
      tmp=mcast_receiver;
      mcast_receiver=null;
      closeMulticastSocket();  // will cause the multicast thread to terminate
      tmp.interrupt();
      try {
        tmp.join(Global.THREAD_SHUTDOWN_WAIT_TIME);
      }
      catch(InterruptedException e) {
        Thread.currentThread().interrupt(); // set interrupt flag again
      }
      tmp=null;
    }
    mcast_receiver=null;
  }
  // 2. Stop the unicast receiver thread
  if(ucast_receiver != null) {
    ucast_receiver.stop();
    ucast_receiver=null;
  }
}

代码示例来源:origin: org.infinispan/infinispan-core

public void channelLookupTest() {
 when(mockChannel.getAddress()).thenReturn(a);
 when(mockChannel.down(isA(Event.class))).thenReturn(a);
 when(mockChannel.getView()).thenReturn(v);
 when(mockChannel.getProtocolStack()).thenReturn(ps);
 when(ps.getTransport()).thenReturn(new UDP());
 EmbeddedCacheManager cm = null;
 try {
   GlobalConfigurationBuilder gc = GlobalConfigurationBuilder.defaultClusteredBuilder();
   gc.transport().defaultTransport().addProperty("channelLookup", DummyLookup.class.getName());
   cm = TestCacheManagerFactory.createClusteredCacheManager(gc, new ConfigurationBuilder());
   cm.start();
   cm.getCache();
   GlobalComponentRegistry gcr = TestingUtil.extractGlobalComponentRegistry(cm);
   Transport t = gcr.getComponent(Transport.class);
   assertNotNull(t);
   assertTrue(t instanceof JGroupsTransport);
   assertNotSame(JChannel.class, ((JGroupsTransport) t).getChannel().getClass());
 } finally {
   TestingUtil.killCacheManagers(cm);
 }
}

代码示例来源:origin: org.jboss.eap/wildfly-client-all

/**
 * Creates the unicast and multicast sockets and starts the unicast and multicast receiver threads
 */
public void start() throws Exception {
  try {
    createSockets();
    super.start();
  }
  catch(Exception ex) {
    destroySockets();
    throw ex;
  }
  ucast_receivers=createReceivers(unicast_receiver_threads, sock, UCAST_NAME);
  if(ip_mcast)
    mcast_receivers=createReceivers(multicast_receiver_threads, mcast_sock, MCAST_NAME);
}

代码示例来源:origin: org.jboss.eap/wildfly-client-all

protected void destroySockets() {
  closeMulticastSocket();
  closeUnicastSocket();
}

代码示例来源:origin: org.jboss.eap/wildfly-client-all

public static void main(String[] args) throws Exception {
  Protocol[] prot_stack={
   new UDP().setValue("bind_addr", InetAddress.getByName("127.0.0.1")),
   new PING(),
   new MERGE3(),

代码示例来源:origin: org.jgroups/com.springsource.org.jgroups

public void sendToAllMembers(byte[] data, int offset, int length) throws Exception {
  if(ip_mcast && mcast_addr != null) {
    _send(mcast_addr.getIpAddress(), mcast_addr.getPort(), true, data, offset, length);
  }
  else {
    ArrayList<Address> mbrs=new ArrayList<Address>(members);
    for(Address mbr: mbrs) {
      _send(((IpAddress)mbr).getIpAddress(), ((IpAddress)mbr).getPort(), false, data, offset, length);
    }
  }
}

代码示例来源:origin: org.jgroups/com.springsource.org.jgroups

void setBufferSizes() {
  if(sock != null)
    setBufferSize(sock, ucast_send_buf_size, ucast_recv_buf_size);
  if(mcast_sock != null)
    setBufferSize(mcast_sock, mcast_send_buf_size, mcast_recv_buf_size);
  if(mcast_send_sockets != null) {
    for(int i=0; i < mcast_send_sockets.length; i++) {
      setBufferSize(mcast_send_sockets[i], mcast_send_buf_size, mcast_recv_buf_size);
    }
  }
}

相关文章