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 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * Copyright (c) 2006-2011 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. /**
  34. * An RMI server that allows other clients to interact with DMDirc.
  35. *
  36. * @author chris
  37. */
  38. public class RemoteServer implements RemoteInterface {
  39. /** The minimum port to use for RMI binding. */
  40. private static final int MINPORT = 3634;
  41. /** The maximum port to use for RMI binding. */
  42. private static final int MAXPORT = MINPORT + 5;
  43. /** The interface we're exposing. */
  44. private static final RemoteServer SERVER = new RemoteServer();
  45. /** {@inheritDoc} */
  46. @Override
  47. public void connect(final List<URI> addresses) throws RemoteException {
  48. for (URI address : addresses) {
  49. ServerManager.getServerManager().connectToAddress(address);
  50. }
  51. }
  52. /**
  53. * Binds to the RMI registry so that other clients may find this remote
  54. * server.
  55. */
  56. public static void bind() {
  57. RemoteInterface stub;
  58. try {
  59. stub = (RemoteInterface) UnicastRemoteObject.exportObject(SERVER, 0);
  60. } catch (RemoteException ex) {
  61. Logger.appError(ErrorLevel.MEDIUM, "Unable to export remote interface", ex);
  62. return;
  63. }
  64. for (int port = MINPORT; port < MAXPORT; port++) {
  65. try {
  66. final Registry registry = LocateRegistry.createRegistry(port);
  67. registry.rebind("DMDirc", stub);
  68. return;
  69. } catch (RemoteException ex) {
  70. continue;
  71. }
  72. }
  73. }
  74. /**
  75. * Retrieves a reference to an existing RemoteServer, if there is one.
  76. * Note that this must be called before bind(), unless you want a reference
  77. * to our own client for some reason.
  78. *
  79. * @return The RemoteServer instance, or null if none was available
  80. */
  81. public static RemoteInterface getServer() {
  82. for (int port = MINPORT; port < MAXPORT; port++) {
  83. try {
  84. final Registry registry = LocateRegistry.getRegistry("localhost", port);
  85. final RemoteInterface iface = (RemoteInterface) registry.lookup("DMDirc");
  86. if (iface == null) {
  87. continue;
  88. } else {
  89. return iface;
  90. }
  91. } catch (RemoteException ex) {
  92. continue;
  93. } catch (NotBoundException ex) {
  94. continue;
  95. }
  96. }
  97. // No RMI server found
  98. return null;
  99. }
  100. }