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

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

本文整理了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

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

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

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

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

  1. throw new IllegalArgumentException("bind_addr cannot be null") ;
  2. Util.checkIfValidAddress(bind_addr, getName());
  3. if(log.isDebugEnabled()) log.debug("sockets will use interface " + bind_addr.getHostAddress());
  4. sock=createMulticastSocketWithBindPort();
  5. else
  6. sock=createMulticastSocket("jgroups.udp.sock", 0);
  7. setTimeToLive(ip_ttl, sock);
  8. mcast_sock=Util.createMulticastSocket(getSocketFactory(), "jgroups.udp.mcast_sock", mcast_group_addr, mcast_port, log);
  9. else
  10. mcast_sock=getSocketFactory().createMulticastSocket("jgroups.udp.mcast_sock", mcast_port);
  11. else
  12. interfaces=Util.getAllAvailableInterfaces();
  13. bindToInterfaces(interfaces, mcast_sock, mcast_addr.getIpAddress());
  14. setInterface(bind_addr, mcast_sock); // not strictly needed for receiving, only for sending of mcasts
  15. mcast_sock.joinGroup(mcast_group_addr);
  16. setBufferSizes();
  17. log.debug("socket information:\n%s", dumpSocketInfo());

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

  1. protected void destroySockets() {
  2. closeMulticastSocket();
  3. closeUnicastSocket();
  4. }

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

  1. /**
  2. * Creates the unicast and multicast sockets and starts the unicast and multicast receiver threads
  3. */
  4. public void start() throws Exception {
  5. try {
  6. createSockets();
  7. super.start();
  8. }
  9. catch(Exception ex) {
  10. destroySockets();
  11. throw ex;
  12. }
  13. ucast_receivers=createReceivers(unicast_receiver_threads, sock, UCAST_NAME);
  14. if(ip_mcast)
  15. mcast_receivers=createReceivers(multicast_receiver_threads, mcast_sock, MCAST_NAME);
  16. }

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

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

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

  1. /**
  2. * Creates the unicast and multicast sockets and starts the unicast and multicast receiver threads
  3. */
  4. public void start() throws Exception {
  5. if(log.isDebugEnabled()) log.debug("creating sockets and starting threads");
  6. try {
  7. createSockets();
  8. }
  9. catch(Exception ex) {
  10. String tmp="problem creating sockets (bind_addr=" + bind_addr + ", mcast_addr=" + mcast_addr + ")";
  11. throw new Exception(tmp, ex);
  12. }
  13. super.start();
  14. startThreads();
  15. }

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

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

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

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

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

  1. /**
  2. * Closed UDP unicast and multicast sockets
  3. */
  4. void closeSockets() {
  5. // 1. Close multicast socket
  6. closeMulticastSocket();
  7. // 2. Close socket
  8. closeSocket();
  9. }

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

  1. void setBufferSizes() {
  2. if(sock != null)
  3. setBufferSize(sock, ucast_send_buf_size, ucast_recv_buf_size);
  4. if(mcast_sock != null)
  5. setBufferSize(mcast_sock, mcast_send_buf_size, mcast_recv_buf_size);
  6. }

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

  1. protected void handleConfigEvent(Map<String,Object> map) {
  2. boolean set_buffers=false;
  3. if(map == null) return;
  4. if(map.containsKey("send_buf_size")) {
  5. mcast_send_buf_size=(Integer)map.get("send_buf_size");
  6. ucast_send_buf_size=mcast_send_buf_size;
  7. set_buffers=true;
  8. }
  9. if(map.containsKey("recv_buf_size")) {
  10. mcast_recv_buf_size=(Integer)map.get("recv_buf_size");
  11. ucast_recv_buf_size=mcast_recv_buf_size;
  12. set_buffers=true;
  13. }
  14. if(set_buffers)
  15. setBufferSizes();
  16. }

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

  1. protected void handleConnect() throws Exception {
  2. startThreads();
  3. }

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

  1. /**
  2. * Stops unicast and multicast receiver threads
  3. */
  4. void stopThreads() {
  5. Thread tmp;
  6. // 1. Stop the multicast receiver thread
  7. if(mcast_receiver != null) {
  8. if(mcast_receiver.isAlive()) {
  9. tmp=mcast_receiver;
  10. mcast_receiver=null;
  11. closeMulticastSocket(); // will cause the multicast thread to terminate
  12. tmp.interrupt();
  13. try {
  14. tmp.join(Global.THREAD_SHUTDOWN_WAIT_TIME);
  15. }
  16. catch(InterruptedException e) {
  17. Thread.currentThread().interrupt(); // set interrupt flag again
  18. }
  19. tmp=null;
  20. }
  21. mcast_receiver=null;
  22. }
  23. // 2. Stop the unicast receiver thread
  24. if(ucast_receiver != null) {
  25. ucast_receiver.stop();
  26. ucast_receiver=null;
  27. }
  28. }

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

  1. public void channelLookupTest() {
  2. when(mockChannel.getAddress()).thenReturn(a);
  3. when(mockChannel.down(isA(Event.class))).thenReturn(a);
  4. when(mockChannel.getView()).thenReturn(v);
  5. when(mockChannel.getProtocolStack()).thenReturn(ps);
  6. when(ps.getTransport()).thenReturn(new UDP());
  7. EmbeddedCacheManager cm = null;
  8. try {
  9. GlobalConfigurationBuilder gc = GlobalConfigurationBuilder.defaultClusteredBuilder();
  10. gc.transport().defaultTransport().addProperty("channelLookup", DummyLookup.class.getName());
  11. cm = TestCacheManagerFactory.createClusteredCacheManager(gc, new ConfigurationBuilder());
  12. cm.start();
  13. cm.getCache();
  14. GlobalComponentRegistry gcr = TestingUtil.extractGlobalComponentRegistry(cm);
  15. Transport t = gcr.getComponent(Transport.class);
  16. assertNotNull(t);
  17. assertTrue(t instanceof JGroupsTransport);
  18. assertNotSame(JChannel.class, ((JGroupsTransport) t).getChannel().getClass());
  19. } finally {
  20. TestingUtil.killCacheManagers(cm);
  21. }
  22. }

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

  1. /**
  2. * Creates the unicast and multicast sockets and starts the unicast and multicast receiver threads
  3. */
  4. public void start() throws Exception {
  5. try {
  6. createSockets();
  7. super.start();
  8. }
  9. catch(Exception ex) {
  10. destroySockets();
  11. throw ex;
  12. }
  13. ucast_receivers=createReceivers(unicast_receiver_threads, sock, UCAST_NAME);
  14. if(ip_mcast)
  15. mcast_receivers=createReceivers(multicast_receiver_threads, mcast_sock, MCAST_NAME);
  16. }

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

  1. protected void destroySockets() {
  2. closeMulticastSocket();
  3. closeUnicastSocket();
  4. }

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

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

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

  1. public void sendToAllMembers(byte[] data, int offset, int length) throws Exception {
  2. if(ip_mcast && mcast_addr != null) {
  3. _send(mcast_addr.getIpAddress(), mcast_addr.getPort(), true, data, offset, length);
  4. }
  5. else {
  6. ArrayList<Address> mbrs=new ArrayList<Address>(members);
  7. for(Address mbr: mbrs) {
  8. _send(((IpAddress)mbr).getIpAddress(), ((IpAddress)mbr).getPort(), false, data, offset, length);
  9. }
  10. }
  11. }

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

  1. void setBufferSizes() {
  2. if(sock != null)
  3. setBufferSize(sock, ucast_send_buf_size, ucast_recv_buf_size);
  4. if(mcast_sock != null)
  5. setBufferSize(mcast_sock, mcast_send_buf_size, mcast_recv_buf_size);
  6. if(mcast_send_sockets != null) {
  7. for(int i=0; i < mcast_send_sockets.length; i++) {
  8. setBufferSize(mcast_send_sockets[i], mcast_send_buf_size, mcast_recv_buf_size);
  9. }
  10. }
  11. }

相关文章