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.9KB

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