Changeset 15 for mymrc

Show
Ignore:
Timestamp:
07/03/07 05:18:10 (5 years ago)
Author:
ciro
Message:

Small improvements, improved exception handling, added Alert dialogs

Location:
mymrc/trunk/Java-client/src/org/myMRC
Files:
6 modified

Legend:

Unmodified
Added
Removed
  • mymrc/trunk/Java-client/src/org/myMRC/control/Controller.java

    r10 r15  
    107107    } 
    108108         
     109    public void setScreen(Displayable screen) { 
     110        display.setCurrent(screen); 
     111    } 
    109112     
    110113    /** 
     
    152155     */ 
    153156    public void processOutput(String opcode, String data) { 
    154         if (opcode.charAt(0) == '0' && opcode.charAt(1) == '9') { 
    155             processInternalEvent(Util.atoi(opcode), data); 
     157        int iopcode = Util.atoi(opcode); 
     158        if (iopcode >= 900 && iopcode <= 999) { 
     159            processInternalEvent(iopcode, data); 
     160         
     161        } else if (!communicator.isAlive()) { 
     162            /*  
     163             * connection to server lost, we should display that and 
     164             * go back to the default screen instead of trying to send data. 
     165             */ 
     166            this.handle(Opcode.IOP_PARSEFORM, Content.CONT_START_DISCONNECTED); 
     167             
    156168        } else { 
    157169            communicator.send(opcode, data); 
     
    166178     * @param data   event's data 
    167179     */ 
    168     private void processInternalEvent(int opcode, String data) { 
     180    public void processInternalEvent(int opcode, String data) { 
     181        AlertScreen alert = null; 
    169182        switch (opcode) { 
    170183            case 900: 
     
    192205                 */ 
    193206                if (!communicator.isAlive()) { 
    194                     ConnectionStarter comstarter = new ConnectionStarter(); 
     207                    ConnectionStarter comstarter = new ConnectionStarter(this.display); 
    195208                    comstarter.start(); 
    196209                } else { 
     
    200213                break; 
    201214                 
    202             case 991: 
     215            case Opcode.IOP_DISCONNECT: /* 991 */ 
    203216                // disconnect 
     217                System.out.println("handling IOP_DISCONNECT"); 
    204218                communicator.destroy(); 
    205                 this.handle(Opcode.IOP_PARSEFORM, Content.CONT_START); 
     219                this.handle(Opcode.IOP_PARSEFORM, Content.CONT_START_DISCONNECTED); 
    206220                break; 
    207221                 
     
    221235                config.writeSettings(form); 
    222236                try { 
    223                 confcon.getInstance().save(); 
     237                    confcon.getInstance().save(); 
    224238                } catch (IOException ioe) { 
    225239                    ioe.printStackTrace(); 
    226                 } 
    227                 this.processInternalEvent(900, null); 
     240                    alert = new AlertScreen("Saving settings failed!", 921, null); 
     241                    alert.setTimeout(alert.FOREVER); 
     242                    alert.setString("Error while saving settings:\n" + ioe.getMessage()); 
     243                    display.setCurrent(alert); 
     244                    break; /* uhm.. I don't get it, I really NEED a break here.. 
     245                            * well, at least in a test case where I manually 
     246                            * threw an IOException .. should be checked when 
     247                            * we have some real exception case 
     248                            */ 
     249                } 
     250                alert = new AlertScreen("Settings saved", 921, null); 
     251                alert.setString("Your settings have been saved successfully"); 
     252                display.setCurrent(alert); 
     253                //this.processInternalEvent(900, null); 
     254                break; 
     255                 
     256            case 998: // exit confirmation 
     257                alert = new AlertScreen("Exit?", 998, display.getCurrent()); 
     258                alert.setString("Do you really want to quit?"); 
     259                display.setCurrent(alert); 
    228260                break; 
    229261                 
    230262            case 999: 
    231                 // exit 
     263                // exit (the real one) 
    232264                if (communicator.isAlive()) { 
    233                     //this.processOutput(Opcode.OP_EXIT, null); 
    234265                    communicator.destroy(); // FIXME does it work? 
    235266                } 
     
    239270    } 
    240271} 
     272 
     273 
    241274 
    242275 
     
    246279 */ 
    247280class ConnectionStarter extends Thread { 
     281    private Display display; 
     282     
     283    public ConnectionStarter(Display mainDisplay) { 
     284        this.display = mainDisplay; 
     285    } 
     286     
    248287    public void run() { 
     288        /* hum, emulator doesn't react here sometimes anymore */ 
    249289        try { 
    250290            CommunicationController.getInstance().open( 
    251291                    "socket://" + Configuration.getInstance().getServerString()); 
    252292        } catch (Exception e) { 
    253             e.printStackTrace(); 
     293            AlertScreen alert = new AlertScreen("Connection failed!", 990, display.getCurrent()); 
     294            alert.setString("Unable to connect to remote server:\n" + e.getMessage()); 
     295            display.setCurrent(alert); 
     296            return; /* hum.. */ 
    254297        } 
    255298        CommunicationController.getInstance().send(Opcode.OP_HELLO, null); 
  • mymrc/trunk/Java-client/src/org/myMRC/events/Content.java

    r10 r15  
    2323    public static final String CONT_START =  
    2424            "<form label=\"myMRC 0.1\">" +  
    25             "   <image file=\"mymrc.png\"/>" + 
     25            //"   <image file=\"mymrc.png\"/>" + 
     26            "   <string label=\"hallo :)\"/>" + 
     27            "   <string label=\"Not Connected\"/>" + 
    2628            "   <command label=\"Connect\" opcode=\"0990\"/>" + 
    2729            "   <command label=\"Settings\" opcode=\"0920\"/>" + 
    2830            "   <command label=\"About\" opcode=\"0901\"/>" + 
    29             "   <command label=\"Exit\" opcode=\"0999\"/>" + 
    30             "   <command type=\"back\" label=\"Exit\" opcode=\"0999\"/>" + 
     31            "   <command label=\"Exit\" opcode=\"0998\"/>" + 
     32            "   <command type=\"back\" label=\"Exit\" opcode=\"0998\"/>" + 
    3133            "</form>"; 
    3234     
     35    public static final String CONT_START_DISCONNECTED = 
     36            "<form label=\"myMRC 0.1\">" +  
     37            //"   <image file=\"mymrc.png\"/>" + 
     38            "   <string label=\"hallo :)\"/>" + 
     39            "   <string label=\"Connection lost!\"/>" + 
     40            "   <command label=\"Connect\" opcode=\"0990\"/>" + 
     41            "   <command label=\"Settings\" opcode=\"0920\"/>" + 
     42            "   <command label=\"About\" opcode=\"0901\"/>" + 
     43            "   <command label=\"Exit\" opcode=\"0998\"/>" + 
     44            "   <command type=\"back\" label=\"Exit\" opcode=\"0998\"/>" + 
     45            "</form>"; 
     46             
    3347    public static final String CONT_START_CONNECTED =  
    3448            "<form label=\"myMRC 0.1\">" +  
    35             "   <image file=\"mymrc.png\"/>" + 
     49            //"   <image file=\"mymrc.png\"/>" + 
     50            "   <string label=\"hallo :)\"/>" + 
    3651            "   <command label=\"Continue\" opcode=\"0000\"/>" + 
    3752            "   <command label=\"Disconnect\" opcode=\"0991\"/>" + 
    3853            "   <command label=\"Settings\" opcode=\"0920\"/>" + 
    3954            "   <command label=\"About\" opcode=\"0901\"/>" + 
    40             "   <command label=\"Exit\" opcode=\"0999\"/>" + 
    41             "   <command type=\"back\" label=\"Exit\" opcode=\"0999\"/>" + 
     55            "   <command label=\"Exit\" opcode=\"0998\"/>" + 
     56            "   <command type=\"back\" label=\"Exit\" opcode=\"0998\"/>" + 
    4257            "</form>"; 
    4358     
  • mymrc/trunk/Java-client/src/org/myMRC/events/Opcode.java

    r10 r15  
    2020    public static final String OP_HELLO     = "0000"; 
    2121    public static final String OP_BACK      = "0001"; 
     22    public static final int   IOP_BACK      =     1; 
    2223    public static final String OP_NEXT      = "0002"; 
    2324     
     
    2829    public static final String OP_FORMKEY   = "0401"; 
    2930    public static final String OP_FORMVAL   = "0402"; 
    30      
    31     public static final String OP_PARSELIST = "0910"; 
    32     public static final int   IOP_PARSELIST =   910; 
    33     public static final String OP_PARSRFORM = "0911"; 
    34     public static final int   IOP_PARSEFORM =   911; 
     31   
     32    /* 
     33     * client internal opcode group 09xx 
     34     */ 
     35    public static final String OP_PARSELIST     = "0910"; 
     36    public static final int   IOP_PARSELIST     =   910; 
     37    public static final String OP_PARSRFORM     = "0911"; 
     38    public static final int   IOP_PARSEFORM     =   911; 
     39    public static final int   IOP_DISCONNECT    =   991; 
    3540} 
  • mymrc/trunk/Java-client/src/org/myMRC/net/CommunicationController.java

    r10 r15  
    1212import org.myMRC.Util; 
    1313import org.myMRC.control.Controller; 
     14import org.myMRC.events.Opcode; 
    1415 
    1516 
     
    5152    private boolean isAlive = false; 
    5253    private RequestTable requestTable; 
    53     //private EventHandler eventHandler; 
     54    private int counter; 
    5455     
    5556    final int EVENTID_SIZE =  4; 
    5657    final int DATABUF_SIZE = 16; 
    57      
     58    final int MAX_UNRESPONDED_SENDS = 5; 
    5859    /** 
    5960     * Returns the singleton instance of this class. 
     
    6869     
    6970    public static void destroy() { 
     71        System.out.println("seek and destroy \\o/"); 
    7072        try { 
    7173            instance.close(); 
     
    8183    private CommunicationController() { 
    8284        requestTable = new RequestTable(); 
    83 //        eventHandler = new EventHandler(); 
     85        counter      = 0; 
    8486    } 
    8587     
     
    9698        try { 
    9799            socket   = (SocketConnection) Connector.open(url); 
     100            socket.setSocketOption(SocketConnection.DELAY, 0); // disable Nagle 
    98101            ostream  = socket.openOutputStream(); 
    99102            receiver = new Receiver(this, socket.openInputStream()); 
     
    105108             * Event.execute() for EV_(RE)CONNECT will display an error in that 
    106109             * case, so no special handling necessary here. 
     110             * 
     111             *          FUCK THAT! 
     112             *          FIXME! 
    107113             */ 
    108114            isAlive = false; 
     115            throw new IOException(cnfe.getMessage()); // FIXME: this is just a temporary hack 
    109116        } catch (IOException ioe) { 
    110117            ioe.printStackTrace(); 
     
    245252            System.out.println("send: '" + sendBuf + "'"); 
    246253            ostream.write(sendBuf.toString().getBytes()); 
     254            ostream.flush(); 
     255             
    247256        } catch (IOException ioe) { 
    248             ioe.printStackTrace(); 
     257            //ioe.printStackTrace(); 
     258            Controller.getInstance().processInternalEvent(Opcode.IOP_DISCONNECT, null); 
    249259        } 
    250260    } 
     
    287297//            eventHandler.handle(Event.getEvent(eventId), data); 
    288298        } 
     299         
     300        counter = 0; 
    289301    } 
    290302     
  • mymrc/trunk/Java-client/src/org/myMRC/net/Receiver.java

    r10 r15  
    8080            return null; 
    8181        } 
     82        /* 
     83         *java.io.IOException: error 104 during TCP read  
     84        at com.sun.midp.io.j2me.socket.Protocol.nonBufferedRead(Protocol.java:299) 
     85        at com.sun.midp.io.BufferedConnectionAdapter.readBytes(BufferedConnectionAdapter.java:99) 
     86        at com.sun.midp.io.BaseInputStream.read(ConnectionBaseAdapter.java:582) 
     87        at org.myMRC.net.Receiver.read(Receiver.java:78) 
     88        at org.myMRC.net.Receiver.run(Receiver.java:47) 
     89 
     90         */ 
    8291         
    8392         
  • mymrc/trunk/Java-client/src/org/myMRC/settings/Configuration.java

    r10 r15  
    3232     * default values 
    3333     */ 
    34     final String DEFAULT_PATH   = "/myMRC/"; 
     34    //final String DEFAULT_PATH   = "/myMRC/"; 
     35    final String DEFAULT_PATH   = "/E:/Others/"; 
    3536    final String CONFIG_FILE    = "myMRC.conf"; 
    3637