Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ----------------------------------------------------------------------------------------
- // WordServerInterface.java
- // This file is incomplete.
- // This file should oversee connection to the word server and retrieval of candidate
- // words from the word server. It will need to convert them into a string array and
- // return the data to blackboard.
- //
- // You will need to add code and edit this file as part of your solution.
- // ----------------------------------------------------------------------------------------
- package blackBoard;
- import akka.actor.ActorRef;
- import akka.actor.ActorSystem;
- import akka.actor.Props;
- import akka.actor.UntypedActor;
- import akka.pattern.Patterns;
- import akka.util.Timeout;
- import scala.concurrent.Await;
- import scala.concurrent.Future;
- import java.io.*;
- import java.net.*;
- import java.util.concurrent.TimeUnit;
- //initialise the connection and logging in
- public class WordServerInterface {
- static Socket sock;
- static String UserInput;
- static String ServerResponse;
- static String codeinput;
- static int i;
- static String msg = "test";
- static PrintWriter out1;
- static BufferedReader in;
- static boolean connected = false;
- static InputStream is;
- static byte[] buffer = new byte[500000];
- static ActorSystem system = ActorSystem.create("actor-demo-java");
- static ActorRef Login = system.actorOf(Props.create(Login1.class));
- public static class Login1 extends UntypedActor {
- @Override
- public void onReceive(Object arg0) throws Exception {
- if (connected == false) {
- sock = new Socket("localhost", 13744);
- is = sock.getInputStream();
- BufferedReader r = new BufferedReader(new InputStreamReader(is));
- OutputStream os = sock.getOutputStream();
- PrintStream out = new PrintStream(os);
- String logIn = r.readLine().substring(11, 18);
- String output1 = ("< 1523153*" + logIn);
- out.println(output1);
- System.out.print(r.readLine());
- out.flush();
- connected = true;
- }
- } //end
- }
- public static class words extends UntypedActor {
- @Override
- public void onReceive(Object pattern) throws Exception {
- PrintWriter out = new PrintWriter(sock.getOutputStream(), true);
- InputStream is = sock.getInputStream();
- String outStr = "";
- codeinput = (String) pattern;
- out.println(codeinput);
- out.flush();
- while ((i = is.read(buffer)) != -1) {
- String output = new String(buffer, 0, i);
- outStr = (output);
- break;
- }
- getSender().tell(outStr, getSelf());
- }
- } //end
- public WordServerInterface () {
- Timeout timeout = new Timeout(3, TimeUnit.SECONDS);
- Future<Object> future = Patterns.ask(Login, msg, timeout);
- try {
- String result = (String) Await.result(future, timeout.duration());
- System.out.println(result);
- } catch (Exception e) {
- return;
- // e.printStackTrace();
- }
- }
- // receives a word code and returns an array of strings into the blackboard
- public String [] allPatterns(String pattern) {
- String result = "";
- ActorRef WordCode = system.actorOf(Props.create(words.class));
- Timeout timeout = new Timeout(5, TimeUnit.SECONDS);
- Future<Object> future = Patterns.ask(WordCode, pattern, timeout);
- try {
- result = (String) Await.result(future, timeout.duration());
- System.out.println(result);
- } catch (Exception e) {
- e.printStackTrace();
- }
- String [] ArrayW = result.split(",");
- return ArrayW;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement