-
Notifications
You must be signed in to change notification settings - Fork 5
/
ServerCommunicationSocketsRunnable.java
72 lines (63 loc) · 2.95 KB
/
ServerCommunicationSocketsRunnable.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package com.doing.more.java.example.sillyclient;
import android.os.Handler;
import android.widget.TextView;
import org.quickconnectfamily.json.JSONInputStream;
import org.quickconnectfamily.json.JSONOutputStream;
import java.lang.ref.WeakReference;
import java.net.Socket;
import java.util.HashMap;
public class ServerCommunicationSocketsRunnable implements Runnable {
private int numMessagesSent;
private String messageForServer;
private WeakReference responseViewReference;
private Handler uiThreadStack;
public ServerCommunicationSocketsRunnable(int currentNumMessagesSent, String aMessageForServer, WeakReference aResponseViewReference, Handler aUIThreadStack) {
this.numMessagesSent = currentNumMessagesSent;
this.messageForServer = aMessageForServer;
this.responseViewReference = aResponseViewReference;
this.uiThreadStack = aUIThreadStack;
}
public void run() {
try {
//connect to the server
Socket toServer = new Socket("10.0.2.2", 9393);
//setup the JSON streams to be used later.
final JSONInputStream inFromServer =
new JSONInputStream(toServer.getInputStream());
final JSONOutputStream outToServer =
new JSONOutputStream(toServer.getOutputStream());
HashMap<String,Object> commandMap = new HashMap<>();
commandMap.put("command", "Speak");
commandMap.put("count",this.numMessagesSent);
commandMap.put("message", this.messageForServer);
outToServer.writeObject(commandMap);
System.out.println("waiting for response");
HashMap<String,Object> response = (HashMap<String, Object>) inFromServer.readObject();
System.out.println("response: "+response);
if(response.get("command").equals("Done")){
final TextView responseView = (TextView) responseViewReference.get();
uiThreadStack.post(new Runnable() {
@Override
public void run() {
if (responseView != null) {
responseView.setText("Sent " + ServerCommunicationSocketsRunnable.this.messageForServer
+ ". Message number " + ServerCommunicationSocketsRunnable.this.numMessagesSent);
}
}
});
}
} catch (Exception e) {
e.printStackTrace();
final String theErrorMessage = e.getLocalizedMessage();
final TextView responseView = (TextView) responseViewReference.get();
uiThreadStack.post(new Runnable() {
@Override
public void run() {
if (responseView != null) {
responseView.setText("Error: Unable to communicate with server. " + theErrorMessage);
}
}
});
}
}
}