44
44
public class MnemoClient implements Runnable {
45
45
private static final String TAG = "MnemoClient";
46
private static final int SO_TIMEOUT = 32768;
46
private static final int SO_TIMEOUT = 16192;
47
47
private static final int BUFFER_SIZE = 1024;
49
49
private DatagramSocket socket;
54
54
private ArrayList<String> cmdQueue = new ArrayList<String>();
55
55
private String dataDir;
56
56
private String filename;
57
private String curVal;
59
60
/** Set up socket and packet */
60
public MnemoClient(String server, int port, String dataDir, String filename) {
61
this.dataDir = dataDir;
62
this.filename = filename;
61
public MnemoClient(String server, int port) {
64
63
host = new InetSocketAddress(server, port);
65
64
socket = new DatagramSocket();
66
65
socket.setSoTimeout(SO_TIMEOUT);
67
66
packet = new DatagramPacket(buf, buf.length);
68
67
} catch(Exception ex) {
69
Log.e(TAG, ex.toString());
68
Log.e(TAG, ex.getMessage());
86
85
* removes it from the cmdQueue, executes it and gives a reply message,
87
86
* constructs a MnemoMessage object from it and forwards it to the
88
87
* appropriate handler. */
88
//TODO: Refactor this ugly abomination, together with MnemoMessage, possibly
89
// split up into several methods.
89
90
public void run() {
90
91
Log.d(TAG, "Mnemosyne background thread started");
91
92
Log.d(TAG, "Initializing Mnemosyne...");
92
String initReply = "";
93
while (initReply == "") {
94
sendMessage("mnemosyne.initialise(data_dir=\'" + dataDir
95
+ "\', " + "filename=\'" + filename + "\')");
96
initReply = getMessage(socket, packet);
93
boolean isOnline = false;
94
// Check if server is online and responding
97
if (getMessage().equals("__DONE__\n")) {
99
102
if (!(cmdQueue.isEmpty())) {
100
sendMessage(cmdQueue.get(0));
102
MnemoMessage msg = new MnemoMessage(getMessage(
103
socket, packet), socket, packet);
104
if (msg.pyClass.equals("__done")) {
106
} else if (msg.pyClass.equals("__exception")) {
108
} else if (msg.pyClass.equals("review_widget")
109
|| msg.pyClass.equals("main_widget")) {
110
handler.sendMessage(handler.obtainMessage(0, msg));
113
Log.e(TAG, "Unknown pyClass called: " + msg.pyClass);
103
communicate(cmdQueue.get(0));
117
104
cmdQueue.remove(0);
129
public static String getMessage(DatagramSocket socket, DatagramPacket packet) {
115
/** Handles the communication to the server, sending commands and receiving
117
private void communicate(String cmd) {
120
MnemoCall callback = null;
121
// Get and parse packets from the server until the series of replies is
123
while (!(reply.equals("__DONE__\n"))) {
124
reply = getMessage();
125
// In case of an exception, loop through it and print it the log
126
if (reply.equals("__EXCEPTION__")) {
127
while (!(reply.equals("__DONE__\n"))) {
130
// Parse callbacks to a MnemoCall object and pass it to the appropriate
132
} else if (reply.startsWith("@@")) {
133
callback = new MnemoCall(reply);
134
// In our frontend the review and main widget are one and the same
135
if (callback.pyClass.equals("review_widget")
136
|| callback.pyClass.equals("main_widget")) {
137
handler.sendMessage(handler.obtainMessage(0, callback));
139
Log.e(TAG, "Unknown pyClass called: " + callback.pyClass);
141
// If its neither an exception nor a callback, we are dealing with a
145
// Value return packets are followed by a packet containg a newline,
146
// which has to be caught
154
/** This is a static method so that MnemoMessage can get callbacks directly
155
* from the server instead of constructing an instance of itself first. */
156
public String getMessage() {
132
159
socket.receive(packet);
133
160
msg = new String(packet.getData(), 0, packet.getLength(), "UTF-8");
134
161
} catch (SocketTimeoutException ex) {
135
Log.e(TAG, "Request timed out, server running?");
162
Log.d(TAG, "Request timed out, server running?");
136
163
} catch (IOException ex) {
137
164
Log.e(TAG, ex.toString());