public void TestStatus() { XElement nav = XElement.Load(CaseFile("Status.xml")); SyncMLStatus f = SyncMLStatus.Create(nav); Assert.IsTrue(CompareXml(nav, f.Xml), f.Xml.ToString()); }
/// <summary> /// Called by ProcessResponseForXXX in order to create status for Alert returned by server. /// </summary> /// <param name="alert"></param> protected void PrepareStatusForReturnedAlert(SyncMLAlert alert) { switch (alert.Data.Content) { case "200": case "201": case "202": case "203": case "204": case "205": SyncMLStatus responseStatus = SyncMLStatus.Create(); //status.CmdID is not defined here, but only defined right before sending next message responseStatus.MsgRef.Content = ServerSyncML.Hdr.MsgID.Content; responseStatus.Data.Content = "200"; responseStatus.Cmd.Content = "Alert"; responseStatus.CmdRef.Content = alert.CmdID.Content; responseStatus.TargetRefCollection.Add(SyncMLSimpleElementFactory.Create <SyncMLTargetRef>(Facade.ContactDataSourceAtServer)); responseStatus.SourceRefCollection.Add(SyncMLSimpleElementFactory.Create <SyncMLSourceRef>(Facade.LocalDataSource.DataSourceName)); //Get alert anchor next SyncMLItem alertItem = alert.ItemCollection[0]; //assuming there's always one. SyncMLMeta alertItemMeta = alertItem.Meta; // assuming there's always one. MetaParser metaParser = new MetaParser(alertItemMeta.Xml); MetaAnchor alertAnchor = metaParser.GetMetaAnchor(); string alertAnchorNext = alertAnchor.Next.Content; SyncMLItem statusItem = SyncMLItem.Create(); MetaAnchor statusAnchor = MetaAnchor.Create(); statusAnchor.Next = SyncMLSimpleElementFactory.Create <MetaNext>(alertAnchorNext); statusItem.Data.Xml.Add(statusAnchor.Xml); responseStatus.ItemCollection.Add(statusItem); Facade.ResponseCommandPool.Add(responseStatus); break; case "100": //Show. The Data element type contains content information that should be processed and displayed through the user agent. string serverStatusMessage = alert.ItemCollection[0].Data.Content; Facade.DisplayOperationMessage(serverStatusMessage); SyncMLStatus statusFor100 = SyncMLStatus.Create(); statusFor100.MsgRef.Content = ServerSyncML.Hdr.MsgID.Content; statusFor100.Data.Content = "200"; statusFor100.Cmd.Content = "Alert"; statusFor100.CmdRef.Content = alert.CmdID.Content; Facade.ResponseCommandPool.Add(statusFor100); break; default: Trace.TraceInformation("Do not know what to do in PrepareStatusForReturnedAlert:"); Trace.TraceInformation(alert.Xml.ToString()); break; } }
/// <summary> /// Generate status commands for received Sync command, and put the commands into the pool. /// </summary> /// <param name="syncCommand">Sync command from the server.</param> private void GenerateStatusCommandsForSyncCommand(SyncMLSync syncCommand) { SyncMLStatus syncStatus = SyncMLStatus.Create(); syncStatus.Cmd.Content = "Sync"; syncStatus.CmdRef.Content = syncCommand.CmdID.Content; syncStatus.Data.Content = "200"; syncStatus.MsgRef.Content = ServerSyncML.Hdr.MsgID.Content; syncStatus.TargetRefCollection.Add(SyncMLSimpleElementFactory.Create <SyncMLTargetRef>(syncCommand.Target.LocURI.Content)); syncStatus.SourceRefCollection.Add(SyncMLSimpleElementFactory.Create <SyncMLSourceRef>(syncCommand.Source.LocURI.Content)); Facade.ResponseCommandPool.Add(syncStatus); Collection <SyncMLCommand> commands = syncCommand.Commands; if (commands != null) { foreach (SyncMLCommand command in commands) { SyncMLAdd addCommand = command as SyncMLAdd; if (addCommand != null) { SyncMLStatus addStatus = SyncMLStatus.Create(); addStatus.Cmd.Content = "Add"; addStatus.CmdRef.Content = command.CmdID.Content; addStatus.Data.Content = "200"; addStatus.MsgRef.Content = syncStatus.MsgRef.Content; addStatus.SourceRefCollection.Add(SyncMLSimpleElementFactory.Create <SyncMLSourceRef>(addCommand.ItemCollection[0].Source.LocURI.Content)); Facade.ResponseCommandPool.Add(addStatus); continue; } SyncMLReplace replaceCommand = command as SyncMLReplace; if (replaceCommand != null) { SyncMLStatus replaceStatus = SyncMLStatus.Create(); replaceStatus.Cmd.Content = "Replace"; replaceStatus.CmdRef.Content = command.CmdID.Content; replaceStatus.Data.Content = "200"; replaceStatus.MsgRef.Content = syncStatus.MsgRef.Content; replaceStatus.TargetRefCollection.Add(SyncMLSimpleElementFactory.Create <SyncMLTargetRef>(replaceCommand.ItemCollection[0].Target.LocURI.Content)); Facade.ResponseCommandPool.Add(replaceStatus); continue; } SyncMLDelete deleteCommand = command as SyncMLDelete; if (deleteCommand != null) { SyncMLStatus deleteStatus = SyncMLStatus.Create(); deleteStatus.Cmd.Content = "Delete"; deleteStatus.CmdRef.Content = command.CmdID.Content; deleteStatus.Data.Content = "200"; deleteStatus.MsgRef.Content = syncStatus.MsgRef.Content; deleteStatus.SourceRefCollection.Add(SyncMLSimpleElementFactory.Create <SyncMLSourceRef>(deleteCommand.ItemCollection[0].Target.LocURI.Content)); Facade.ResponseCommandPool.Add(deleteStatus); continue; } } } }
protected override bool ProcessResponse(string text) { if (!base.ProcessResponse(text)) { return(false); } // So now syncml model is created from text UpdateCurrentURI(ServerSyncML); //0: Always have a status response to the SyncHdr of the server message. However, this status may not be sent back if it is the only one in the queue. SyncMLStatus responseStatus = SyncMLStatus.Create(); responseStatus.MsgRef.Content = ServerSyncML.Hdr.MsgID.Content; responseStatus.Data.Content = "200"; responseStatus.Cmd.Content = "SyncHdr"; responseStatus.CmdRef.Content = "0"; responseStatus.TargetRefCollection.Add(SyncMLSimpleElementFactory.Create <SyncMLTargetRef>(ServerSyncML.Hdr.Target.LocURI.Content)); responseStatus.SourceRefCollection.Add(SyncMLSimpleElementFactory.Create <SyncMLSourceRef>(ServerSyncML.Hdr.Source.LocURI.Content)); Facade.ResponseCommandPool.Add(responseStatus);//respond in next request. //1: Handle returned status commands Collection <SyncMLStatus> serverStatusCommands = AccessBody.GetStatusCommands(ServerSyncML); foreach (SyncMLStatus status in serverStatusCommands) { CommandAndStatusRegister.RegisterStatus(status.CmdRef.Content, status.Data.Content); HandleServerStatus(status); //this fn is still abstract here. Derived classes have different ways of handling. } //2: Prepare status commands for returned Alert commands. Derived classes then handle the alerts all the same way. Collection <SyncMLAlert> serverAlertCommands = AccessBody.GetAlertCommands(ServerSyncML); foreach (SyncMLAlert alert in serverAlertCommands) { Debug.WriteLine("Alert:" + alert.Xml.ToString()); PrepareStatusForReturnedAlert(alert); } //3: Handle returned Sync command. Derived classes handle sync commands the same way. SyncMLSync serverSyncCommand = AccessBody.GetSyncCommand(ServerSyncML); if (serverSyncCommand != null) { string numberOfChangesStr = serverSyncCommand.NumberOfChanges.Content; if (!String.IsNullOrEmpty(numberOfChangesStr)) {//where numberOfChanges > 0, display progress bar in GUI. Facade.totalNumberOfChangesReceiving = Convert.ToInt32(numberOfChangesStr); if (Facade.totalNumberOfChangesReceiving > 0) { Facade.DisplayOperationMessage(String.Format("Total number of changes received from the server: {0}", numberOfChangesStr)); Facade.InitProgressBarReceiving(0, Facade.totalNumberOfChangesReceiving, 1); Facade.DisplayStageMessageReceiving(String.Format("Receiving {0} updates ...", Facade.totalNumberOfChangesReceiving)); } } int numbersOfChangesThisMessage = serverSyncCommand.Commands.Count; // the server might send in multiple messages Facade.numberOfChangesReceived += numbersOfChangesThisMessage; if (Facade.numberOfChangesReceived == Facade.totalNumberOfChangesReceiving) { Facade.DisplayStageMessageReceiving("Receiving Done"); } GenerateStatusCommandsForSyncCommand(serverSyncCommand); if (Facade.GracefulStop) { return(true); // simply return, ture of false is meaningless. } Facade.IncrementProgressBarReceiving(numbersOfChangesThisMessage); ApplySyncCommandToLocal(serverSyncCommand); } //4: Verify if the server return all status codes to commands sent if (!CommandAndStatusRegister.IsAllCommandsReturnedWithStatus()) { Trace.TraceInformation("!!!! Not all commands got status code. Please check the log for details."); Trace.TraceInformation("Commands without status: " + CommandAndStatusRegister.CommandsXmlWithoutStatus); //It is expected CommandAndStatusRegister is not used any more, otherwise, should clear it here. } //5: At the end, do what the server ask to do, likely a new SyncML message to be sent ProcessServerAlertCommands(serverAlertCommands); return(true); }