/subprojects/messaging/src/main/java/org/gradle/messaging/remote/internal/MessagingServices.java

http://github.com/gradle/gradle · Java · 194 lines · 144 code · 20 blank · 30 comment · 0 complexity · 3a7742460ae52804780bbe05f3fc9645 MD5 · raw file

  1. /*
  2. * Copyright 2011 the original author or authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.gradle.messaging.remote.internal;
  17. import org.gradle.internal.CompositeStoppable;
  18. import org.gradle.internal.Stoppable;
  19. import org.gradle.internal.UncheckedException;
  20. import org.gradle.internal.concurrent.DefaultExecutorFactory;
  21. import org.gradle.internal.concurrent.ExecutorFactory;
  22. import org.gradle.internal.id.IdGenerator;
  23. import org.gradle.internal.id.UUIDGenerator;
  24. import org.gradle.internal.service.DefaultServiceRegistry;
  25. import org.gradle.messaging.dispatch.DiscardingFailureHandler;
  26. import org.gradle.messaging.remote.MessagingClient;
  27. import org.gradle.messaging.remote.MessagingServer;
  28. import org.gradle.messaging.remote.internal.inet.*;
  29. import org.gradle.messaging.remote.internal.protocol.DiscoveryMessage;
  30. import org.gradle.messaging.remote.internal.protocol.DiscoveryProtocolSerializer;
  31. import org.slf4j.LoggerFactory;
  32. import java.net.InetAddress;
  33. import java.net.UnknownHostException;
  34. import java.util.UUID;
  35. /**
  36. * A factory for a set of messaging services. Provides the following services:
  37. *
  38. * <ul>
  39. *
  40. * <li>{@link MessagingClient}</li>
  41. *
  42. * <li>{@link MessagingServer}</li>
  43. *
  44. * <li>{@link OutgoingBroadcast}</li>
  45. *
  46. * <li>{@link IncomingBroadcast}</li>
  47. *
  48. * </ul>
  49. */
  50. public class MessagingServices extends DefaultServiceRegistry implements Stoppable {
  51. private final IdGenerator<UUID> idGenerator = new UUIDGenerator();
  52. private final ClassLoader messageClassLoader;
  53. private final String broadcastGroup;
  54. private final SocketInetAddress broadcastAddress;
  55. private DefaultMessagingClient messagingClient;
  56. private DefaultMultiChannelConnector multiChannelConnector;
  57. private TcpIncomingConnector<Message> incomingConnector;
  58. private DefaultExecutorFactory executorFactory;
  59. private DefaultMessagingServer messagingServer;
  60. private DefaultIncomingBroadcast incomingBroadcast;
  61. private AsyncConnectionAdapter<DiscoveryMessage> multicastConnection;
  62. private DefaultOutgoingBroadcast outgoingBroadcast;
  63. public MessagingServices(ClassLoader messageClassLoader) {
  64. this(messageClassLoader, "gradle");
  65. }
  66. public MessagingServices(ClassLoader messageClassLoader, String broadcastGroup) {
  67. this(messageClassLoader, broadcastGroup, defaultBroadcastAddress());
  68. }
  69. public MessagingServices(ClassLoader messageClassLoader, String broadcastGroup, SocketInetAddress broadcastAddress) {
  70. this.messageClassLoader = messageClassLoader;
  71. this.broadcastGroup = broadcastGroup;
  72. this.broadcastAddress = broadcastAddress;
  73. }
  74. private static SocketInetAddress defaultBroadcastAddress() {
  75. try {
  76. return new SocketInetAddress(InetAddress.getByName("233.253.17.122"), 7912);
  77. } catch (UnknownHostException e) {
  78. throw UncheckedException.throwAsUncheckedException(e);
  79. }
  80. }
  81. public void stop() {
  82. close();
  83. }
  84. @Override
  85. public void close() {
  86. CompositeStoppable stoppable = new CompositeStoppable();
  87. stoppable.add(incomingConnector);
  88. stoppable.add(messagingClient);
  89. stoppable.add(messagingServer);
  90. stoppable.add(multiChannelConnector);
  91. stoppable.add(outgoingBroadcast);
  92. stoppable.add(incomingBroadcast);
  93. stoppable.add(multicastConnection);
  94. stoppable.add(executorFactory);
  95. stoppable.stop();
  96. }
  97. protected MessageOriginator createMessageOriginator() {
  98. String hostName = get(InetAddressFactory.class).getHostName();
  99. String nodeName = String.format("%s@%s", System.getProperty("user.name"), hostName);
  100. return new MessageOriginator(idGenerator.generateId(), nodeName);
  101. }
  102. protected ExecutorFactory createExecutorFactory() {
  103. executorFactory = new DefaultExecutorFactory();
  104. return executorFactory;
  105. }
  106. protected InetAddressFactory createInetAddressFactory() {
  107. return new InetAddressFactory();
  108. }
  109. protected OutgoingConnector<Message> createOutgoingConnector() {
  110. return new TcpOutgoingConnector<Message>(
  111. new DefaultMessageSerializer<Message>(
  112. messageClassLoader));
  113. }
  114. protected IncomingConnector<Message> createIncomingConnector() {
  115. incomingConnector = new TcpIncomingConnector<Message>(
  116. get(ExecutorFactory.class),
  117. new DefaultMessageSerializer<Message>(
  118. messageClassLoader),
  119. get(InetAddressFactory.class),
  120. idGenerator);
  121. return incomingConnector;
  122. }
  123. protected MultiChannelConnector createMultiChannelConnector() {
  124. multiChannelConnector = new DefaultMultiChannelConnector(
  125. get(OutgoingConnector.class),
  126. get(IncomingConnector.class),
  127. get(ExecutorFactory.class),
  128. messageClassLoader,
  129. idGenerator);
  130. return multiChannelConnector;
  131. }
  132. protected MessagingClient createMessagingClient() {
  133. messagingClient = new DefaultMessagingClient(
  134. get(MultiChannelConnector.class),
  135. messageClassLoader);
  136. return messagingClient;
  137. }
  138. protected MessagingServer createMessagingServer() {
  139. messagingServer = new DefaultMessagingServer(
  140. get(MultiChannelConnector.class),
  141. messageClassLoader);
  142. return messagingServer;
  143. }
  144. protected IncomingBroadcast createIncomingBroadcast() {
  145. incomingBroadcast = new DefaultIncomingBroadcast(
  146. get(MessageOriginator.class),
  147. broadcastGroup,
  148. get(AsyncConnection.class),
  149. get(IncomingConnector.class),
  150. get(ExecutorFactory.class),
  151. idGenerator,
  152. messageClassLoader);
  153. return incomingBroadcast;
  154. }
  155. protected OutgoingBroadcast createOutgoingBroadcast() {
  156. outgoingBroadcast = new DefaultOutgoingBroadcast(
  157. get(MessageOriginator.class),
  158. broadcastGroup,
  159. get(AsyncConnection.class),
  160. get(OutgoingConnector.class),
  161. get(ExecutorFactory.class),
  162. idGenerator,
  163. messageClassLoader);
  164. return outgoingBroadcast;
  165. }
  166. protected AsyncConnection<DiscoveryMessage> createMulticastConnection() {
  167. MulticastConnection<DiscoveryMessage> connection = new MulticastConnection<DiscoveryMessage>(broadcastAddress, new DiscoveryProtocolSerializer());
  168. multicastConnection = new AsyncConnectionAdapter<DiscoveryMessage>(
  169. connection,
  170. new DiscardingFailureHandler<DiscoveryMessage>(LoggerFactory.getLogger(MulticastConnection.class)),
  171. get(ExecutorFactory.class));
  172. return multicastConnection;
  173. }
  174. }