View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.transport.multicast;
8   
9   import org.mule.api.MuleContext;
10  import org.mule.api.construct.FlowConstruct;
11  import org.mule.api.endpoint.InboundEndpoint;
12  import org.mule.api.lifecycle.InitialisationException;
13  import org.mule.transport.udp.UdpConnector;
14  
15  /**
16   * <code>MulticastConnector</code> can dispatch mule events using ip multicasting
17   */
18  public class MulticastConnector extends UdpConnector
19  {
20  
21      public static final String MULTICAST = "multicast";
22      private boolean loopback = false;
23      private int timeToLive = INT_VALUE_NOT_SET;
24  
25      public String getProtocol()
26      {
27          return MULTICAST;
28      }
29  
30      public MulticastConnector(MuleContext context)
31      {
32          super(context);
33      }
34  
35      @Override
36      protected void doInitialise() throws InitialisationException
37      {
38          socketFactory = new MulticastSocketFactory();
39          dispatcherSocketsPool.setFactory(socketFactory);
40          dispatcherSocketsPool.setTestOnBorrow(false);
41          dispatcherSocketsPool.setTestOnReturn(true);
42          //For clarity, note that the max active value does not need to be 1 since you can have multiple
43          //Multicast sockets bound to a single port
44          //dispatcherSocketsPool.setMaxActive(1);
45      }
46  
47      public boolean isLoopback()
48      {
49          return loopback;
50      }
51  
52      public void setLoopback(boolean loopback)
53      {
54          this.loopback = loopback;
55      }
56  
57  
58      public int getTimeToLive()
59      {
60          return timeToLive;
61      }
62  
63      public void setTimeToLive(int timeToLive)
64      {
65          this.timeToLive = timeToLive;
66      }
67  
68  
69      @Override
70      protected Object getReceiverKey(FlowConstruct flowConstruct, InboundEndpoint endpoint)
71      {
72          //you can have multiple Multicast sockets bound to a single port,
73          // so store listeners with the service name too
74          return endpoint.getEndpointURI().getAddress() + "/" + flowConstruct.getName();
75      }
76  }