PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/SharpSnmpLib/Messaging/Manager.cs

#
C# | 296 lines | 119 code | 25 blank | 152 comment | 0 complexity | 5754dbb97bdfe5531fbd4ea97bc6a057 MD5 | raw file
Possible License(s): MIT, LGPL-2.1
  1. // Manager class.
  2. // Copyright (C) 2008-2010 Malcolm Crowe, Lex Li, and other contributors.
  3. //
  4. // This library is free software; you can redistribute it and/or
  5. // modify it under the terms of the GNU Lesser General Public
  6. // License as published by the Free Software Foundation; either
  7. // version 2.1 of the License, or (at your option) any later version.
  8. //
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. // Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public
  15. // License along with this library; if not, write to the Free Software
  16. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. using System;
  18. using System.Collections.Generic;
  19. using System.Globalization;
  20. using System.Net;
  21. using System.Threading;
  22. #pragma warning disable 612,618
  23. namespace Lextm.SharpSnmpLib.Messaging
  24. {
  25. /// <summary>
  26. /// SNMP manager component that provides SNMP operations.
  27. /// </summary>
  28. /// <remarks>
  29. /// <para>Drag this component into your form in designer, or create an instance in code.</para>
  30. /// <para>Currently only SNMP v1 and v2c operations are supported.</para>
  31. /// </remarks>
  32. [CLSCompliant(false)]
  33. [Obsolete("Please use Messenger class instead.")]
  34. public sealed class Manager
  35. {
  36. private const int DefaultPort = 161;
  37. private readonly object _locker = new object();
  38. private int _timeout = 5000;
  39. private VersionCode _version;
  40. /// <summary>
  41. /// Initializes a new instance of the <see cref="Manager"/> class.
  42. /// </summary>
  43. public Manager()
  44. {
  45. MaxRepetitions = 10;
  46. }
  47. /// <summary>
  48. /// Default protocol version for operations.
  49. /// </summary>
  50. /// <remarks>By default, the value is SNMP v1.</remarks>
  51. public VersionCode DefaultVersion
  52. {
  53. get
  54. {
  55. return _version;
  56. }
  57. set
  58. {
  59. lock (_locker)
  60. {
  61. _version = value;
  62. }
  63. }
  64. }
  65. /// <summary>
  66. /// Timeout value.
  67. /// </summary>
  68. /// <remarks>By default, the value is 5,000-milliseconds (5 seconds).</remarks>
  69. public int Timeout
  70. {
  71. get
  72. {
  73. return _timeout;
  74. }
  75. set
  76. {
  77. Interlocked.Exchange(ref _timeout, value);
  78. }
  79. }
  80. /// <summary>
  81. /// Gets or sets the objects.
  82. /// </summary>
  83. /// <value>The objects.</value>
  84. /// <remarks>Changed from 2.0: it will return null if not set.</remarks>
  85. public IObjectRegistry Objects { get; set; }
  86. /// <summary>
  87. /// Gets a variable bind.
  88. /// </summary>
  89. /// <param name="endpoint">Endpoint.</param>
  90. /// <param name="community">Community name.</param>
  91. /// <param name="variable">Variable bind.</param>
  92. /// <returns></returns>
  93. public Variable GetSingle(IPEndPoint endpoint, string community, Variable variable)
  94. {
  95. var variables = new List<Variable> { variable };
  96. return Messenger.Get(_version, endpoint, new OctetString(community), variables, _timeout)[0];
  97. }
  98. /// <summary>
  99. /// Gets a variable bind.
  100. /// </summary>
  101. /// <param name="address">Address.</param>
  102. /// <param name="community">Community name.</param>
  103. /// <param name="variable">Variable bind.</param>
  104. /// <returns></returns>
  105. public Variable GetSingle(string address, string community, Variable variable)
  106. {
  107. return GetSingle(IPAddress.Parse(address), community, variable);
  108. }
  109. /// <summary>
  110. /// Gets a variable bind.
  111. /// </summary>
  112. /// <param name="address">Address.</param>
  113. /// <param name="community">Community name.</param>
  114. /// <param name="variable">Variable bind.</param>
  115. /// <returns></returns>
  116. public Variable GetSingle(IPAddress address, string community, Variable variable)
  117. {
  118. return GetSingle(new IPEndPoint(address, DefaultPort), community, variable);
  119. }
  120. /// <summary>
  121. /// Gets a list of variable binds.
  122. /// </summary>
  123. /// <param name="endpoint">Endpoint.</param>
  124. /// <param name="community">Community name.</param>
  125. /// <param name="variables">Variable binds.</param>
  126. /// <returns></returns>
  127. public IList<Variable> Get(IPEndPoint endpoint, string community, IList<Variable> variables)
  128. {
  129. return Messenger.Get(_version, endpoint, new OctetString(community), variables, _timeout);
  130. }
  131. /// <summary>
  132. /// Gets a list of variable binds.
  133. /// </summary>
  134. /// <param name="address">Address.</param>
  135. /// <param name="community">Community name.</param>
  136. /// <param name="variables">Variable binds.</param>
  137. /// <returns></returns>
  138. public IList<Variable> Get(string address, string community, IList<Variable> variables)
  139. {
  140. return Get(IPAddress.Parse(address), community, variables);
  141. }
  142. /// <summary>
  143. /// Gets a list of variable binds.
  144. /// </summary>
  145. /// <param name="address">Address.</param>
  146. /// <param name="community">Community name.</param>
  147. /// <param name="variables">Variable binds.</param>
  148. /// <returns></returns>
  149. public IList<Variable> Get(IPAddress address, string community, IList<Variable> variables)
  150. {
  151. return Get(new IPEndPoint(address, DefaultPort), community, variables);
  152. }
  153. /// <summary>
  154. /// Sets a variable bind.
  155. /// </summary>
  156. /// <param name="endpoint">Endpoint.</param>
  157. /// <param name="community">Community name.</param>
  158. /// <param name="variable">Variable bind.</param>
  159. /// <returns></returns>
  160. public Variable SetSingle(IPEndPoint endpoint, string community, Variable variable)
  161. {
  162. var variables = new List<Variable> { variable };
  163. return Messenger.Set(_version, endpoint, new OctetString(community), variables, _timeout)[0];
  164. }
  165. /// <summary>
  166. /// Sets a variable bind.
  167. /// </summary>
  168. /// <param name="address">Address.</param>
  169. /// <param name="community">Community name.</param>
  170. /// <param name="variable">Variable bind.</param>
  171. /// <returns></returns>
  172. public Variable SetSingle(IPAddress address, string community, Variable variable)
  173. {
  174. return SetSingle(new IPEndPoint(address, DefaultPort), community, variable);
  175. }
  176. /// <summary>
  177. /// Sets a variable bind.
  178. /// </summary>
  179. /// <param name="address">Address.</param>
  180. /// <param name="community">Community name.</param>
  181. /// <param name="variable">Variable bind.</param>
  182. /// <returns></returns>
  183. public Variable SetSingle(string address, string community, Variable variable)
  184. {
  185. return SetSingle(IPAddress.Parse(address), community, variable);
  186. }
  187. /// <summary>
  188. /// Sets a list of variable binds.
  189. /// </summary>
  190. /// <param name="endpoint">Endpoint.</param>
  191. /// <param name="community">Community name.</param>
  192. /// <param name="variables">Variable binds.</param>
  193. /// <returns></returns>
  194. public IList<Variable> Set(IPEndPoint endpoint, string community, IList<Variable> variables)
  195. {
  196. return Messenger.Set(_version, endpoint, new OctetString(community), variables, _timeout);
  197. }
  198. /// <summary>
  199. /// Sets a list of variable binds.
  200. /// </summary>
  201. /// <param name="address">Address.</param>
  202. /// <param name="community">Community name.</param>
  203. /// <param name="variables">Variable binds.</param>
  204. /// <returns></returns>
  205. public IList<Variable> Set(string address, string community, IList<Variable> variables)
  206. {
  207. return Set(IPAddress.Parse(address), community, variables);
  208. }
  209. /// <summary>
  210. /// Sets a list of variable binds.
  211. /// </summary>
  212. /// <param name="address">Address.</param>
  213. /// <param name="community">Community name.</param>
  214. /// <param name="variables">Variable binds.</param>
  215. /// <returns></returns>
  216. public IList<Variable> Set(IPAddress address, string community, IList<Variable> variables)
  217. {
  218. return Set(new IPEndPoint(address, DefaultPort), community, variables);
  219. }
  220. /// <summary>
  221. /// Gets a table of variables.
  222. /// </summary>
  223. /// <param name="endpoint">Endpoint.</param>
  224. /// <param name="community">Community name.</param>
  225. /// <param name="table">Table OID.</param>
  226. /// <returns></returns>
  227. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1814:PreferJaggedArraysOverMultidimensional", MessageId = "Return", Justification = "ByDesign")]
  228. public Variable[,] GetTable(IPEndPoint endpoint, string community, ObjectIdentifier table)
  229. {
  230. return Messenger.GetTable(DefaultVersion, endpoint, new OctetString(community), table, Timeout, MaxRepetitions, Objects);
  231. }
  232. /// <summary>
  233. /// Gets or sets the max repetitions for GET BULK operations.
  234. /// </summary>
  235. /// <value>The max repetitions.</value>
  236. public int MaxRepetitions { get; set; }
  237. /// <summary>
  238. /// Gets a table of variables.
  239. /// </summary>
  240. /// <param name="address">Address.</param>
  241. /// <param name="community">Community name.</param>
  242. /// <param name="table">Table OID.</param>
  243. /// <returns></returns>
  244. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1814:PreferJaggedArraysOverMultidimensional", MessageId = "Return", Justification = "ByDesign")]
  245. public Variable[,] GetTable(IPAddress address, string community, ObjectIdentifier table)
  246. {
  247. return GetTable(new IPEndPoint(address, DefaultPort), community, table);
  248. }
  249. /// <summary>
  250. /// Gets a table of variables.
  251. /// </summary>
  252. /// <param name="address">Address.</param>
  253. /// <param name="community">Community name.</param>
  254. /// <param name="table">Table OID.</param>
  255. /// <returns></returns>
  256. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1814:PreferJaggedArraysOverMultidimensional", MessageId = "Return", Justification = "ByDesign")]
  257. public Variable[,] GetTable(string address, string community, ObjectIdentifier table)
  258. {
  259. return GetTable(IPAddress.Parse(address), community, table);
  260. }
  261. /// <summary>
  262. /// Returns a <see cref="String"/> that represents this <see cref="Manager"/>.
  263. /// </summary>
  264. /// <returns></returns>
  265. public override string ToString()
  266. {
  267. return string.Format(CultureInfo.InvariantCulture, "SNMP manager: timeout: {0}; version: {1}", Timeout.ToString(CultureInfo.InvariantCulture), DefaultVersion);
  268. }
  269. }
  270. }
  271. #pragma warning restore 612,618