You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RemoteServer.java 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * Copyright (c) 2006-2013 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.commandline;
  23. import com.dmdirc.ServerManager;
  24. import com.dmdirc.logger.ErrorLevel;
  25. import com.dmdirc.logger.Logger;
  26. import java.net.URI;
  27. import java.rmi.NotBoundException;
  28. import java.rmi.RemoteException;
  29. import java.rmi.registry.LocateRegistry;
  30. import java.rmi.registry.Registry;
  31. import java.rmi.server.UnicastRemoteObject;
  32. import java.util.List;
  33. import javax.inject.Provider;
  34. /**
  35. * An RMI server that allows other clients to interact with DMDirc.
  36. */
  37. public class RemoteServer implements RemoteInterface {
  38. /** The minimum port to use for RMI binding. */
  39. private static final int MINPORT = 3634;
  40. /** The maximum port to use for RMI binding. */
  41. private static final int MAXPORT = MINPORT + 5;
  42. /** Provider for the server manager to use to connect. */
  43. private final Provider<ServerManager> serverManager;
  44. /**
  45. * Crate a new RemoteServer.
  46. *
  47. * @param serverManager Provider of a server manager to use to connect.
  48. */
  49. public RemoteServer(final Provider<ServerManager> serverManager) {
  50. this.serverManager = serverManager;
  51. }
  52. /** {@inheritDoc} */
  53. @Override
  54. public void connect(final List<URI> addresses) throws RemoteException {
  55. for (URI address : addresses) {
  56. serverManager.get().connectToAddress(address);
  57. }
  58. }
  59. /**
  60. * Binds to the RMI registry so that other clients may find this remote
  61. * server.
  62. */
  63. public void bind() {
  64. RemoteInterface stub;
  65. try {
  66. stub = (RemoteInterface) UnicastRemoteObject.exportObject(this, 0);
  67. } catch (RemoteException ex) {
  68. Logger.appError(ErrorLevel.MEDIUM, "Unable to export remote interface", ex);
  69. return;
  70. }
  71. for (int port = MINPORT; port < MAXPORT; port++) {
  72. try {
  73. final Registry registry = LocateRegistry.createRegistry(port);
  74. registry.rebind("DMDirc", stub);
  75. return;
  76. } catch (RemoteException ex) {
  77. continue;
  78. }
  79. }
  80. }
  81. /**
  82. * Retrieves a reference to an existing RemoteServer, if there is one.
  83. * Note that this must be called before bind(), unless you want a reference
  84. * to our own client for some reason.
  85. *
  86. * @return The RemoteServer instance, or null if none was available
  87. */
  88. public static RemoteInterface getServer() {
  89. for (int port = MINPORT; port < MAXPORT; port++) {
  90. try {
  91. final Registry registry = LocateRegistry.getRegistry("localhost", port);
  92. final RemoteInterface iface = (RemoteInterface) registry.lookup("DMDirc");
  93. if (iface == null) {
  94. continue;
  95. } else {
  96. return iface;
  97. }
  98. } catch (RemoteException | NotBoundException ex) {
  99. continue;
  100. }
  101. }
  102. // No RMI server found
  103. return null;
  104. }
  105. }