dotnet plugin: Export "dispatch_values" to the .Net code.
[collectd.git] / bindings / csharp / collectd.cs
1 /**
2  * collectd - bindings/csharp/collectd.cs
3  * Copyright (C) 2010  Florian Forster
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  *
23  * Authors:
24  *   Florian Forster <ff at octo.it>
25  **/
26
27 using System;
28 using System.Collections;
29 using System.Runtime.CompilerServices;
30
31 namespace CollectdAPI
32 {
33   public interface IRead /* {{{ */
34   {
35     int read ();
36   } /* }}} interface IRead */
37
38   public class Collectd /* {{{ */
39   {
40     [MethodImplAttribute(MethodImplOptions.InternalCall)]
41       public extern static int log (int severity, string message);
42
43     [MethodImplAttribute(MethodImplOptions.InternalCall)]
44       public extern static int registerRead (string name, IRead obj);
45
46     [MethodImplAttribute(MethodImplOptions.InternalCall)]
47       public extern static int dispatchValues (ValueList vl);
48   } /* }}} class Collectd */
49
50   public abstract class Value /* {{{ */
51   {
52     public abstract long toLong ();
53     public abstract double toDouble ();
54   } /* }}} class Value */
55
56   public class gaugeValue: Value /* {{{ */
57   {
58     private double _value;
59
60     public gaugeValue (double v)
61     {
62       this._value = v;
63     }
64
65     public override long toLong () { return ((long) this._value); }
66     public override double toDouble () { return (this._value); }
67   } /* }}} class gaugeValue */
68
69   public class deriveValue: Value /* {{{ */
70   {
71     private long _value;
72
73     public deriveValue (long v)
74     {
75       this._value = v;
76     }
77
78     public override long toLong () { return (this._value); }
79     public override double toDouble () { return ((double) this._value); }
80   } /* }}} class deriveValue */
81
82   public class Identifier /* {{{ */
83   {
84     protected string _host;
85     protected string _plugin;
86     protected string _pluginInstance;
87     protected string _type;
88     protected string _typeInstance;
89
90     public Identifier (string host,
91         string plugin, string pluginInstance,
92         string type, string typeInstance)
93     {
94       this._host = host;
95       this._plugin = plugin;
96       this._pluginInstance = pluginInstance;
97       this._type = type;
98       this._typeInstance = typeInstance;
99     } /* Identifier() */
100
101     public string getHost           () { return (this._host);           }
102     public string getPlugin         () { return (this._plugin);         }
103     public string getPluginInstance () { return (this._pluginInstance); }
104     public string getType           () { return (this._type);           }
105     public string getTypeInstance   () { return (this._typeInstance);   }
106
107     public void setHost           (string s) { this._host           = s; }
108     public void setPlugin         (string s) { this._plugin         = s; }
109     public void setPluginInstance (string s) { this._pluginInstance = s; }
110     public void setType           (string s) { this._type           = s; }
111     public void setTypeInstance   (string s) { this._typeInstance   = s; }
112   } /* }}} class Identifier */
113
114   public class ValueList: Identifier /* {{{ */
115   {
116     protected double _time;
117     protected double _interval;
118     protected IList  _values;
119     
120     public ValueList (string host,
121         string plugin, string pluginInstance,
122         string type, string typeInstance)
123       :base (host, plugin, pluginInstance, type, typeInstance)
124     {
125       this._interval = 10.0;
126       this._values = new ArrayList ();
127       this.setTime (DateTime.Now);
128     }
129
130     public IList getValues ()
131     {
132       return (this._values);
133     }
134
135     public void setValues (IList values)
136     {
137       this._values = new ArrayList (values);
138     }
139
140     public void addValue (Value v)
141     {
142       this._values.Add (v);
143     }
144
145     public void clearValues ()
146     {
147       this._values.Clear ();
148     }
149
150     public double getInterval ()
151     {
152       return (this._interval);
153     }
154
155     public void setInterval (double interval)
156     {
157       if (interval > 0.0)
158         this._interval = interval;
159     }
160
161     public double getTime ()
162     {
163       return (this._time);
164     }
165
166     public void setTime (DateTime dt)
167     {
168       DateTime dtBase = new DateTime (1970,1,1,0,0,0);
169       TimeSpan tsDiff = dt.ToUniversalTime () - dtBase;
170
171       this._time = (double) tsDiff.TotalSeconds;
172     }
173
174     public void setTime (double t)
175     {
176       if (t > 0.0)
177         this._time = t;
178     }
179   } /* }}} class ValueList */
180 }
181
182 /* vim: set sw=2 sts=2 et fdm=marker : */