Advertisement
Guest User

Untitled

a guest
Apr 27th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.56 KB | None | 0 0
  1. // ----------------------------------------------------------------------------------------
  2. // WordServerInterface.java
  3. // This file is incomplete.
  4. // This file should oversee connection to the word server and retrieval of candidate
  5. // words from the word server. It will need to convert them into a string array and
  6. // return the data to blackboard.
  7. //
  8. // You will need to add code and edit this file as part of your solution.
  9. // ----------------------------------------------------------------------------------------
  10. package blackBoard;
  11.  
  12. import akka.actor.ActorRef;
  13. import akka.actor.ActorSystem;
  14. import akka.actor.Props;
  15. import akka.actor.UntypedActor;
  16.  
  17. import akka.pattern.Patterns;
  18. import akka.util.Timeout;
  19. import scala.concurrent.Await;
  20. import scala.concurrent.Future;
  21.  
  22. import java.io.*;
  23. import java.net.*;
  24.  
  25. import java.util.concurrent.TimeUnit;
  26.  
  27.  
  28. //initialise the connection and logging in
  29. public class WordServerInterface {
  30.  
  31. static Socket sock;
  32. static String UserInput;
  33. static String ServerResponse;
  34. static String codeinput;
  35. static int i;
  36. static String msg = "test";
  37. static PrintWriter out1;
  38. static BufferedReader in;
  39. static boolean connected = false;
  40. static InputStream is;
  41. static byte[] buffer = new byte[500000];
  42.  
  43. static ActorSystem system = ActorSystem.create("actor-demo-java");
  44. static ActorRef Login = system.actorOf(Props.create(Login1.class));
  45.  
  46. public static class Login1 extends UntypedActor {
  47.  
  48.  
  49. @Override
  50. public void onReceive(Object arg0) throws Exception {
  51.  
  52. if (connected == false) {
  53.  
  54. sock = new Socket("localhost", 13744);
  55. is = sock.getInputStream();
  56. BufferedReader r = new BufferedReader(new InputStreamReader(is));
  57.  
  58.  
  59.  
  60. OutputStream os = sock.getOutputStream();
  61. PrintStream out = new PrintStream(os);
  62. String logIn = r.readLine().substring(11, 18);
  63. String output1 = ("< 1523153*" + logIn);
  64.  
  65. out.println(output1);
  66. System.out.print(r.readLine());
  67. out.flush();
  68.  
  69. connected = true;
  70. }
  71.  
  72. } //end
  73. }
  74.  
  75. public static class words extends UntypedActor {
  76.  
  77.  
  78. @Override
  79. public void onReceive(Object pattern) throws Exception {
  80.  
  81. PrintWriter out = new PrintWriter(sock.getOutputStream(), true);
  82. InputStream is = sock.getInputStream();
  83. String outStr = "";
  84.  
  85. codeinput = (String) pattern;
  86. out.println(codeinput);
  87. out.flush();
  88.  
  89.  
  90. while ((i = is.read(buffer)) != -1) {
  91. String output = new String(buffer, 0, i);
  92. outStr = (output);
  93. break;
  94. }
  95. getSender().tell(outStr, getSelf());
  96. }
  97. } //end
  98.  
  99.  
  100.  
  101. public WordServerInterface () {
  102.  
  103.  
  104. Timeout timeout = new Timeout(3, TimeUnit.SECONDS);
  105. Future<Object> future = Patterns.ask(Login, msg, timeout);
  106.  
  107. try {
  108. String result = (String) Await.result(future, timeout.duration());
  109. System.out.println(result);
  110. } catch (Exception e) {
  111. return;
  112. // e.printStackTrace();
  113. }
  114. }
  115.  
  116. // receives a word code and returns an array of strings into the blackboard
  117. public String [] allPatterns(String pattern) {
  118.  
  119. String result = "";
  120.  
  121. ActorRef WordCode = system.actorOf(Props.create(words.class));
  122.  
  123. Timeout timeout = new Timeout(5, TimeUnit.SECONDS);
  124. Future<Object> future = Patterns.ask(WordCode, pattern, timeout);
  125.  
  126. try {
  127. result = (String) Await.result(future, timeout.duration());
  128. System.out.println(result);
  129. } catch (Exception e) {
  130.  
  131. e.printStackTrace();
  132. }
  133.  
  134.  
  135. String [] ArrayW = result.split(",");
  136.  
  137. return ArrayW;
  138.  
  139. }
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement