Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

RemoteServer.java 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * Copyright (c) 2006-2015 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.interfaces.ConnectionManager;
  24. import java.net.URI;
  25. import java.rmi.NotBoundException;
  26. import java.rmi.RemoteException;
  27. import java.rmi.registry.LocateRegistry;
  28. import java.rmi.registry.Registry;
  29. import java.rmi.server.UnicastRemoteObject;
  30. import java.util.List;
  31. import javax.inject.Provider;
  32. /**
  33. * An RMI server that allows other clients to interact with DMDirc.
  34. */
  35. public class RemoteServer implements RemoteInterface {
  36. /** The minimum port to use for RMI binding. */
  37. private static final int MINPORT = 3634;
  38. /** The maximum port to use for RMI binding. */
  39. private static final int MAXPORT = MINPORT + 5;
  40. /** Provider for the server manager to use to connect. */
  41. private final Provider<ConnectionManager> serverManager;
  42. /**
  43. * Crate a new RemoteServer.
  44. *
  45. * @param serverManager Provider of a server manager to use to connect.
  46. */
  47. public RemoteServer(final Provider<ConnectionManager> serverManager) {
  48. this.serverManager = serverManager;
  49. }
  50. @Override
  51. public void connect(final List<URI> addresses) throws RemoteException {
  52. for (URI address : addresses) {
  53. serverManager.get().connectToAddress(address);
  54. }
  55. }
  56. /**
  57. * Binds to the RMI registry so that other clients may find this remote server.
  58. */
  59. public void bind() {
  60. final RemoteInterface stub;
  61. try {
  62. stub = (RemoteInterface) UnicastRemoteObject.exportObject(this, 0);
  63. } catch (RemoteException ex) {
  64. System.err.println("Unable to export the remote interface");
  65. ex.printStackTrace();
  66. return;
  67. }
  68. for (int port = MINPORT; port < MAXPORT; port++) {
  69. try {
  70. final Registry registry = LocateRegistry.createRegistry(port);
  71. registry.rebind("DMDirc", stub);
  72. return;
  73. } catch (RemoteException ex) {
  74. }
  75. }
  76. }
  77. /**
  78. * Retrieves a reference to an existing RemoteServer, if there is one. Note that this must be
  79. * called before bind(), unless you want a reference to our own client for some reason.
  80. *
  81. * @return The RemoteServer instance, or null if none was available
  82. */
  83. public static RemoteInterface getServer() {
  84. for (int port = MINPORT; port < MAXPORT; port++) {
  85. try {
  86. final Registry registry = LocateRegistry.getRegistry("localhost", port);
  87. final RemoteInterface iface = (RemoteInterface) registry.lookup("DMDirc");
  88. if (iface != null) {
  89. return iface;
  90. }
  91. } catch (RemoteException | NotBoundException ex) {
  92. }
  93. }
  94. // No RMI server found
  95. return null;
  96. }
  97. }