~johannes.baiter/mnemosyne-proj/mnemodroid

« back to all changes in this revision

Viewing changes to src/com/mnemodroid/mnemodroid/MnemoClient.java

  • Committer: Johannes Baiter
  • Date: 2011-02-11 01:11:20 UTC
  • Revision ID: johannes.baiter@gmail.com-20110211011120-wjdmwbwa2qcjpfzq
Added installation routines for scripts and scripting environment; refactored the network code

Show diffs side-by-side

added added

removed removed

Lines of Context:
43
43
 */
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;
48
48
 
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;
57
58
 
58
59
  
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) {
63
62
    try {
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());
70
69
    }
71
70
  }
72
71
 
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
 
95
    while (!isOnline) {
 
96
      sendMessage("#ping");
 
97
      if (getMessage().equals("__DONE__\n")) {
 
98
        isOnline = true;
 
99
      }
97
100
    }
98
101
    while (true) {
99
102
      if (!(cmdQueue.isEmpty())) {
100
 
        sendMessage(cmdQueue.get(0));
101
 
        while (true) {
102
 
          MnemoMessage msg = new MnemoMessage(getMessage(
103
 
              socket, packet), socket, packet);
104
 
          if (msg.pyClass.equals("__done")) {
105
 
            break;
106
 
          } else if (msg.pyClass.equals("__exception")) {
107
 
            break;
108
 
          } else if (msg.pyClass.equals("review_widget")
109
 
              || msg.pyClass.equals("main_widget")) {
110
 
            handler.sendMessage(handler.obtainMessage(0, msg));
111
 
          }
112
 
          else {
113
 
            Log.e(TAG, "Unknown pyClass called: " + msg.pyClass);
114
 
            break;
115
 
          }
116
 
        }
 
103
        communicate(cmdQueue.get(0));
117
104
        cmdQueue.remove(0);
118
105
      } else {
119
106
        try {
125
112
    }
126
113
  }
127
114
  
128
 
 
129
 
  public static String getMessage(DatagramSocket socket, DatagramPacket packet) {
 
115
  /** Handles the communication to the server, sending commands and receiving
 
116
   * their replies.*/
 
117
  private void communicate(String cmd) {
 
118
    sendMessage(cmd);
 
119
    String reply = "";
 
120
    MnemoCall callback = null;
 
121
    // Get and parse packets from the server until the series of replies is
 
122
    // finished.
 
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"))) {
 
128
          Log.e(TAG, reply);
 
129
        }
 
130
      // Parse callbacks to a MnemoCall object and pass it to the appropriate
 
131
      // handler object
 
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));
 
138
        } else {
 
139
          Log.e(TAG, "Unknown pyClass called: " + callback.pyClass);
 
140
        }
 
141
      // If its neither an exception nor a callback, we are dealing with a
 
142
      // value return
 
143
      } else {
 
144
        curVal = reply;
 
145
        // Value return packets are followed by a packet containg a newline,
 
146
        // which has to be caught
 
147
        getMessage();
 
148
      }
 
149
    }
 
150
  }
 
151
 
 
152
 
 
153
 
 
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() {
130
157
    String msg = "";
131
158
    try {
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());
138
165
    }
148
175
      Log.e(TAG, ex.toString());
149
176
    }
150
177
  }
 
178
 
 
179
  public String getValue(String var) {
 
180
    cmdQueue.add("print " + var);
 
181
    while (curVal == null) {
 
182
      continue;
 
183
    }
 
184
    String value = curVal;
 
185
    curVal = null;
 
186
    return value;
 
187
  }
151
188
}