#region
/* -----------------------------------------------------------------------
*
*
* Copyright 2011-2012 GRNET S.A. All rights reserved.
*
* Redistribution and use in source and binary forms, with or
* without modification, are permitted provided that the following
* conditions are met:
*
* 1. Redistributions of source code must retain the above
* copyright notice, this list of conditions and the following
* disclaimer.
*
* 2. Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials
* provided with the distribution.
*
*
* THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and
* documentation are those of the authors and should not be
* interpreted as representing official policies, either expressed
* or implied, of GRNET S.A.
*
* -----------------------------------------------------------------------
*/
#endregion
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.IO;
using System.Linq;
using System.Reflection;
using log4net;
namespace Pithos.Core.Agents
{
public class FileEventIdleBatch
{
//Requirements
//------------
//* The class should group workflow actions according to file name and type
//* The class should pass all batched actions to a continuation when
//* the batch is triggered.
//* Actions should not survice a trigger
private static readonly ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
private readonly IdleBatch _idleBatch;
private readonly Action> _callback;
private PollAgent _pollAgent;
public FileEventIdleBatch(PollAgent pollAgent, int idleTimeout, Action> callback)
{
_pollAgent = pollAgent;
_callback = callback;
_idleBatch = new IdleBatch(idleTimeout, Process);
}
public void Post(FileSystemEventArgs arg)
{
if (arg is RenamedEventArgs)
throw new ArgumentException("arg");
Contract.EndContractBlock();
if (Log.IsDebugEnabled)
Log.DebugFormat("Batch Post {0}:{1}", arg.ChangeType.ToString("g"), arg.FullPath);
_pollAgent.Pause =true;
_idleBatch.Post(arg);
}
public void ForceTrigger()
{
_idleBatch.ForceTrigger();
}
protected virtual void Process(FileSystemEventArgs[] args)
{
var dict = new Dictionary();
foreach (var arg in args)
{
if (arg.ChangeType == WatcherChangeTypes.Created )
dict[arg.FullPath] = new[] { arg };
else if (arg.ChangeType == WatcherChangeTypes.Renamed)
{
var renamedArg = (MovedEventArgs)arg;
FileSystemEventArgs[] oldValue;
if (dict.TryGetValue(renamedArg.OldFullPath, out oldValue))
{
dict.Remove(renamedArg.OldFullPath);
var last = oldValue.Last();
if (last.ChangeType == WatcherChangeTypes.Created)
{
var createRenamed = new FileSystemEventArgs(WatcherChangeTypes.Created, Path.GetDirectoryName(renamedArg.FullPath),
renamedArg.Name);
dict[renamedArg.FullPath] = new[] {createRenamed};
}
else
dict[renamedArg.FullPath] = new[] { renamedArg };
}
else
dict[renamedArg.FullPath] = new[] { renamedArg};
}
else
{
FileSystemEventArgs[] oldValue;
var hasValue=dict.TryGetValue(arg.FullPath, out oldValue);
var last = hasValue?oldValue.LastOrDefault():null;
if (last==null || last.ChangeType == WatcherChangeTypes.Changed)
{
dict[arg.FullPath] = new[] { arg};
}
else if (arg.ChangeType == WatcherChangeTypes.Deleted)
{
if (last.ChangeType == WatcherChangeTypes.Created)
dict.Remove(arg.FullPath);
else
{
dict[arg.FullPath] = new[] { arg };
}
}
else if (last.ChangeType == WatcherChangeTypes.Renamed && arg.ChangeType == WatcherChangeTypes.Changed)
{
var newValue = oldValue.Concat(new[] { arg }).ToArray();
dict[arg.FullPath] = newValue;
}
}
}
_pollAgent.Pause = false;
_callback(dict);
}
}
}