public Reply RouteRequest(Request request, List<string> remainingPath)
{
if (remainingPath.Count == 0)
{
if (IsDefaultHandler)
{
//Execute handler
return ProcessRequest(request);
}
else
return null;
}
if (IsDefaultHandler)
{
//Default handlers can only match empty remaining paths
return null;
}
else
{
string currentName = remainingPath[0];
remainingPath = remainingPath.GetRange(1, remainingPath.Count - 1);
if (currentName == Name)
{
//We have a match for the request
if (IsContainer)
{
//We have a hit for the container but the container does not handle the request itself
//The remaining path needs to be passed on to the children so don't do anything at this point and just continue with the children
}
else
{
//The request must be handled by this object
List<string> argumentStrings = remainingPath;
if (argumentStrings.Count != ArgumentTypes.Length)
throw new HandlerException("Invalid argument count");
List<object> arguments = new List<object>();
for (int i = 0; i < ArgumentTypes.Length; i++)
{
ArgumentType type = ArgumentTypes[i];
string argumentString = argumentStrings[i];
object argument;
switch (type)
{
case ArgumentType.String:
argument = argumentString;
break;
case ArgumentType.Integer:
try
{
argument = Convert.ToInt32(argumentString);
}
catch (FormatException)
{
throw new HandlerException("Invalid integer specified");
}
break;
default:
throw new Exception("Unknown argument type encountered");
}
arguments.Add(argument);
}
request.Arguments = arguments;
//Execute handler
return ProcessRequest(request);
}
}
else
{
//The handler does not match the request
return null;
}
}
//Check children
foreach (var child in Children)
{
Reply reply = child.RouteRequest(request, remainingPath);
if (reply != null)
{
//A child of the handler found a match for the request
return reply;
}
}
//None of the children matched
return null;
}