Implementing SyncNow for MBS Applications

You can implement SyncNow for message-based synchronization applications for Windows or Windows Mobile using the Sybase.Persistence.CallbackHandler. The onConnectionStatus methods provide the implementation.

  1. In Mobile Application Diagram, generate Windows Mobile code. Select Message-based as one of the configuration parameters.
  2. In the generated code, call the client API and implement Callback OnSynchronize() to implement the SyncNow functionality. See Developer Reference for Windows and Windows Mobile > Reference > Windows Mobile Client Object API > Utility APIs > Callback Handlers.

    A sample SyncNow method looks like:

    public void SyncNow()
    {
         int connStatus = Sybase.Persistence.MessagingClient.ConnectionStatus;
         int MAX_WAIT_TIME_OUT = 300; // 5 minutes
         int timeout = 0;
         while (connStatus != DeviceConnectionStatus.CONNECTED)
         {
               timeout++;
               Thread.Sleep(1000);
               if (timeout >= MAX_WAIT_TIME_OUT)
               {
                    throw new Sybase.Persistence.PersistenceException("Waiting timeout: " + MAX_WAIT_TIME_OUT + " seconds");
               }
               connStatus = Sybase.Persistence.MessagingClient.ConnectionStatus;
         }
    
         if (!DsTestDB.IsSubscribed())
         {
               DsTestDB.LoginToSync("test", "test123");
               DsTestDB.Subscribe();
         }
         DsTestDB.BeginSynchronize();
         int MAX_SYNC_TIMEOUT = 3600; // one hour ? 
         timeout = 0;
         while (!TestMain.syncFinished)
         {
               timeout++;
               Thread.Sleep(1000);
               if (timeout >= MAX_SYNC_TIMEOUT)
               {
                   throw new Sybase.Persistence.PersistenceException("Waiting SYNC timeout: " + MAX_SYNC_TIMEOUT + "seconds");
               }
         }
    }
  3. To determine whether all data is successfully downloaded or uploaded, the client application can send a synchronize message to the server, and should receive a callback. (The client must subscribe to the package as shown above). A sample callback looks similar to:
    public class TextResponseHandler : DefaultCallbackHandler
    {
         public override void OnConnectionStatusChange(int connStatus, int connType, int errorCode, string errorMessage)
         {
               Console.WriteLine("Device Connection status changed to : " + connStatus);
               if (errorMessage != null && errorMessage.Length > 0)
               {
                    Console.WriteLine("Connection error: " + errorMessage);
               }
         }
    
         public override SynchronizationAction OnSynchronize(Sybase.Collections.GenericList<ISynchronizationGroup> groups, Sybase.Persistence.SynchronizationContext context)
         {
               Console.WriteLine("synchronize request processed returned. ");
               SynchronizationStatus status = context.Status;
               if (status == SynchronizationStatus.FINISHING)
               {
                    Console.WriteLine("synchronizeResult");
               }
               else if (status == SynchronizationStatus.ERROR)
               {
                    Console.WriteLine("synchronizeFailed");
               }
               lock (typeof(TestMain))
               {
                    TestMain.syncFinished = true;
               }
               return SynchronizationAction.CONTINUE;
         }
       ......
    }
  4. On the device, two options are available to view error messages related to connection status:
    • Using the callback, the connection error messages appear on the device:
      public override void OnConnectionStatusChange(int connStatus, int connType, int errorCode, string errorMessage)
      {
            Console.WriteLine("Device Connection status changed to : " + connStatus);
            if (errorMessage != null && errorMessage.Length > 0)
            {
                 Console.WriteLine("Connection error: " + errorMessage);
            }
      }
    • The user can access Sybase Settings > Show Log to see the detail device status.