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

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