PageRenderTime 50ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/.metadata/.plugins/org.eclipse.core.resources/.history/51/0024724e83a3001e1dff9ae635b3e1ee

https://bitbucket.org/fixpoint/connexion
#! | 91 lines | 79 code | 12 blank | 0 comment | 0 complexity | 542aec8406fb2acb373282a0f28b92a5 MD5 | raw file
  1. package info.reflectionsofmind.connexion.platform.cli;
  2. import info.reflectionsofmind.connexion.platform.control.IRoot;
  3. import info.reflectionsofmind.connexion.platform.control.ServerInfo;
  4. import info.reflectionsofmind.connexion.platform.control.control.IConnectControl;
  5. import info.reflectionsofmind.connexion.platform.control.control.IJoinControl;
  6. import info.reflectionsofmind.connexion.platform.control.cts.CTSMessageCoder;
  7. import info.reflectionsofmind.connexion.platform.control.cts.message.MsgParticipationRequest;
  8. import info.reflectionsofmind.connexion.platform.control.stc.message.MsgParticipationAccepted;
  9. import info.reflectionsofmind.connexion.platform.control.util.ParticipationAcceptedWaiter;
  10. import info.reflectionsofmind.connexion.platform.game.IGame;
  11. import info.reflectionsofmind.connexion.transport.AbstractTransport;
  12. import info.reflectionsofmind.connexion.transport.ITransport;
  13. import info.reflectionsofmind.connexion.transport.ITransport.INode;
  14. import info.reflectionsofmind.connexion.util.convert.EncodingException;
  15. import java.util.concurrent.Callable;
  16. import java.util.concurrent.Executors;
  17. import java.util.concurrent.Future;
  18. public class ConnectInterpreter implements IConnectControl, IInterpreter
  19. {
  20. private final IGame game;
  21. private final IRoot root;
  22. public ConnectInterpreter(final IRoot root, final IGame game)
  23. {
  24. this.game = game;
  25. this.root = root;
  26. }
  27. public Future<IJoinControl> connect(final String name, final INode server)
  28. {
  29. try
  30. {
  31. final ParticipationAcceptedWaiter waiter = new ParticipationAcceptedWaiter();
  32. server.getTransport().addListener(waiter);
  33. server.send(CTSMessageCoder.INSTANCE.encode(new MsgParticipationRequest(name)));
  34. final Future<MsgParticipationAccepted> message = waiter.get();
  35. return Executors.newSingleThreadExecutor().submit(new Callable<IJoinControl>()
  36. {
  37. public IJoinControl call() throws Exception
  38. {
  39. return new JoinInterpreter(getRoot(), getGame(), message.get().getNames());
  40. }
  41. });
  42. }
  43. catch (final EncodingException exception)
  44. {
  45. exception.printStackTrace();
  46. }
  47. }
  48. public IGame getGame()
  49. {
  50. return this.game;
  51. }
  52. public IRoot getRoot()
  53. {
  54. return this.root;
  55. }
  56. public void interpret(final String command)
  57. {
  58. if (command.equals("connect"))
  59. {
  60. final ServerInfo info = this.root.getConfiguration().getServers().get(0);
  61. final ITransport transport = info.getFactory().createTransport(info.getParameters());
  62. final ITransport.IListener listener = new AbstractTransport.Listener()
  63. {
  64. @Override
  65. public void onTrace(final ITransport transport, final String trace)
  66. {
  67. System.out.println(trace);
  68. }
  69. };
  70. transport.addListener(listener);
  71. transport.start();
  72. connect("Player", transport.createNode(info.getAddress()));
  73. }
  74. else if ("exit".equals(command))
  75. {
  76. getRoot().restart();
  77. }
  78. }
  79. }