Permalink
Browse files

Update socket connection example

  • Loading branch information...
1 parent 5f1f37e commit c4f01d75f33a0de119aed463be1597375465f71e @huyttq committed Apr 19, 2012
Showing with 90 additions and 31 deletions.
  1. +3 −3 FirebaseJ2meClient/nbproject/private/private.properties
  2. +86 −0 HelloMidlet.java
  3. +1 −28 README
@@ -1,7 +1,7 @@
-#Thu, 19 Apr 2012 01:38:28 +0700
+#Thu, 19 Apr 2012 01:44:46 +0700
app-version.autoincrement=true
config.active=
-deployment.counter=62
-deployment.number=0.0.61
+deployment.counter=63
+deployment.number=0.0.62
javadoc.preview=true
netbeans.user=C\:\\Users\\Linh\\.netbeans\\7.1.1
View
@@ -0,0 +1,86 @@
+package hello;
+
+import com.cubeia.firebase.clients.java.connector.PacketListener;
+import com.cubeia.firebase.io.ProtocolObject;
+import com.cubeia.firebase.io.protocol.LoginRequestPacket;
+import com.cubeia.firebase.io.protocol.LoginResponsePacket;
+import java.io.IOException;
+import java.security.GeneralSecurityException;
+import javax.microedition.lcdui.*;
+import javax.microedition.midlet.MIDlet;
+import org.huy.firebase.clients.j2me.connector.SocketConnector;
+
+/**
+ * @author
+ * Huy Thai
+ */
+public class HelloMidlet extends MIDlet implements CommandListener {
+
+ private Form loginForm;
+ private TextField usernameTextField;
+ private TextField passwordTextField;
+ private Command cancel;
+ private Command login;
+ private Display display;
+
+ private SocketConnector connector;
+
+ public void startApp() {
+ loginForm = new Form("Login");
+ usernameTextField = new TextField("Username", "", 200, TextField.ANY);
+ passwordTextField = new TextField("Password", "", 200, TextField.PASSWORD);
+ cancel = new Command("Cancel", Command.CANCEL, 2);
+ login = new Command("Login", Command.OK, 2);
+ display = Display.getDisplay(this);
+ loginForm.append(usernameTextField);
+ loginForm.append(passwordTextField);
+ loginForm.addCommand(cancel);
+ loginForm.addCommand(login);
+ loginForm.setCommandListener(this);
+ display.setCurrent(loginForm);
+
+ try {
+ connector = new SocketConnector("localhost", 4123);
+ TestLoginListener listener = new TestLoginListener();
+ connector.addListener(listener);
+ connector.connect();
+
+ } catch (GeneralSecurityException ex) {
+ } catch (IOException ex) {
+ }
+ }
+
+ public void pauseApp() {
+ }
+
+ public void destroyApp(boolean unconditional) {
+ connector.disconnect();
+ notifyDestroyed();
+ }
+
+ public void commandAction(Command c, Displayable d) {
+ String label = c.getLabel();
+ if (label.equals("Cancel")) {
+ destroyApp(true);
+ } else if (label.equals("Login")) {
+ login(usernameTextField.getString(), passwordTextField.getString());
+ }
+ }
+
+ private void login(String username, String pass) {
+ LoginRequestPacket pak = new LoginRequestPacket();
+ pak.user = username;
+ pak.password = pass;
+ pak.operatorid = 0;
+ connector.send(pak);
+ }
+
+ private class TestLoginListener implements PacketListener {
+
+ public void packetRecieved(ProtocolObject packet) {
+ LoginResponsePacket re = (LoginResponsePacket) packet;
+ Alert notice = new Alert("Welcome", "Welcom user: " + re.pid, null, AlertType.INFO);
+ display.setCurrent(notice, loginForm);
+ }
+ }
+}
View
29 README
@@ -1,30 +1,3 @@
1. Download and install Netbeans and J2ME SDK 3.05
2. Build FirebaseJ2meClientTest project and run tests
-3. Connection example:
- try {
- SocketConnector connector = new SocketConnector("localhost", 4123);
- connector.connect();
-
- LoginListener listener = new LoginListener();
- connector.addListener(listener);
- LoginRequestPacket pak = new LoginRequestPacket();
- pak.user = "test";
- pak.password = "1234";
- pak.operatorid = 0;
- connector.send(pak);
-
- // This is just an example to wait for result, you should implement call back listener
- while (listener.loginPacket == null) {
- try {
- Thread.sleep(50);
- } catch (InterruptedException ex) {
- }
- }
-
- LoginResponsePacket result = (LoginResponsePacket) listener.loginPacket;
- // Get pid of current login user: result.pid;
- connector.disconnect();
-
- } catch (GeneralSecurityException ex) {
- } catch (IOException ex) {
- }
+3. See HelloMidlet.java for example

0 comments on commit c4f01d7

Please sign in to comment.