// SNMP application class. // Copyright (C) 2009-2010 Lex Li // // Permission is hereby granted, free of charge, to any person obtaining a copy of this // software and associated documentation files (the "Software"), to deal in the Software // without restriction, including without limitation the rights to use, copy, modify, merge, // publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons // to whom the Software is furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in all copies or // substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE // FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. namespace GSF.Net.Snmp.Pipeline { /// /// SNMP application class, who is a pipeline for message processing. /// public sealed class SnmpApplication { private readonly ILogger _logger; private readonly IMembershipProvider _provider; private readonly MessageHandlerFactory _factory; private readonly ObjectStore _store; private readonly SnmpApplicationFactory _owner; private IMessageHandler _handler; /// /// Initializes a new instance of the class. /// /// The owner. /// The logger. /// The store. /// The provider. /// The factory. public SnmpApplication(SnmpApplicationFactory owner, ILogger logger, ObjectStore store, IMembershipProvider provider, MessageHandlerFactory factory) { _owner = owner; _provider = provider; _logger = logger; _store = store; _factory = factory; } /// /// Initializes the specified context. /// /// The context. public void Init(ISnmpContext context) { Context = context; ProcessingFinished = false; _handler = null; } /// /// Gets the context. /// /// The context. public ISnmpContext Context { get; private set; } /// /// Gets a value indicating whether processing is finished. /// /// true if processing is finished; otherwise, false. public bool ProcessingFinished { get; private set; } /// /// Processes an incoming request. /// public void Process() { OnAuthenticateRequest(); // TODO: add authorization. OnMapRequestHandler(); OnRequestHandlerExecute(); OnLogRequest(); _owner.Reuse(this); } private void OnRequestHandlerExecute() { if (ProcessingFinished) { return; } _handler.Handle(Context, _store); } private void OnMapRequestHandler() { if (ProcessingFinished) { return; } _handler = _factory.GetHandler(Context.Request); if (_handler is NullMessageHandler) { // TODO: handle error here. CompleteProcessing(); } } private void OnAuthenticateRequest() { if (!_provider.AuthenticateRequest(Context)) { // TODO: handle error here. // return TRAP saying authenticationFailed. CompleteProcessing(); } if (Context.Response != null) { CompleteProcessing(); } } private void OnLogRequest() { Context.SendResponse(); if (_logger == null) { return; } _logger.Log(Context); } /// /// Completes the processing. /// public void CompleteProcessing() { ProcessingFinished = true; } } }