public void OnRequest( IDataObjectOutputStream outStream,
                               Query query,
                               IZone zone,
                               IMessageInfo info )
        {
            // To be a successful publisher of data using the ADK, follow these steps

            // 1) Examine the query conditions. If they are too complex for your agent,
            // 	throw the appropriate SIFException

            // This example agent uses the autoFilter() capability of DataObjectOutputStream. Using
            // this capability, any object can be written to the output stream and the stream will
            // filter out any objects that don't meet the conditions of the Query. However, a more
            // robust agent with large amounts of data would want to pre-filter the data when it does its
            // initial database query.
            outStream.Filter = query;

            Console.WriteLine( "Responding to SIF_Request for StudentPersonal" );

            // 2) Write any data to the output stream
            foreach ( StudentPersonal sp in fData )
            {
                outStream.Write( sp );
            }
        }
        public void OnRequest(IDataObjectOutputStream outStream,
                              Query query,
                              IZone zone,
                              IMessageInfo info)
        {
            // To be a successful publisher of data using the ADK, follow these steps

            // 1) Examine the query conditions. If they are too complex for your agent,
            //  throw the appropriate SIFException

            // This example agent uses the autoFilter() capability of DataObjectOutputStream. Using
            // this capability, any object can be written to the output stream and the stream will
            // filter out any objects that don't meet the conditions of the Query. However, a more
            // robust agent with large amounts of data would want to pre-filter the data when it does its
            // initial database query.
            outStream.Filter = query;

            Console.WriteLine("Responding to SIF_Request for StudentPersonal");

            // 2) Write any data to the output stream
            foreach (StudentPersonal sp in fData)
            {
                outStream.Write(sp);
            }
        }
Example #3
0
        private void doBehavior(IZone zone, String reportObjectRefId, IDataObjectOutputStream reportStream)
        {
            fWasCalled = true;
            if ((Behavior & HandlerBehavior.WaitForPulse) != 0)
            {
                Console.WriteLine("Signaling...");
                fSignalObject.Set();
                Console.WriteLine("Waiting...");
                fWaitForObject.WaitOne();
                Console.WriteLine("Resuming publishing...");
            }

            if ((Behavior & HandlerBehavior.ThrowException) != 0)
            {
                AdkException exc = new AdkException("Errors Occurred", zone);
                throw exc;
            }
            if ((Behavior & HandlerBehavior.ThrowADKRetryException) != 0)
            {
                AdkException exc = new AdkException("Errors Occurred", zone);
                exc.Retry = true;
                throw exc;
            }
            if ((Behavior & HandlerBehavior.ThrowSIFRetryException) != 0)
            {
                AdkException exc =
                    new SifException(SifErrorCategoryCode.Transport, SifErrorCodes.GENERIC_GENERIC_ERROR_1,
                                     "Errors Occurred", zone);
                exc.Retry = true;
                throw exc;
            }
            if ((Behavior & HandlerBehavior.ThrowNullPointerException) != 0)
            {
                throw new NullReferenceException("Bogus!");
            }
        }
Example #4
0
        /// <summary>
        /// Handler for SIF Request messages from the zone.
        /// </summary>
        /// <param name="outStream">The output stream to send SIF Data Object results to.</param>
        /// <param name="query">The query conditions for the SIF Request.</param>
        /// <param name="zone">The zone this SIF Request was received on.</param>
        /// <param name="info">Provides protocol-specific information about the message.</param>
        void IPublisher.OnRequest(IDataObjectOutputStream outStream, Query query, IZone zone, IMessageInfo info)
        {
            if (log.IsInfoEnabled)
            {
                log.Info(this.GetType().Name + " received SIF Request for " + query.ObjectTag + " from agent " + ((SifMessageInfo)info).SourceId + " in zone " + zone.ZoneId + ".");
            }

            // Use the autoFilter() capability of DataObjectOutputStream so that any object can be written to the
            // output stream and the stream will filter out any objects that don't meet the conditions of the Query.
            // This filter is applied in conjunction with any filtering that may have occurred with a call to
            // GetSifResponses().
            outStream.Filter = query;

            try
            {
                ISifResponseIterator <T> iterator = GetSifResponses(query, zone);

                if (iterator == null)
                {
                    if (log.IsWarnEnabled)
                    {
                        log.Warn("getSifResponses() for Publisher " + this.GetType().Name + " returned null.");
                    }
                }
                else
                {
                    int successCount = 0;
                    int failureCount = 0;

                    while (iterator.HasNextResponse())
                    {
                        T sifDataObject = null;

                        try
                        {
                            sifDataObject = iterator.GetNextResponse();

                            if (sifDataObject == null)
                            {
                                if (log.IsWarnEnabled)
                                {
                                    log.Warn("iterator.GetNextResponse() has returned True, but iterator.GetNextResponse() has returned null. The SIF Data Object has been ignored.");
                                }
                                failureCount++;
                            }
                            else
                            {
                                try
                                {
                                    // Write the SIF Data Object to the zone via the output stream.
                                    outStream.Write(sifDataObject);
                                    successCount++;
                                }
                                catch (Exception e)
                                {
                                    failureCount++;
                                    if (log.IsErrorEnabled)
                                    {
                                        log.Error("The requested SIF Data Object ...\n" + sifDataObject.ToXml() + "\nwas not sent because it returned the following error ...\n" + e.Message, e);
                                    }
                                    ;
                                }
                            }
                        }
                        catch (Exception e)
                        {
                            if (log.IsWarnEnabled)
                            {
                                log.Warn("SIF Response has been ignored because iterator.getNextResponse() failed.", e);
                            }
                            failureCount++;
                        }
                    }

                    if (log.IsInfoEnabled)
                    {
                        log.Info(this.GetType().Name + " successfully responded to a SIF Request for " + this.SifObjectType.Name + " to zone " + zone.ZoneId + " with " + successCount + " SIF Data Objects.");
                    }
                    if (log.IsInfoEnabled && failureCount > 0)
                    {
                        log.Info(failureCount + " SIF Data Objects failed to be sent for " + this.SifObjectType.Name + " to zone " + zone.ZoneId + ".");
                    }
                }
            }
            catch (Exception e)
            {
                if (log.IsErrorEnabled)
                {
                    log.Error("Error responding to SIF Request: " + e.Message, e);
                }
            }
        }
Example #5
0
    /// <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 )
                {
                }
            }
        }
    }
Example #6
0
        /// <summary>  Respond to SIF RequestsGetTopicMap
        /// </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);

            //  Read all students 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 StudentPersonal object using the Mappings class and stream it to
            //  the supplied output stream.
            //
            IDbCommand command = null;

            // Set a basic filter on the outgoing data stream
            // What will happen is that any object written to the output stream will
            // be evaluated based on the query conditions. If the object doesn't meet the
            // query conditions, it will be excluded
            outStream.Filter = query;

            //  Get the root Mappings object from the configuration file
            Edustructures.SifWorks.Tools.Mapping.Mappings m = fCfg.Mappings.GetMappings("Default");

            //  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.select() method will select
            //  the most appropriate instance from the hierarchy given the
            //  three parameters passed to it.
            //
            //IDictionary<string, string> dataMap = new System.Collections.Generic.Dictionary<string, string>();
            // IFieldAdaptor adaptor = new StringMapAdaptor(dataMap);
            //m.MapOutbound();
            MappingsContext mc = 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()) {

                        //  Finally, create a new StudentPersonal 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 StudentPersonal object.
                        //
                        StudentPersonal sp = new StudentPersonal();
                        sp.RefId = Adk.MakeGuid();
                        // TODO: When using custom macros for outboud mapping operations, set the ValueBuilder.
                        // You will need to call SetValueBuilder() giving the MappingsContext a derived version
                        // of DefaultValueBuilder that has the macro methods available in it.
                        mc.SetValueBuilder(new DataUtilMacro(dra));
                        mc.Map(sp, dra);

                        //  Now write out the StudentPersonal to the output stream and
                        //  we're done publishing this student.
                        //
                        Console.WriteLine("\nThe agent has read these values from the database:");

                        DumpFieldsToConsole(rs);
                        Console.WriteLine("To produce this StudentPersonal object:");
                        debug.Write(sp);
                        debug.Flush();

                        outStream.Write(sp);
                    }

                    rs.Close();
                }
                Console.WriteLine
                    ("- Returned " + count + " records from the Student database in response");
            } catch (Exception ex) {
                Console.WriteLine("- Returning a SIF_Error response: " + ex);
                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) {
                        Log.Warn(ignored.Message, ignored);
                    }
                }
            }
        }
Example #7
0
    /// <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)
                {
                }
            }
        }
    }
Example #8
0
        /// <summary>  Respond to SIF RequestsGetTopicMap
        /// </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);

            //  Read all students 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 StudentPersonal object using the Mappings class and stream it to
            //  the supplied output stream.
            //
            IDbCommand command = null;

            // Set a basic filter on the outgoing data stream
            // What will happen is that any object written to the output stream will
            // be evaluated based on the query conditions. If the object doesn't meet the
            // query conditions, it will be excluded
            outStream.Filter = query;

            //  Get the root Mappings object from the configuration file
            Edustructures.SifWorks.Tools.Mapping.Mappings m = fCfg.Mappings.GetMappings("Default");

            //  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.select() method will select
            //  the most appropriate instance from the hierarchy given the
            //  three parameters passed to it.
            //
            //IDictionary<string, string> dataMap = new System.Collections.Generic.Dictionary<string, string>();
            // IFieldAdaptor adaptor = new StringMapAdaptor(dataMap);
            //m.MapOutbound();
            MappingsContext mc = 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())
                    {
                        //  Finally, create a new StudentPersonal 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 StudentPersonal object.
                        //
                        StudentPersonal sp = new StudentPersonal();
                        sp.RefId = Adk.MakeGuid();
                        // TODO: When using custom macros for outboud mapping operations, set the ValueBuilder.
                        // You will need to call SetValueBuilder() giving the MappingsContext a derived version
                        // of DefaultValueBuilder that has the macro methods available in it.
                        mc.SetValueBuilder(new DataUtilMacro(dra));
                        mc.Map(sp, dra);

                        //  Now write out the StudentPersonal to the output stream and
                        //  we're done publishing this student.
                        //
                        Console.WriteLine("\nThe agent has read these values from the database:");

                        DumpFieldsToConsole(rs);
                        Console.WriteLine("To produce this StudentPersonal object:");
                        debug.Write(sp);
                        debug.Flush();

                        outStream.Write(sp);
                    }

                    rs.Close();
                }
                Console.WriteLine
                    ("- Returned " + count + " records from the Student database in response");
            } catch (Exception ex) {
                Console.WriteLine("- Returning a SIF_Error response: " + ex);
                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) {
                        Log.Warn(ignored.Message, ignored);
                    }
                }
            }
        }
Example #9
0
 public void OnReportRequest(String reportObjectRefId, IDataObjectOutputStream stream, Query query, IZone zone,
                             IMessageInfo info)
 {
     doBehavior(zone);
     doBehavior(zone, reportObjectRefId, stream);
 }
Example #10
0
 public void OnRequest(IDataObjectOutputStream stream, Query query, IZone zone, IMessageInfo info)
 {
     doBehavior(zone);
 }