Changeset 32 for mymrc

Show
Ignore:
Timestamp:
07/22/07 02:54:29 (5 years ago)
Author:
ciro
Message:

Implemented Bluetooth communication.
For that, the communication was rewritten a bit.

Also removed all filesystem issues, the settings are now stored in a
RecordStore? object; this will remove any compatibility issues between
different devices.

Location:
mymrc/trunk/Java-client
Files:
8 added
4 removed
8 modified

Legend:

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

    r20 r32  
    1010import org.myMRC.*; 
    1111import org.myMRC.events.*; 
    12 import org.myMRC.net.CommunicationController; 
     12import org.myMRC.net.BluetoothController; 
     13import org.myMRC.net.InetController; 
     14import org.myMRC.net.NetworkController; 
    1315import org.myMRC.parser.*; 
    1416import org.myMRC.screen.*; 
     
    3133    private PlaybackCanvas canvas; 
    3234    private Display display; 
    33     private CommunicationController communicator; 
    34     private ConfigurationConnection confcon; 
     35    private NetworkController communicator; 
    3536    private Configuration config; 
    3637    private ListParser listParser; 
     
    7879    } 
    7980     
    80      
     81    public MIDlet getMidlet() { return this.myMRC; } 
    8182     
    8283    /** Creates a new instance of Controller */ 
     
    8687        canvas       = new PlaybackCanvas(); 
    8788        display      = Display.getDisplay(myMRC); 
    88         communicator = CommunicationController.getInstance(); 
    89         confcon      = ConfigurationConnection.getInstance(); 
    90         config       = Configuration.getInstance(); 
     89         
     90        try { 
     91            config = Configuration.createInstance(); 
     92        } catch (Exception e) { 
     93            e.printStackTrace(); 
     94            AlertScreen alert = new AlertScreen(null, Opcode.IOP_START, display.getCurrent()); 
     95            alert.setTimeout(alert.FOREVER); 
     96            alert.setString("Error while loading settings:\n" + e.getMessage()); 
     97            display.setCurrent(alert); 
     98        } 
     99 
    91100        listParser   = new ListParser(list); 
    92101        formParser   = new FormParser(form); 
    93102        canvasParser = new CanvasParser(canvas); 
     103 
    94104    } 
    95105     
     
    125135    public void setScreen(Displayable screen) { 
    126136        display.setCurrent(screen); 
     137    } 
     138     
     139    /** 
     140     * Returns the currently displayed screen element. 
     141     * @return Currently displayed screen element. 
     142     */ 
     143    public Displayable getCurrentScreen() { 
     144        return display.getCurrent(); 
    127145    } 
    128146     
     
    212230                    this.handle(Opcode.IOP_PARSEFORM, Content.CONT_START_CONNECTED); 
    213231                } 
     232                 
    214233                break; 
    215234                 
    216235            case Opcode.IOP_CONNECT: 
    217                 /* 
    218                  * We have to use a separate thread to start the 
    219                  * CommunicationController. 
    220                  */ 
    221                 if (!communicator.isAlive()) { 
    222                     ConnectionStarter comstarter = new ConnectionStarter(this.display); 
    223                     comstarter.start(); 
    224                 } else { 
    225                     communicator.send(Opcode.OP_HELLO, null); 
     236                switch (Util.atoi(config.get(Settings.CONNECTION_TYPE))) { 
     237                    case Settings.CONN_BLUETOOTH: 
     238                        this.communicator = BluetoothController.getInstance(); 
     239                        break; 
     240                         
     241                    case Settings.CONN_INET: 
     242                        /* 
     243                         * We have to use a separate thread to start the 
     244                         * InetController. 
     245                         */ 
     246                         
     247                        /* 
     248                         *  W A R N I N G 
     249                         * FIXME/CHECKME - I changed the whole net.*Controller 
     250                         * stuff, and this here hasn't been tested yet, 
     251                         * so it should be done :) 
     252                         */ 
     253                        this.communicator = InetController.getInstance(); // <-- !! 
     254                         
     255                        if (!communicator.isAlive()) { 
     256                            ConnectionStarter comstarter = new ConnectionStarter(this.display); 
     257                            comstarter.start(); 
     258                        } else { 
     259                            communicator.send(Opcode.OP_HELLO, null); 
     260                        } 
     261                         
     262                        break; 
    226263                } 
    227264                break; 
     
    238275                 
    239276            case Opcode.IOP_DISCONNECT: 
    240                 communicator.destroy(); 
    241                 this.handle(Opcode.IOP_PARSEFORM, Content.CONT_START_DISCONNECTED); 
     277                try { 
     278                    communicator.close(); 
     279                    this.handle(Opcode.IOP_PARSEFORM, Content.CONT_START_DISCONNECTED); 
     280                } catch (IOException ioe) { 
     281                    ioe.printStackTrace(); 
     282                } 
     283                break; 
     284                 
     285            case Opcode.IOP_BT_GET_DEVICES: 
     286            case Opcode.IOP_BT_GET_DEV_CANCEL: 
     287            case Opcode.IOP_BT_DEVICE_DETAILS: 
     288            case Opcode.IOP_BT_GET_SERVICES: 
     289            case Opcode.IOP_BT_SERVICES_DETAILS: 
     290            case Opcode.IOP_BT_CONNECT: 
     291                /* distribute to BluetoothController */ 
     292                BluetoothController.getInstance().handleBTopcode(opcode, data); 
    242293                break; 
    243294                 
     
    251302                 
    252303            case Opcode.IOP_SETTINGS_SAVE: 
    253                 config.writeSettings(form); 
     304                //config.writeSettings(form); 
    254305                try { 
    255                     confcon.getInstance().save(); 
    256                 } catch (IOException ioe) { 
    257                     ioe.printStackTrace(); 
     306                    config.setFromForm(form); 
     307                } catch (Exception e) { 
     308                    e.printStackTrace(); 
    258309                    alert.setTimeout(alert.FOREVER); 
    259                     alert.setString("Error while saving settings:\n" + ioe.getMessage()); 
     310                    alert.setString("Error while saving settings:\n" + e.getMessage()); 
    260311                    display.setCurrent(alert); 
    261312                    break; 
     
    272323            case Opcode.IOP_INT_EXIT: 
    273324                if (communicator.isAlive()) { 
    274                     communicator.destroy(); // FIXME: does it work? 
     325                    try { 
     326                    communicator.close(); // FIXME: does it work? 
     327                    } catch (IOException ioe) { 
     328                        ioe.printStackTrace(); 
     329                    } 
    275330                } 
    276331                Controller.killEmAll(); 
     
    296351    public void run() { 
    297352        /* hum, emulator doesn't react here sometimes anymore */ 
     353        String url = "socket://" +  
     354                Configuration.getInstance().get(Settings.INET_SERVER_HOST) + ":" + 
     355                Configuration.getInstance().get(Settings.INET_SERVER_PORT); 
    298356        try { 
    299             CommunicationController.getInstance().open( 
    300                     "socket://" + Configuration.getInstance().getServerString()); 
     357            InetController.getInstance().open(url); 
    301358        } catch (Exception e) { 
    302359            AlertScreen alert = new AlertScreen("Connection failed!", 990, display.getCurrent()); 
     
    305362            return; 
    306363        } 
    307         CommunicationController.getInstance().send(Opcode.OP_HELLO, null); 
     364        InetController.getInstance().send(Opcode.OP_HELLO, null); 
    308365    } 
    309366} 
  • mymrc/trunk/Java-client/src/org/myMRC/events/Content.java

    r20 r32  
    11package org.myMRC.events; 
    22 
    3 import org.myMRC.net.CommunicationController; 
    43/* 
    54 * Content.java 
     
    76 * Created on June 29, 2007, 1:08 AM 
    87 */ 
    9  
     8import org.myMRC.net.InetController; 
     9import org.myMRC.settings.Settings; 
    1010 
    1111/** 
     
    2424            "<form label=\"myMRC 0.1\">" +  
    2525            "   <image file=\"mymrc.png\"/>" + 
    26             "   <string label=\"Not Connected\"/>" + 
     26            "   <string text=\"Not Connected\"/>" + 
    2727            "   <command label=\"Connect\" opcode=\"0" + Opcode.IOP_CONNECT + "\"/>" + 
    2828            "   <command label=\"Settings\" opcode=\"0" + Opcode.IOP_SETTINGS + "\"/>" + 
     
    3535            "<form label=\"myMRC 0.1\">" +  
    3636            "   <image file=\"mymrc.png\"/>" + 
    37             "   <string label=\"Connection lost!\"/>" + 
     37            "   <string text=\"Connection lost!\"/>" + 
    3838            "   <command label=\"Connect\" opcode=\"0" + Opcode.IOP_CONNECT + "\"/>" + 
    3939            "   <command label=\"Settings\" opcode=\"0" + Opcode.IOP_SETTINGS + "\"/>" + 
     
    5757    public static final String CONT_SETTINGS =  
    5858            "<form label=\"Settings\">" + 
    59             "   <textfield setting=\"serverHost\" label=\"Server Host\" size=\"20\"/>" + 
    60             "   <textfield setting=\"serverPort\" label=\"Server Port\" type=\"number\" size=\"5\"/>" + 
     59            "   <select type=\"exclusive\" label=\"Connection Type\" setting=\"" + Settings.CONNECTION_TYPE + "\">" + 
     60            "       <option value=\"" + Settings.CONN_BLUETOOTH + "\" label=\"Bluetooth\"/>" + 
     61            "       <option value=\"" + Settings.CONN_INET + "\" label=\"Internet Socket\"/>" + 
     62            "   </select>" + 
     63            "   <spacer height=\"5\"/>" + 
     64            "   <textfield setting=\"" + Settings.INET_SERVER_HOST + "\" label=\"Inet Server Host\" size=\"20\"/>" + 
     65            "   <textfield setting=\"" + Settings.INET_SERVER_PORT + "\" label=\"Inet Server Port\" type=\"number\" size=\"5\"/>" + 
     66            "   <spacer height=\"5\"/>" + 
     67            "   <textfield setting=\"" + Settings.BT_SERVER_ADDR + "\" label=\"Bluetooth Server Address\" size=\"20\"/>" + 
     68            "   <textfield setting=\"" + Settings.BT_SERVER_PORT + "\" label=\"Bluetooth Server Port\" type=\"number\" size=\"5\"/>" + 
    6169            "   <command type=\"back\" label=\"Cancel\" opcode=\"0" + Opcode.IOP_START + "\"/>" + 
    6270            "   <command label=\"Save\" opcode=\"0" + Opcode.IOP_SETTINGS_SAVE + "\"/>" + 
     
    6573    public static final String CONT_ABOUT =  
    6674            "<form label=\"About\">" + 
    67             "   <string label=\"myMRC 0.1\"/>" + 
    68             "   <string label=\"(c) 2007 Sven Gregori and Damien Lefevre\"/>" + 
     75            "   <string label=\"myMRC 0.1\" text=\"(c) 2007 Sven Gregori and Damien Lefevre\"/>" + 
     76            "   <string label=\"\" text=\"For documentation, updates, source code, etc. please visit http://www.mymrc.org/\"/>" + 
     77            "   <string label=\"\" text=\"myMRC is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version.\"/>" + 
     78            "   <string label=\"\" text=\"myMRC is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.\"/>" + 
    6979            "   <command type=\"back\" label=\"Ok\" opcode=\"0" + Opcode.IOP_START + "\"/>" + 
    7080            "</form>"; 
  • mymrc/trunk/Java-client/src/org/myMRC/events/Opcode.java

    r20 r32  
    4343    public static final int   IOP_SETTINGS          =   920; 
    4444    public static final int   IOP_SETTINGS_SAVE     =   921; 
    45     public static final int   IOP_CONNECT           =   990; 
    46     public static final int   IOP_DISCONNECT_CONF   =   991; 
    47     public static final int   IOP_DISCONNECT_FORCED =   992; 
    48     public static final int   IOP_DISCONNECT        =   993; 
     45    public static final int   IOP_CONNECT           =   950; 
     46    public static final int   IOP_DISCONNECT_CONF   =   951; 
     47    public static final int   IOP_DISCONNECT_FORCED =   952; 
     48    public static final int   IOP_DISCONNECT        =   953; 
     49    public static final int   IOP_BT_GET_DEVICES    =   960; 
     50    public static final int   IOP_BT_GET_DEV_CANCEL =   961; 
     51    public static final int   IOP_BT_DEVICE_DETAILS =   962; 
     52    public static final int   IOP_BT_GET_SERVICES   =   963; 
     53    public static final int   IOP_BT_SERVICES_DETAILS   =   964; 
     54    public static final int   IOP_BT_CONNECT        =   965; 
    4955    public static final int   IOP_INT_EXIT_CONF     =   998; 
    5056    public static final int   IOP_INT_EXIT          =   999; 
  • mymrc/trunk/Java-client/src/org/myMRC/myMRC.java

    r22 r32  
    1111import org.myMRC.events.Content; 
    1212import org.myMRC.events.Opcode; 
    13 import org.myMRC.net.CommunicationController; 
     13import org.myMRC.net.BluetoothController; 
     14import org.myMRC.net.InetController; 
    1415import org.myMRC.settings.*; 
    1516import org.myMRC.control.Controller; 
     
    3132     */ 
    3233    public myMRC() { 
    33          
    34         try { 
    35             ConfigurationConnection ccon = ConfigurationConnection.createInstance(); 
    36             ccon.load(); 
    37         } catch (IOException ioe) {  
    38             ioe.printStackTrace(); 
    39         } catch (ConfigParserException cpe) { 
    40             cpe.printStackTrace(); 
    41         } 
    42          
    4334        Controller.createInstance(this); 
    4435        Controller.getInstance().handle(Opcode.IOP_PARSEFORM, Content.CONT_START); 
     
    5142    public void exitMIDlet() { 
    5243        try { 
    53             if (CommunicationController.getInstance().isAlive()) { 
    54                 CommunicationController.getInstance().close(); 
     44            if (InetController.getInstance().isAlive()) { 
     45                InetController.getInstance().close(); 
     46            } 
     47            if (BluetoothController.getInstance().isAlive()) { 
     48                BluetoothController.getInstance().close(); 
    5549            } 
    5650        } catch (IOException ioe) { 
     
    6155        } 
    6256         
    63         CommunicationController.destroy(); 
     57        //InetController.getInstance().destroy(); 
    6458         
    6559        Display.getDisplay(this).setCurrent(null); 
  • mymrc/trunk/Java-client/src/org/myMRC/net/Receiver.java

    r15 r32  
    77import java.io.*; 
    88import org.myMRC.Util; 
     9import org.myMRC.control.Controller; 
    910 
    1011/** 
    1112 * Handles all incoming network traffic. Works together with the 
    12  * CommunicationController. 
    13  * 
     13 * InetController and BluetoothController. 
     14 *  
    1415 * @author Sven Gregori 
    1516 */ 
    1617class Receiver extends Thread { 
    1718    private boolean doRun; 
    18     private CommunicationController controller; 
    1919    private InputStream istream; 
     20    private NetworkController controller; 
    2021     
    2122    private final int READBUF_SIZE = 512; 
    22      
     23    static final int EVENTID_SIZE =  4; 
     24    static final int DATABUF_SIZE = 16; 
    2325     
    2426     
    2527    /** 
    2628     * Creates a new instance of Receiver. 
    27      * 
    28      * @param controller Reference to the CommunicationController which 
     29     *  
     30     * @param controller Reference to the InetController which 
    2931     *                   creates this object. 
    3032     * @param istream InputStream object from the socket to the server 
    3133     */ 
    32     Receiver(CommunicationController controller, InputStream istream) { 
     34    Receiver(NetworkController controller, InputStream istream) { 
    3335        this.doRun      = true; 
    3436        this.controller = controller; 
     
    3638    } 
    3739     
     40     
    3841    /** 
    3942     * Thread run method. 
    4043     * Waits all the time for data from the server. After receiving data it 
    41      * calls the handleReceived method from the CommunicationController. 
     44     * calls the handleReceived method from the InetController. 
    4245     */ 
    4346    public void run() { 
    4447        while (doRun) { 
     48            Controller c = Controller.getInstance(); 
     49            org.myMRC.screen.AlertScreen alert = new org.myMRC.screen.AlertScreen(null, 0, c.getCurrentScreen()); 
     50            if (istream == null) { 
     51                alert.setString("fuck, istream is null"); 
     52                c.setScreen(alert); 
     53                break; 
     54            } 
     55         
     56            StringBuffer buf = null; 
     57            String eventId = "-"; 
    4558            try { 
    46                 StringBuffer buf = new StringBuffer(); 
    47                 String eventId   = read(buf); 
     59                //StringBuffer buf = new StringBuffer(); 
     60                buf = new StringBuffer(); 
     61                //String eventId   = read(buf); 
     62                eventId   = read(buf); 
    4863                if (eventId != null) { 
    49                     controller.handleReceived(eventId, buf.toString()); 
     64                    controller.resetCounter(); 
     65                    Controller.getInstance().handle(Util.atoi(eventId), buf.toString()); 
    5066                } 
    5167                 
     
    5470            } catch (IOException ioe) { 
    5571                ioe.printStackTrace(); 
     72            } catch (Exception e) { 
     73                alert.setTimeout(javax.microedition.lcdui.Alert.FOREVER); 
     74                alert.setString(e.getClass().toString() + " in Receiver: " + e.getMessage() + 
     75                        "  istream = " + istream + "  buf = " + buf.toString() +  
     76                        "  eventId = " + eventId); 
     77                c.setScreen(alert); 
    5678            } 
    5779        } 
     
    6789     * @return Event ID of the read data 
    6890     */ 
    69     private String read(StringBuffer buf) throws IOException, IndexOutOfBoundsException { 
    70         byte[] eventId = new byte[controller.EVENTID_SIZE]; 
    71         byte[] dSize   = new byte[controller.DATABUF_SIZE]; 
     91    private String read(StringBuffer buf) throws IOException, IndexOutOfBoundsException, NullPointerException { 
     92        byte[] eventId = new byte[EVENTID_SIZE]; 
     93        byte[] dSize   = new byte[DATABUF_SIZE]; 
    7294        byte[] readBuf = new byte[READBUF_SIZE]; 
    7395         
     
    7698         
    7799        /* First read the event ID from the server */ 
    78         if (istream.read(eventId, 0, controller.EVENTID_SIZE) == -1) { 
     100        if (istream.read(eventId, 0, EVENTID_SIZE) == -1) { 
    79101            doRun = false; 
    80102            return null; 
     
    96118         * larger than 10 petabyte ;p 
    97119         */ 
    98         if ( (readSize = istream.read(dSize, 0, controller.DATABUF_SIZE)) == -1) { 
     120        if ( (readSize = istream.read(dSize, 0, DATABUF_SIZE)) == -1) { 
    99121            doRun = false; 
    100122            return null; 
  • mymrc/trunk/Java-client/src/org/myMRC/parser/FormParser.java

    r20 r32  
    9595                 
    9696                String label = parser.getAttributeValue(null, "label"); 
    97                 String setting = parser.getAttributeValue(null, "setting"); 
     97                int setting = Util.atoi(parser.getAttributeValue(null, "setting")); 
    9898                int size = Util.atoi(parser.getAttributeValue(null, "width")); 
    9999                if (size == 0) { 
    100100                    size = DEFAULT_TEXTFIELD_SIZE; 
    101101                } 
    102                 String text = config.getSettingString(setting); 
     102 
     103                String text = config.get(setting); 
    103104                form.append(new TextField(label, text, size, constraints)); 
    104105                 
     
    142143                ChoiceGroup select = new ChoiceGroup( 
    143144                        parser.getAttributeValue(null, "label"), selectType); 
    144                 String setting = parser.getAttributeValue(null, "setting"); 
     145                int setting = Util.atoi(parser.getAttributeValue(null, "setting")); 
    145146                while (!(parser.next() == XmlPullParser.END_TAG && 
    146147                         parser.getName() != null && 
     
    179180     * structure and stores it in the given ChoiceGroup object. Sets also the 
    180181     * selected option (if any) as selected. 
    181      * 
     182     *  
    182183     * @param select ChoiceGroup object to fill 
    183184     * @param setting String representation of the variable from the 
    184      *                Configuration class which the ChoiceGroup is handling 
     185     *                CoConfigurationXlass which the ChoiceGroup is handling 
    185186     * @throws XmlPullParserException if an Exception from the underlying XML 
    186187     *                                parser occurs. 
    187188     */ 
    188     private void createSelectOptions(ChoiceGroup select, String setting) 
     189    private void createSelectOptions(ChoiceGroup select, int setting) 
    189190            throws XmlPullParserException 
    190191    { 
     
    192193            if (parser.getName().equalsIgnoreCase("option")) { 
    193194                String value = parser.getAttributeValue(null, "value"); 
     195                String label = parser.getAttributeValue(null, "label"); 
    194196                /* 
    195197                 * TODO: add pictures in choicegroup (e.g. flags in language 
    196198                 *       choice, could look neat :> 
    197199                 */ 
    198                 int index = select.append(value, null); 
    199                 if (config.getSettingString(setting).equalsIgnoreCase(value)) { 
     200                int index = select.append(label, null); 
     201                if (config.get(setting).equalsIgnoreCase(value)) { 
    200202                    select.setSelectedIndex(index, true); 
    201203                } 
  • mymrc/trunk/Java-client/src/org/myMRC/settings/Configuration.java

    r15 r32  
    33 * Configuration.java 
    44 * 
    5  * Created on March 22, 2007, 6:40 PM 
     5 * Created on July 20, 2007, 2:30 AM 
    66 */ 
     7import java.util.Vector; 
    78import javax.microedition.lcdui.*; 
     9import javax.microedition.rms.*; 
    810import org.myMRC.screen.FormScreen; 
    9 import org.myMRC.Util; 
    1011 
    1112 
    1213/** 
    13  * Global myMRC configuration class. 
    14  * Holds all information from the myMRC.conf file and handles operations on it. 
    15  * Implemented as Singleton Pattern. 
    1614 * 
    1715 * @author Sven Gregori 
    1816 */ 
    1917public class Configuration { 
    20     /** static instance for Singleton implementation */ 
    2118    private static Configuration instance; 
     19    RecordStore storage; 
    2220     
    23     /* 
    24      * myMRC settings itself. 
    25      */ 
    26     private String path; 
    27     private String configFile; 
    28     private String serverHost; 
    29     private int serverPort; 
    30      
    31     /* 
    32      * default values 
    33      */ 
    34     //final String DEFAULT_PATH   = "/myMRC/"; 
    35     final String DEFAULT_PATH   = "/E:/Others/"; 
    36     final String CONFIG_FILE    = "myMRC.conf"; 
    37      
    38     /** 
    39      * Returns the singleton instance of this class. 
    40      * 
    41      * @param none 
    42      * @return Singleton Configuration instance 
    43      */ 
    44     public static synchronized Configuration getInstance() { 
     21    public static synchronized Configuration createInstance() throws Exception { 
    4522        if (instance == null) { 
    4623            instance = new Configuration(); 
     24             
     25            // only during developemnt! 
     26            /* 
     27            instance.storage.closeRecordStore(); 
     28            instance.storage.deleteRecordStore("myMRCconf"); 
     29            instance.storage = RecordStore.openRecordStore("myMRCconf", true,  
     30                RecordStore.AUTHMODE_PRIVATE, true); 
     31            System.out.println("num records = " + instance.storage.getNumRecords()); 
     32            */ 
     33             
     34             
     35            if (instance.storage.getNumRecords() == 0) { 
     36                instance.loadDefaults(); 
     37            } 
    4738        } 
    4839        return instance; 
    4940    } 
    5041     
    51     /** 
    52      * Creates a new instance of Configuration 
    53      */ 
    54     private Configuration() { 
    55         this.path       = DEFAULT_PATH; 
    56         this.configFile = CONFIG_FILE; 
    57         this.serverHost = "0.0.0.0"; 
    58         this.serverPort = 0; 
     42    public static synchronized Configuration getInstance() { 
     43        return instance; 
     44    } 
     45     
     46    /** Creates a new instance of Configuration */ 
     47    private Configuration() throws Exception { 
     48        storage = RecordStore.openRecordStore("myMRCconf", true,  
     49            RecordStore.AUTHMODE_PRIVATE, true); 
     50    } 
     51     
     52    private void loadDefaults() throws Exception { 
     53        this.add("0");          /* 1: CONNECTION_TYPE */ 
     54        this.add("0.0.0.0");    /* 2: INET_SERVER_HOST */ 
     55        this.add("0");          /* 3: INET_SERVER_PORT */ 
     56        this.add("00:00:00:00:00:00");  /* 4: BT_SERVER_ADDR */ 
     57        this.add("0");                  /* 5: BT_SERVER_PORT */ 
     58    } 
     59     
     60    private void add(String value) throws Exception { 
     61        int ret = storage.addRecord(value.getBytes(), 0, value.length()); 
     62        System.out.println("added #" + ret); 
    5963    } 
    6064     
    6165     
    62     /** 
    63      * Returns the configuration for "path" 
    64      * @return configuration for "path" 
    65      */ 
    66     public String getPath() { 
    67         return this.path; 
     66    public String get(int setting) { 
     67        byte[] value; 
     68         
     69        System.out.println("getting #" + setting); 
     70        try { 
     71            value = storage.getRecord(setting); 
     72        } catch (Exception e) { 
     73            /* 
     74             * Again, no matter which exception is thrown, we're screwed 
     75             * anyway - no we can just return null. (maybe signalise it to 
     76             * the user as well? well - TODO) 
     77             */ 
     78            e.printStackTrace(); 
     79            return null; 
     80        } 
     81         
     82        return new String(value); 
    6883    } 
    6984     
    70     /** 
    71      * Returns the configuration for "configFile". 
    72      * @return configuration for "configFile" 
    73      */ 
    74     public String getConfigFile() { 
    75         return this.configFile; 
    76     } 
    77      
    78     /** 
    79      * Returns the configuration for "serverHost". 
    80      * @return configuration for "serverHost" 
    81      */ 
    82     public String getServerHost() { 
    83         return this.serverHost; 
    84     } 
    85      
    86     /** 
    87      * Returns the configuration for "serverPort", converted to integer. 
    88      * @return configuration for "serverPort" 
    89      */ 
    90     public int getServerPort() { 
    91         return this.serverPort; 
    92     } 
    93      
    94     /** 
    95      * Returns the server host and server port concatenated as a single String 
    96      * so it can be used for opening sockets. 
    97      * @return serverHost:serverPort 
    98      */ 
    99     public String getServerString() { 
    100         return this.serverHost + ":" + this.serverPort; 
    101     } 
    102      
    103     /** 
    104      * Returns the configuration value for a given setting as a String. 
    105      * 
    106      * @param setting requested setting to return its value. 
    107      * @return value as a String, or an empty String if setting is unknown. 
    108      */ 
    109     public String getSettingString(String setting) { 
    110         if (setting == null) { 
    111             return ""; 
    112         } else if (setting.equalsIgnoreCase("path")) { 
    113             return this.path; 
    114         } else if (setting.equalsIgnoreCase("configFile")) { 
    115             return this.configFile; 
    116         } else if (setting.equalsIgnoreCase("Server Host") || setting.equalsIgnoreCase("serverHost")) { 
    117             return this.serverHost; 
    118         } else if (setting.equalsIgnoreCase("Server Port") || setting.equalsIgnoreCase("serverPort")) { 
    119             return String.valueOf(this.serverPort); 
     85    public String get(String settingString) { 
     86        if (settingString.equals("Connection Type")) { 
     87            return get(Settings.CONNECTION_TYPE); 
     88        } else if (settingString.equals("Inet Server Host")) { 
     89            return get(Settings.INET_SERVER_HOST); 
     90        } else if (settingString.equals("Inet Server Port")) { 
     91            return get(Settings.INET_SERVER_PORT); 
     92        } else if (settingString.equals("Bluetooth Server Address")) { 
     93            return get(Settings.BT_SERVER_ADDR); 
     94        } else if (settingString.equals("Bluetooth Server Port")) { 
     95            return get(Settings.BT_SERVER_PORT); 
    12096        } else { 
    121             return ""; 
     97            return null; 
    12298        } 
    12399    } 
    124100     
    125     /** 
    126      * Sets the given value of a given setting. 
    127      * If the setting doesn't exist, nothing is done. 
    128      * 
    129      * @param setting setting whose value is to be set 
    130      * @param value the actual value 
    131      */ 
    132     public void setValue(String setting, String value) { 
    133         System.out.println(setting + " now " + value); 
    134         if (setting.equalsIgnoreCase("Server Host") || setting.equalsIgnoreCase("serverHost")) { 
    135             this.serverHost = value; 
    136         } else if (setting.equalsIgnoreCase("Server Port") || setting.equalsIgnoreCase("serverPort")) { 
    137             this.serverPort = Util.atoi(value); 
     101     
     102    public void set(int setting, String value) throws Exception { 
     103        System.out.println("setting #" + setting + " -- " + value); 
     104        try { 
     105            storage.setRecord(setting, value.getBytes(), 0, value.length()); 
     106        } catch (Exception e) { 
     107            throw new Exception(e.getMessage() +  
     108                    "\nsetting: " + setting + "\nvalue: " + value); 
    138109        } 
    139110    } 
    140111     
    141     /** 
    142      * Writes setting values taken from a FormEntry object. 
    143      *  
    144      * @param entry FormScreen object holding the values. 
    145      */ 
    146     public void writeSettings(FormScreen form) { 
     112    public void set(String settingString, String value) throws Exception { 
     113        if (settingString.equals("Connection Type")) { 
     114            set(Settings.CONNECTION_TYPE, value); 
     115        } else if (settingString.equals("Inet Server Host")) { 
     116            set(Settings.INET_SERVER_HOST, value); 
     117        } else if (settingString.equals("Inet Server Port")) { 
     118            set(Settings.INET_SERVER_PORT, value); 
     119        } else if (settingString.equals("Bluetooth Server Address")) { 
     120            set(Settings.BT_SERVER_ADDR, value); 
     121        } else if (settingString.equals("Bluetooth Server Port")) { 
     122            set(Settings.BT_SERVER_PORT, value); 
     123        } else { 
     124            // ignore it 
     125        } 
     126    } 
     127     
     128    public void setFromForm(FormScreen form) throws Exception { 
    147129        String value = ""; 
    148130        for (int i = 0; i < form.size(); i++) { 
     
    150132            if (item instanceof TextField) { 
    151133                value = ((TextField) item).getString(); 
    152                 this.setValue(item.getLabel(), value); 
     134                this.set(item.getLabel(), value); 
    153135            } else if (item instanceof ChoiceGroup) { 
    154136                int index = ((ChoiceGroup) item).getSelectedIndex(); 
    155                 value = ((ChoiceGroup) item).getString(index); 
    156                 this.setValue(item.getLabel(), value); 
     137                //value = ((ChoiceGroup) item).getString(index); 
     138                this.set(item.getLabel(), Integer.toString(index)); 
    157139            } 
    158140        }