public class NativeRecorderCmd extends AbstractSocketCom {

  Logger log = Logger.getInstance();

  public NativeRecorderCmd() {
    setPort(8112);
  }

  public void sendRecordCmd() throws IOException {
    sendCmd('r');
  }

  public void sendStopCmd() throws IOException {
    sendCmd('s');
  }

  private void sendCmd(char cmd) throws IOException {
    log.debug("sending cmd " + cmd);
    if (!connect()) {
      throw new IOException("Cmd Server connection failed");
    }

    if (!sendRequest(cmd)) {

      throw new IOException("sending cmd '" + cmd + "' failed");
    }
    close();
  }

  private boolean sendRequest(char cmd) {

    try {
      byte[] buf = ("" + cmd).getBytes();
      out.write(buf, 0, buf.length);
      out.flush();

      byte[] data = new byte[256]; // maybe use the buff instead of data[]
      int actualLength = in.read(data);

      String response = new String(data, 0, actualLength);
      log.debug("server response: " + response);
      return true;

    } catch (IOException ioe) {
      log.error("sending error: " + ioe.getMessage());
      return false;
    }
  }
}
Beispiel #2
0
/**
 * Login Form
 *
 * @author maisonneuve, mstevens
 */
public class LoginForm {

  Logger log = Logger.getInstance();

  private Form loginForm;
  // private Container buttonPanel;
  private Container loginPanel;

  private Label titleLbl;

  private Label usernameLbl;
  private Label passwordLbl;
  private TextField usernameTxt = new TextField();
  private TextField passwordTxt;
  private Command skipCmd, loginCmd;

  final String ERROR_MSG = "Username/passwd incorrect";
  final String CONNECTION_MSG = "Connection error";

  private MainMidlet midlet = MainMidlet.getInstance();

  public Form getForm() {

    if (loginForm != null) {
      titleLbl.setText("Login:"******"NoiseTube Account");
    loginPanel = new Container();
    loginPanel.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
    titleLbl = new Label(" ");

    usernameLbl = new Label();
    usernameTxt = new TextField();
    usernameLbl.setText("Username:"******"Password:"******"Skip");
    loginCmd = new Command("Login");

    // buttonPanel = new Container();
    // buttonPanel.addComponent(loginBtn);
    // buttonPanel.getStyle().setMargin(2, 2, 2, 2);

    titleLbl.setText("Login:"******"Connexion error");
                titleLbl.setText("Connexion error");
              } catch (RecordStoreException e1) {
                titleLbl.setText("Error saving APIkey");
                log.error(e1, "Error saving APIkey");
              }
            } else if (skipCmd.equals(arg0.getCommand())) {
              MainMidlet.getInstance()
                  .getPreferences()
                  .setSavingMode(
                      Device.supportsSavingToFile() ? Preferences.SAVE_FILE : Preferences.SAVE_NO);
              midlet.showMeasureForm();
            } else midlet.notifyDestroyed();
          }
        });
    return loginForm;
  }

  private boolean authentication() throws SaveException, RecordStoreException {

    titleLbl.setText("Authenticating...");
    String username = usernameTxt.getText().trim().toLowerCase();
    String password = passwordTxt.getText().trim().toLowerCase();

    // TODO crytped password + encoding
    HTTPWebAPI api = new HTTPWebAPI();
    String apikey = api.getAPIKey(username, password);
    log.debug("API key: " + apikey);
    if (apikey.length() == 40) {
      titleLbl.setText("Saving API key...");
      midlet.getPreferences().setAPIKey(apikey);
      midlet.getPreferences().setUsername(usernameTxt.getText().trim());
      midlet.getPreferences().save();
      titleLbl.setText("API key saved");
      return true;
    } else {

      return false;
    }
  }
}