public void SendEvent() { /** * This class periodically publishes change events to the zone. This * method starts up the thread that publishes the change events. * * A normal SIF Agent would, instead look for changes in the application's database * and publish those as changes. */ Console.WriteLine ( "Event publishing enabled with an interval of " + EVENT_INTERVAL/1000 + " seconds." ); Random random = new Random(); bool isProcessing = true; // Go into a loop and send events while ( isProcessing ) { try { Thread.Sleep( EVENT_INTERVAL ); StudentPersonal changedObject = fData[random.Next( fData.Count )]; StudentPersonal eventObject = new StudentPersonal(); eventObject.RefId = changedObject.RefId; // Create a change event with a random Student ID; String newNum = "A" + random.Next( 999999 ).ToString(); eventObject.LocalId = newNum; fZone.ReportEvent( eventObject, EventAction.Change ); } catch ( Exception ex ) { Console.WriteLine( "Error during event processing: " + ex ); isProcessing = false; } } }
/// <summary> /// Process a response (of a request) for an StudentPersonal SIF Object. /// </summary> /// <param name="sifDataObject">StudentPersonal response received.</param> /// <param name="zone">Zone used.</param> protected override void ProcessResponse(StudentPersonal sifDataObject, IZone zone) { if (log.IsDebugEnabled) log.Debug("Received a request response for StudentPersonal in Zone " + zone.ZoneId + ":\n" + sifDataObject.ToXml()); }
private static StudentPersonal CreateStudent( String id, String lastName, String firstName, String street, String city, String state, CountryCode country, String post, String phone, Sex gender, YearLevelCode grade, String birthDateyyyyMMdd ) { StudentPersonal student = new StudentPersonal(); ; student.RefId = Adk.MakeGuid(); student.LocalId = id; PersonInfo stupersonal = new PersonInfo(); student.PersonInfo = stupersonal; // Set the Name Name name = new Name( NameType.LEGAL ); name.FamilyName = lastName; name.GivenName = firstName; stupersonal.Name = name; Address addr = new Address(); addr.SetType( AddressType.C0765_PHYSICAL_LOCATION ); addr.SetStreet( street ); addr.City = city; addr.StateProvince = state; addr.PostalCode = post; addr.Country = country.ToString(); stupersonal.AddressList = new AddressList( addr ); stupersonal.PhoneNumberList = new PhoneNumberList( new PhoneNumber( PhoneNumberType.PRIMARY, phone ) ); Demographics dem = new Demographics(); dem.SetSex( gender ); dem.BirthDate = DateTime.ParseExact ( birthDateyyyyMMdd, "yyyyMMdd", CultureInfo.InvariantCulture.DateTimeFormat ); stupersonal.Demographics = dem; return student; }
/// <summary> Respond to SIF Requests /// </summary> public virtual void OnRequest( IDataObjectOutputStream outStream, Query query, IZone zone, IMessageInfo inf ) { SifMessageInfo info = (SifMessageInfo) inf; SifWriter debug = new SifWriter( Console.Out ); Console.WriteLine ( "Received a request for " + query.ObjectTag + " from agent \"" + info.SourceId + "\" in zone " + zone.ZoneId ); // Tell the ADK to automatically filter out any objects that don't meet the requirements // of the query conditions outStream.Filter = query; // Read all learners from the database to populate a HashMap of // field/value pairs. The field names can be whatever we choose as long // as they match the field names used in the <mappings> section of the // agent.cfg configuration file. Each time a record is read, convert it // to a LearnerPersonal object using the Mappings class and stream it to // the supplied output stream. // IDictionary data = new Hashtable(); IDbCommand command = null; Console.WriteLine( "The SIF Request was requested in the following SIF Versions" ); foreach ( SifVersion version in info.SIFRequestVersions ) { Console.WriteLine( " - " + version ); } Console.WriteLine( "This agent will respond in its latest supported version, which is: " ); Console.WriteLine( " - " + info.LatestSIFRequestVersion ); // Get the root Mappings object from the configuration file Mappings m = fCfg.Mappings.GetMappings( "Default" ).Select ( info.SourceId, zone.ZoneId, info.LatestSIFRequestVersion ); // Ask the root Mappings instance to select a Mappings from its // hierarchy. For example, you might have customized the agent.cfg // file with mappings specific to zones, versions of SIF, or // requesting agents. The Mappings.selectOutbound() method will select // the most appropriate instance from the hierarchy given the // three parameters passed to it. MappingsContext mappings = m.SelectOutbound( StudentDTD.STUDENTPERSONAL, info ); try { int count = 0; // Query the database for all students command = fConn.CreateCommand(); fConn.Open(); command.CommandText = "SELECT * FROM Students"; using ( IDataReader rs = command.ExecuteReader( CommandBehavior.CloseConnection ) ) { DataReaderAdaptor dra = new DataReaderAdaptor( rs ); while ( rs.Read() ) { count++; // Finally, create a new LearnerPersonal object and ask the // Mappings to populate it with SIF elements from the HashMap // of field/value pairs. As long as there is an <object>/<field> // definition for each entry in the HashMap, the ADK will take // care of producing the appropriate SIF element/attribute in // the LearnerPersonal object. // StudentPersonal sp = new StudentPersonal(); sp.RefId = Adk.MakeGuid(); mappings.Map( sp, dra ); // Now write out the LearnerPersonal to the output stream and // we're done publishing this student. // Console.WriteLine( "\nThe agent has read these values from the database:" ); DumpDictionaryToConsole( data ); Console.WriteLine( "To produce this LearnerPersonal object:" ); debug.Write( sp ); debug.Flush(); outStream.Write( sp ); data.Clear(); } rs.Close(); } Console.WriteLine ( "- Returned " + count + " records from the Student database in response" ); } catch ( Exception ex ) { Console.WriteLine( "- Returning a SIF_Error response: " + ex.ToString() ); throw new SifException ( SifErrorCategoryCode.RequestResponse, SifErrorCodes.REQRSP_GENERIC_ERROR_1, "An error occurred while querying the database for students", ex.ToString(), zone ); } finally { if ( command != null ) { try { fConn.Close(); } catch ( Exception ignored ) { } } } }