Java, Synchronization, & the PalmPilot
by Tilo Christ


Listing One
/* samplecode/dlptest.java:
* Copyright (C) 1998, Tilo Christ
* This is free software, licensed under the GNU Public License V2.
* See the file COPYING for details.
*/
/* $Id: dlptest.java,v 1.1 1998/09/16 08:57:51 christ Exp $ */

import com.syncbuilder.sync.*;
import com.syncbuilder.storage.*;
import com.syncbuilder.device.*;

/** A test for the PalmOS DLP-functionality. This is also a sanity check 
for the new Pure Java Implementation of the DLP.
* @author Tilo Christ
*/
public class dlptest
{
    public static void main(String[] args)
    {
        try
        {
        String port = null;
        System.out.print("Port to use (\".\" means 
                                      Network HotSync) [/dev/pilot]? ");
        System.out.flush();
        port = com.syncbuilder.util.Util.readLine();
        if (port.equals("")) 
            port = "/dev/pilot";

        if (port.equals("."))
        {
            Socket.setSocketFactory( new NetworkHSSocketImplFactory() );
            System.out.println("Initiate Network HotSync now."); 
        }
        else
        {
            System.out.println("Please hit the HotSync button now."); 
        }
        ServerSocket ssock = new ServerSocket(port);
        Link link = ssock.accept();
        Dlp dlp = link.getDlp();
        System.out.println("The connection-protocol has a version of: " 
                                               + link.getProtocolVersion());
        System.out.println("Information about the HotSync user will");
        System.out.println("be read from your device");
        System.out.println("as soon as you start the action.");
        System.out.println("Press <RETURN> to start the action...");
        com.syncbuilder.util.Util.readLine();

        UserInfo uinfo = dlp.getUserInfo();
        System.out.println( uinfo.toString() );

        System.out.println("The user-info should have been displayed now.");
        System.out.println("Press <RETURN> to continue...");
        com.syncbuilder.util.Util.readLine();
        
        System.out.println("The message on your PalmOS device should");
        System.out.println("switch from 'Identifying User' 
                                                      to 'Synchronizing'");
        System.out.println("as soon as you start the action.");
        System.out.println("Press <RETURN> to start the action...");
        com.syncbuilder.util.Util.readLine();

        dlp.switchMessage();
        
        System.out.println("The message should have switched now.");
        System.out.println("Press <RETURN> to continue...");
        com.syncbuilder.util.Util.readLine();
        System.out.println("The message on your PalmOS device should");
        System.out.println("switch from 'Synchronizing' to 
                                              'Synchronizing Datebook'");
        System.out.println("as soon as you start the action.");
        System.out.println("Press <RETURN> to start the action...");
        com.syncbuilder.util.Util.readLine();

        Database dbase = dlp.openStockDB
                 (new com.syncbuilder.storage.appointment.DatabaseImpl());
        System.out.println("The message should have switched now.");
        System.out.println("Press <RETURN> to continue...");
        com.syncbuilder.util.Util.readLine();

        System.out.println("All " + dbase.getRecordCount() + " 
                  entries from your datebook will be read and displayed");
        System.out.println("as soon as you start the action.");
        System.out.println("Press <RETURN> to start the action...");
        com.syncbuilder.util.Util.readLine();

        for (int i = 0;; i++)
        {
            Record rec = dbase.getRecord(i);
            if (rec == null)
                break;
            System.out.println( rec.toString() );
        }
        System.out.println("The records should have been printed now.");
        System.out.println("Press <RETURN> to continue...");
        com.syncbuilder.util.Util.readLine();

        System.out.println("A test entry will be added to 
                                    your datebook at the current date");
        System.out.println("as soon as you start the action.");
        System.out.println("Press <RETURN> to start the action...");
        com.syncbuilder.util.Util.readLine();

        com.syncbuilder.storage.Record record = dbase.createRecord();
        ((com.syncbuilder.storage.appointment.Record)record).description = 
                                                 "Dinner at the White House";
        dbase.putRecord(record);

        System.out.println("The record should have been added 
                          now with UID " + record.getID().getValue() + ".");
        System.out.println("Press <RETURN> to continue...");
        com.syncbuilder.util.Util.readLine();

        dlp.endHotSync();
        }
        catch(Throwable t)
        {
            t.printStackTrace();
        }
    }
}


Listing Two
/* samplecode/SyncServerTest.java:
* Copyright (C) 1998, Tilo Christ
* This is free software, licensed under the GNU Public License V2.
* See the file COPYING for details.

*/
/* $Id$ */

import com.syncbuilder.sync.*;
import com.syncbuilder.device.*;

/** Demonstrates use of the com.syncbuilder.sync.SyncServer-class.
* Will not work through PalmPilot's regular HotSync app; only through
* the Network HotSync app (obtain separately from Palm Computing).
* @author Tilo Christ
*/
public class SyncServerTest
{
    public static void main(String[] args)
    {
        try
        {
        // Create a ServerSocket for use with Network HotSync.
        Socket.setSocketFactory( new NetworkHSSocketImplFactory() );
        ServerSocket ssock = new ServerSocket(null);

        // Create the SyncServer from the ServerSocket. The SyncServer
        // will serve all clients using SimpleHandler (see the class
        // SimpleHandler below). SimpleHandler will mostly print diagnostic
        // messages and read the UserInfo from the device.
        SyncServer sync_server = new SyncServer(ssock, new SimpleHandler()); 
        System.err.println("Initiate the Network HotSync procedures now."); 
        // The SyncServer starts running NOW!
        sync_server.start();
        System.err.println("Press <RETURN> to stop the service");
        com.syncbuilder.util.Util.readLine(); 
        // The SyncServer is shut down
        sync_server.stop();
        }
        catch(Throwable t)
        {
            t.printStackTrace();
        }
    }
}
  /** Non-public class. Used by SyncServer to handle incoming connections. */
  class SimpleHandler
        implements com.syncbuilder.sync.SyncHandler
  {
        /** Invoked one time at start-up */
        public void init()
        {

           System.err.println("SimpleHandler.init()");
        }
        /** Invoked every time a device connects */     
        public void service(Link link)
            throws Exception
        {
            System.err.println("SimpleHandler.service() with " 
                                                  + link.toString() );
            Dlp dlp = link.getDlp();
            // Do it ten times, so it takes more time...
            for (int i=0; i < 10; i++)
            {
                UserInfo uinfo = dlp.getUserInfo();
                System.err.println( uinfo.toString() );
            }
            dlp.endHotSync();
        }
        /** Invoked one time at shutdown */     
        public void destroy()
        {
            System.err.println("SimpleHandler.destroy()");
        }
    }


5


