1 // Copyright 2006 Alp Toker <alp@atoker.com>
2 // This software is made available under the MIT License
3 // See COPYING for details
4 
5 using System;
6 using System.Collections.Generic;
7 using DBus;
8 using org.freedesktop.DBus;
9 
10 //NOTE: MarshalByRefObject use is not recommended in new code
11 //See TestExportInterface.cs for a more current example
12 public class ManagedDBusTestExport
13 {
Main()14 	public static void Main ()
15 	{
16 		Bus bus = Bus.Session;
17 
18 		string bus_name = "org.ndesk.test";
19 		ObjectPath path = new ObjectPath ("/org/ndesk/test");
20 
21 		DemoObject demo;
22 
23 		if (bus.RequestName (bus_name) == RequestNameReply.PrimaryOwner) {
24 			//create a new instance of the object to be exported
25 			demo = new DemoObject ();
26 			bus.Register (path, demo);
27 
28 			//run the main loop
29 			while (true)
30 				bus.Iterate ();
31 		} else {
32 			//import a remote to a local proxy
33 			demo = bus.GetObject<DemoObject> (bus_name, path);
34 		}
35 
36 		demo.Say ("Hello world!");
37 		demo.Say ("Sibérie");
38 		demo.Say (21);
39 		demo.SayByteArray (new byte[] {0, 2, 1}, "test string");
40 		demo.SayByteEnumArray (new BEnum[] {BEnum.Zero, BEnum.Two, BEnum.One}, "test string two");
41 		Console.WriteLine (demo.EchoCaps ("foo bar"));
42 		Console.WriteLine (demo.GetEnum ());
43 		demo.CheckEnum (DemoEnum.Bar);
44 		demo.CheckEnum (demo.GetEnum ());
45 
46 		Console.WriteLine ();
47 		long someLong = demo.GetSomeLong ();
48 		Console.WriteLine ("someLong: " + someLong);
49 
50 		Console.WriteLine ();
51 		ulong someULong = demo.GetSomeULong ();
52 		Console.WriteLine ("someULong: " + someULong);
53 
54 		/*
55 		Console.WriteLine ();
56 		string outVal;
57 		demo.ReturnOut (out outVal);
58 		Console.WriteLine ("outVal: " + outVal);
59 		*/
60 
61 		Console.WriteLine ();
62 		string[] texts = {"one", "two", "three"};
63 		texts = demo.EchoCapsArr (texts);
64 		foreach (string text in texts)
65 			Console.WriteLine (text);
66 
67 		Console.WriteLine ();
68 		string[][] arrarr = demo.ArrArr ();
69 		Console.WriteLine (arrarr[1][0]);
70 
71 		Console.WriteLine ();
72 		int[] vals = demo.TextToInts ("1 2 3");
73 		foreach (int val in vals)
74 			Console.WriteLine (val);
75 
76 		Console.WriteLine ();
77 		MyTuple fooTuple = demo.GetTuple ();
78 		Console.WriteLine ("A: " + fooTuple.A);
79 		Console.WriteLine ("B: " + fooTuple.B);
80 
81 		Console.WriteLine ();
82 		//KeyValuePair<string,string>[] kvps = demo.GetDict ();
83 		IDictionary<string,string> dict = demo.GetDict ();
84 		foreach (KeyValuePair<string,string> kvp in dict)
85 			Console.WriteLine (kvp.Key + ": " + kvp.Value);
86 
87 		Console.WriteLine ();
88 		demo.SomeEvent += delegate (string arg1, object arg2, double arg3, MyTuple mt) {Console.WriteLine ("SomeEvent handler: " + arg1 + ", " + arg2 + ", " + arg3 + ", " + mt.A + ", " + mt.B);};
89 		demo.SomeEvent += delegate (string arg1, object arg2, double arg3, MyTuple mt) {Console.WriteLine ("SomeEvent handler two: " + arg1 + ", " + arg2 + ", " + arg3 + ", " + mt.A + ", " + mt.B);};
90 		demo.FireOffSomeEvent ();
91 		//handle the raised signal
92 		//bus.Iterate ();
93 
94 		Console.WriteLine ();
95 		demo.SomeEvent += HandleSomeEventA;
96 		demo.FireOffSomeEvent ();
97 		//handle the raised signal
98 		//bus.Iterate ();
99 
100 		Console.WriteLine ();
101 		demo.SomeEvent -= HandleSomeEventA;
102 		demo.FireOffSomeEvent ();
103 		//handle the raised signal
104 		//bus.Iterate ();
105 
106 		Console.WriteLine ();
107 		{
108 			object tmp = demo.GetArrayOfInts ();
109 			int[] arr = (int[])tmp;
110 			Console.WriteLine ("Array of ints as variant: " + arr[0] + " " + arr[1]);
111 		}
112 
113 		Console.WriteLine ();
114 		{
115 			demo.UseSomeVariant ("hello");
116 			demo.UseSomeVariant (21);
117 		}
118 
119 		Console.WriteLine ();
120 		{
121 			Console.WriteLine ("get SomeProp: " + demo.SomeProp);
122 			demo.SomeProp = 4;
123 		}
124 	}
125 
HandleSomeEventA(string arg1, object arg2, double arg3, MyTuple mt)126 	public static void HandleSomeEventA (string arg1, object arg2, double arg3, MyTuple mt)
127 	{
128 		Console.WriteLine ("SomeEvent handler A: " + arg1 + ", " + arg2 + ", " + arg3 + ", " + mt.A + ", " + mt.B);
129 	}
130 
HandleSomeEventB(string arg1, object arg2, double arg3, MyTuple mt)131 	public static void HandleSomeEventB (string arg1, object arg2, double arg3, MyTuple mt)
132 	{
133 		Console.WriteLine ("SomeEvent handler B: " + arg1 + ", " + arg2 + ", " + arg3 + ", " + mt.A + ", " + mt.B);
134 	}
135 }
136 
137 [Interface ("org.ndesk.test")]
138 public class DemoObject : MarshalByRefObject
139 {
Say(string text)140 	public void Say (string text)
141 	{
142 		Console.WriteLine ("string: " + text);
143 	}
144 
Say(object var)145 	public void Say (object var)
146 	{
147 		Console.WriteLine ("variant: " + var);
148 	}
149 
SayByteArray(byte[] data, string str)150 	public void SayByteArray (byte[] data, string str)
151 	{
152 		for (int i = 0 ; i != data.Length ; i++)
153 			Console.WriteLine ("data[" + i + "]: " + data[i]);
154 
155 		Console.WriteLine (str);
156 	}
157 
SayByteEnumArray(BEnum[] data, string str)158 	public void SayByteEnumArray (BEnum[] data, string str)
159 	{
160 		for (int i = 0 ; i != data.Length ; i++)
161 			Console.WriteLine ("data[" + i + "]: " + data[i]);
162 
163 		Console.WriteLine (str);
164 	}
165 
EchoCaps(string text)166 	public string EchoCaps (string text)
167 	{
168 		return text.ToUpper ();
169 	}
170 
GetSomeLong()171 	public long GetSomeLong ()
172 	{
173 		return Int64.MaxValue;
174 	}
175 
GetSomeULong()176 	public ulong GetSomeULong ()
177 	{
178 		return UInt64.MaxValue;
179 	}
180 
CheckEnum(DemoEnum e)181 	public void CheckEnum (DemoEnum e)
182 	{
183 		Console.WriteLine (e);
184 	}
185 
GetEnum()186 	public DemoEnum GetEnum ()
187 	{
188 		return DemoEnum.Bar;
189 	}
190 
191 	//this doesn't work yet, except for introspection
192 	public DemoEnum EnumState
193 	{
194 		get {
195 			return DemoEnum.Bar;
196 		} set {
197 			Console.WriteLine ("EnumState prop set to " + value);
198 		}
199 	}
200 
201 	/*
202 	public void ReturnOut (out string val)
203 	{
204 		val = "out value";
205 	}
206 	*/
207 
EchoCapsArr(string[] texts)208 	public string[] EchoCapsArr (string[] texts)
209 	{
210 		string[] retTexts = new string[texts.Length];
211 
212 		for (int i = 0 ; i != texts.Length ; i++)
213 			retTexts[i] = texts[i].ToUpper ();
214 
215 		return retTexts;
216 	}
217 
ArrArr()218 	public string[][] ArrArr ()
219 	{
220 		string[][] ret = new string[2][];
221 
222 		ret[0] = new string[] {"one", "two"};
223 		ret[1] = new string[] {"three", "four"};
224 
225 		return ret;
226 	}
227 
TextToInts(string text)228 	public int[] TextToInts (string text)
229 	{
230 		string[] parts = text.Split (' ');
231 		int[] rets = new int[parts.Length];
232 
233 		for (int i = 0 ; i != parts.Length ; i++)
234 			rets[i] = Int32.Parse (parts[i]);
235 
236 		return rets;
237 	}
238 
GetTuple()239 	public MyTuple GetTuple ()
240 	{
241 		MyTuple tup;
242 
243 		tup.A = "alpha";
244 		tup.B = "beta";
245 
246 		return tup;
247 	}
248 
GetDict()249 	public IDictionary<string,string> GetDict ()
250 	{
251 		Dictionary<string,string> dict = new Dictionary<string,string> ();
252 
253 		dict["one"] = "1";
254 		dict["two"] = "2";
255 
256 		return dict;
257 	}
258 
259 	/*
260 	public KeyValuePair<string,string>[] GetDict ()
261 	{
262 		KeyValuePair<string,string>[] rets = new KeyValuePair<string,string>[2];
263 
264 		//rets[0] = new KeyValuePair<string,string> ("one", "1");
265 		//rets[1] = new KeyValuePair<string,string> ("two", "2");
266 
267 		rets[0] = new KeyValuePair<string,string> ("second", " from example-service.py");
268 		rets[1] = new KeyValuePair<string,string> ("first", "Hello Dict");
269 
270 		return rets;
271 	}
272 	*/
273 
274 	public event SomeEventHandler SomeEvent;
275 
FireOffSomeEvent()276 	public void FireOffSomeEvent ()
277 	{
278 		Console.WriteLine ("Asked to fire off SomeEvent");
279 
280 		MyTuple mt;
281 		mt.A = "a";
282 		mt.B = "b";
283 
284 		if (SomeEvent != null) {
285 			SomeEvent ("some string", 21, 19.84, mt);
286 			Console.WriteLine ("Fired off SomeEvent");
287 		}
288 	}
289 
GetArrayOfInts()290 	public object GetArrayOfInts ()
291 	{
292 		int[] arr = new int[2];
293 		arr[0] = 21;
294 		arr[1] = 22;
295 
296 		return arr;
297 	}
298 
UseSomeVariant(object value)299 	public void UseSomeVariant (object value)
300 	{
301 		Console.WriteLine ("variant value: " + value);
302 	}
303 
304 	public int SomeProp
305 	{
306 		get {
307 			return 1;
308 		}
309 		set {
310 			Console.WriteLine ("set prop SomeProp: " + value);
311 		}
312 	}
313 }
314 
315 public enum DemoEnum
316 {
317 	Foo,
318 	Bar,
319 }
320 
321 public enum BEnum : byte
322 {
323 	Zero = 0,
324 	One = 1,
325 	Two = 2,
326 }
327 
328 
329 public struct MyTuple
330 {
331 	public string A;
332 	public string B;
333 }
334 
SomeEventHandler(string arg1, object arg2, double arg3, MyTuple mt)335 public delegate void SomeEventHandler (string arg1, object arg2, double arg3, MyTuple mt);
336