singrdk/base/Services/NetStack/Runtime/ProtocolParams.cs

186 lines
6.6 KiB
C#
Raw Normal View History

2008-11-17 18:29:00 -05:00
// ----------------------------------------------------------------------------
2008-03-05 09:52:00 -05:00
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
2008-11-17 18:29:00 -05:00
// ----------------------------------------------------------------------------
2008-03-05 09:52:00 -05:00
2008-11-17 18:29:00 -05:00
///
// Microsoft Research, Cambridge
//
2008-03-05 09:52:00 -05:00
using System;
using System.Collections.Specialized;
using System.Net.IP;
using Drivers.Net;
namespace NetStack.Runtime
{
/// <summary>
/// This class encapsulated the modules parameters
/// as given in the configuration file.
/// The interpretation of each parameter is module specific.
/// </summary>
public class ProtocolParams : StringDictionary
{
/// <summary>
/// Read a named integer parameter from a <c>ProtocolParams</c>
/// instance.
/// </summary>
/// <returns><paramref name="defaultValue"/> if <paramref name="parameters"/>
/// is <c>null</c> or <paramref name="parameterName"/>
/// cannot be found. Otherwise it
/// returns the named parameter as an integer.</returns>
public static int LookupInt32(ProtocolParams parameters,
string parameterName,
int defaultValue)
{
2008-11-17 18:29:00 -05:00
if (parameters == null) {
2008-03-05 09:52:00 -05:00
return defaultValue;
}
2008-11-17 18:29:00 -05:00
else if (parameterName == null) {
2008-03-05 09:52:00 -05:00
throw new ArgumentNullException();
}
string sValue = parameters[parameterName];
2008-11-17 18:29:00 -05:00
if (sValue == null) {
2008-03-05 09:52:00 -05:00
return defaultValue;
}
2008-11-17 18:29:00 -05:00
try {
2008-03-05 09:52:00 -05:00
return Int32.Parse(sValue);
}
2008-11-17 18:29:00 -05:00
catch {
2008-03-05 09:52:00 -05:00
Core.Log("Failed on parameter \"{0}\" value \"{1}\"\n",
parameterName, sValue);
return defaultValue;
}
}
/// <summary>
/// Read a named unsigned integer parameter from a
/// <c>ProtocolParams</c> instance.
/// </summary>
/// <returns><paramref name="defaultValue"/> if <paramref name="parameters"/>
/// is <c>null</c> or <paramref name="parameterName"/> cannot be found.
/// Otherwise it returns the named parameter as an unsigned
/// integer.</returns>
public static uint LookupUInt32(ProtocolParams parameters,
string parameterName,
uint defaultValue)
{
2008-11-17 18:29:00 -05:00
if (parameters == null) {
2008-03-05 09:52:00 -05:00
return defaultValue;
}
2008-11-17 18:29:00 -05:00
else if (parameterName == null) {
2008-03-05 09:52:00 -05:00
throw new ArgumentNullException();
}
string sValue = parameters[parameterName];
2008-11-17 18:29:00 -05:00
if (sValue == null) {
2008-03-05 09:52:00 -05:00
return defaultValue;
}
2008-11-17 18:29:00 -05:00
try {
2008-03-05 09:52:00 -05:00
return UInt32.Parse(sValue);
}
2008-11-17 18:29:00 -05:00
catch {
2008-03-05 09:52:00 -05:00
Core.Log("Failed on parameter \"{0}\" value \"{1}\"\n",
parameterName, sValue);
return defaultValue;
}
}
/// <summary>
/// Read a named IPv4 parameter from a <c>ProtocolParams</c>
/// instance.
/// </summary>
/// <returns><paramref name="defaultValue"/> if <paramref name="parameters"/>
/// is <c>null</c> or <paramref name="parameterName"/> cannot be found.
/// Otherwise it returns the named parameter as an IPv4 address.</returns>
public static IPv4 LookupIPv4(ProtocolParams parameters,
string parameterName,
IPv4 defaultValue)
{
2008-11-17 18:29:00 -05:00
if (parameters == null) {
2008-03-05 09:52:00 -05:00
return defaultValue;
}
2008-11-17 18:29:00 -05:00
else if (parameterName == null) {
2008-03-05 09:52:00 -05:00
throw new ArgumentNullException();
}
string sValue = parameters[parameterName];
2008-11-17 18:29:00 -05:00
if (sValue == null) {
2008-03-05 09:52:00 -05:00
return defaultValue;
}
2008-11-17 18:29:00 -05:00
try {
2008-03-05 09:52:00 -05:00
return IPv4.Parse(sValue);
}
2008-11-17 18:29:00 -05:00
catch (FormatException) {
2008-03-05 09:52:00 -05:00
Core.Log("Failed on parameter \"{0}\" value \"{1}\"\n",
parameterName, sValue);
}
return defaultValue;
}
/// <summary>
/// Read a named string parameter from a <c>ProtocolParams</c>
/// instance.
/// </summary>
/// <returns><paramref name="defaultValue"/> if <paramref name="parameters"/>
/// is <c>null</c> or <paramref name="parameterName"/> cannot be found.
/// Otherwise it returns the named parameter as a string.</returns>
public static string LookupString(ProtocolParams parameters,
string parameterName,
string defaultValue)
{
2008-11-17 18:29:00 -05:00
if (parameters == null) {
2008-03-05 09:52:00 -05:00
return defaultValue;
}
2008-11-17 18:29:00 -05:00
else if (parameterName == null) {
2008-03-05 09:52:00 -05:00
throw new ArgumentNullException();
}
string sValue = parameters[parameterName];
2008-11-17 18:29:00 -05:00
if (sValue == null) {
2008-03-05 09:52:00 -05:00
return defaultValue;
}
return sValue;
}
/// <summary>
/// Read a named boolean parameter from a <c>ProtocolParams</c>
/// instance.
/// </summary>
/// <returns><paramref name="defaultValue"/> if <paramref name="parameters"/>
/// is <c>null</c> or <paramref name="parameterName"/> cannot be found.
/// Otherwise it returns the named parameter as a boolean.</returns>
public static bool LookupBoolean(ProtocolParams parameters,
string parameterName,
bool defaultValue)
{
2008-11-17 18:29:00 -05:00
if (parameters == null) {
2008-03-05 09:52:00 -05:00
return defaultValue;
}
2008-11-17 18:29:00 -05:00
else if (parameterName == null) {
2008-03-05 09:52:00 -05:00
throw new ArgumentNullException();
}
string sValue = parameters[parameterName];
2008-11-17 18:29:00 -05:00
if (sValue == null) {
2008-03-05 09:52:00 -05:00
return defaultValue;
}
2008-11-17 18:29:00 -05:00
try {
2008-03-05 09:52:00 -05:00
return Boolean.Parse(sValue);
}
2008-11-17 18:29:00 -05:00
catch (FormatException) {
2008-03-05 09:52:00 -05:00
Core.Log("Failed on parameter \"{0}\" value \"{1}\"\n",
parameterName, sValue);
return defaultValue;
}
}
}
}