1 /*
2 @file Hello.cs
3 @comment Access xmlBlaster from C# (Csharp)
4 @author mr@marcelruff.info
5
6 @prepare Linux: cd ~/xmlBlaster; build c-lib; cd ~/xmlBlaster/src/csharp; ln -s ../../lib/libxmlBlasterClientCD.so .
7 @compile Linux: mcs /unsafe /d:"XMLBLASTER_MONO" /d:CF1 -debug+ -out:Hello.exe NativeC.cs XmlBlasterAccess.cs Hello.cs Key.cs Qos.cs
8 gmcs /unsafe /d:XMLBLASTER_MONO /d:FORCE_PINVOKECE_PLUGIN -debug+ -out:Hello.exe PInvokeCE.cs XmlBlasterAccess.cs Hello.cs Key.cs Qos.cs
9
10 @prepare Windows: Compile the C client library first (see xmlBlaster\src\c\xmlBlasterClientC.sln)
11 @compile Windows: csc /unsafe -debug+ -out:Hello.exe XmlBlasterAccess.cs PInvokeCE.cs Hello.cs
12
13 @prepare WindowsCE: Compile the C client library first (see xmlBlaster\src\c\WindowsCE\xmlBlasterCE.sln)
14 or download the dll binaries xmlBlasterClientC-Arm4.dll, pthreads270-Arm4.dll, zlib123-Arm4.dll
15 @compile WindowsCE: It is best to compile from VC++ 8.0, the compile looks something like
16 C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\Csc.exe /noconfig /nostdlib+ -debug+ /unsafe /define:Smartphone /reference:..\..\lib\xmlBlasterClientCD-Arm4.dll -out:Hello.exe PInvokeCE.cs Hello.cs XmlBlasterAccess.cs Key.cs Qos.cs
17
18
19 @run mono Hello.exe
20 @run mono Hello.exe --help
21 @see http://www.xmlblaster.org/xmlBlaster/doc/requirements/client.csharp.html
22 @c http://www.xmlBlaster/org
23 */
24 using System;
25 using System.Runtime.InteropServices;
26 using org.xmlBlaster.client;
27
28 public class Hello : I_Callback, I_ConnectionStateListener
29 {
30 const string callbackSessionId = "secretCb";
31
32 static void Main(string[] argv) {
33 new Hello(argv);
34 }
35
36 public Hello(string[] argv) {
37 I_XmlBlasterAccess nc = XmlBlasterAccessFactory.CreateInstance();
38 nc.Initialize(argv);
39 nc.RegisterConnectionListener(this);
40
41 string connectQos = String.Format(
42 "<qos>\n"+
43 " <securityService type='htpasswd' version='1.0'>\n"+
44 " <![CDATA[\n"+
45 " <user>fritz</user>\n"+
46 " <passwd>secret</passwd>\n"+
47 " ]]>\n"+
48 " </securityService>\n"+
49 " <queue relating='callback' maxEntries='50000' maxEntriesCache='10000'>\n"+
50 " <callback type='SOCKET' sessionId='{0}'>\n"+
51 " </callback>\n"+
52 " </queue>\n"+
53 "</qos>", callbackSessionId); //" socket://{1}:{2}"+
54 Console.WriteLine("Connecting with:" + connectQos);
55
56 I_Callback callback = this;
57 ConnectReturnQos qos = nc.Connect(connectQos, callback);
58
59 Console.WriteLine("Connected." + qos.GetSessionName());
60
61 for (int i=0; i<5; i++) {
62 SubscribeReturnQos srq = nc.Subscribe("<key oid='Hello'/>", "<qos/>");
63 Console.WriteLine("subscribe() returned " + srq.GetSubscriptionId());
64
65 //nc.publish("<key oid='Hello'/>", "HIII", "<qos/>");
66 PublishReturnQos prq = nc.Publish("<key oid='C#C#C#'/>", "HIIIHAAAA", "<qos/>");
67 Console.WriteLine("publish() returned " + prq.GetKeyOid());
68
69 prq = nc.Publish("<key oid='C#C#C#'/>", "HIIIHOOO", "<qos/>");
70 Console.WriteLine("publish() returned " + prq.GetKeyOid());
71
72 MsgUnitGet[] msgs = nc.Get("<key oid='C#C#C#'/>", "<qos><history numEntries='6'/></qos>");
73 Console.WriteLine("get() returned " + msgs.Length + " messages");
74
75 string p = nc.Ping("<qos/>");
76 Console.WriteLine("ping() returned " + p);
77
78 bool b = nc.IsConnected();
79 Console.WriteLine("isConnected() returned " + b);
80
81 UnSubscribeReturnQos[] urq = nc.UnSubscribe("<key oid='Hello'/>", "<qos/>");
82 Console.WriteLine("unSubscribe() returned " + urq[0].GetSubscriptionId());
83
84 EraseReturnQos[] erq = nc.Erase("<key oid='C#C#C#'/>", "<qos/>");
85 Console.WriteLine("erase() returned " + erq[0].GetKeyOid());
86
87 Console.WriteLine("\nHit a key " + i);
88 Console.ReadLine();
89 }
90 bool drq = nc.Disconnect("<qos/>");
91 Console.WriteLine("disconnect() returned " + drq);
92
93 Console.WriteLine("DONE");
94 }
95
96 #region I_Callback Members
97 public string OnUpdate(string cbSessionId, MsgUnitUpdate msgUnit) {
98 Console.WriteLine("OnUpdate() invoked START ==================");
99 if (callbackSessionId != cbSessionId)
100 Console.WriteLine("Not authorized");
101 Console.WriteLine(msgUnit.GetUpdateKey().ToXml());
102 Console.WriteLine(msgUnit.GetContentStr());
103 Console.WriteLine(msgUnit.GetUpdateQos().ToXml());
104 string ret = "<qos><state id='OK'/></qos>";
105 Console.WriteLine("OnUpdate() invoked DONE ===================");
106 //throw new XmlBlasterException("user.update.illegalArgument", "A test exception from OnUpdate()");
107 return ret;
108 }
109 #endregion
110 #region I_ConnectionStateListener Members
111 public void reachedAlive(ConnectionStateEnum oldState, I_XmlBlasterAccess connection) {
112 Console.WriteLine("reachedAlive() ...");
113 }
114 public void reachedPolling(ConnectionStateEnum oldState, I_XmlBlasterAccess connection) {
115 Console.WriteLine("reachedPolling() ...");
116 }
117 public void reachedDead(ConnectionStateEnum oldState, I_XmlBlasterAccess connection) {
118 Console.WriteLine("reachedDead() ...");
119 }
120 #endregion
121 }
syntax highlighted by Code2HTML, v. 0.9.1