1 /*
2 @file TestPInvoke.cs
3 @comment Access xmlBlaster from C# (Csharp)
4 @author mr@marcelruff.info
5 @compile csc /unsafe -debug+ -out:TestPInvoke.exe PInvokeCE.cs XmlBlasterAccess.cs Key.cs Qos.cs TestPInvoke.cs
6 gmcs /unsafe /define:"XMLBLASTER_MONO;FORCE_PINVOKECE_PLUGIN" -debug+ -out:TestPInvoke.exe PInvokeCE.cs TestPInvoke.cs XmlBlasterAccess.cs Key.cs Qos.cs
7 @see http://www.xmlblaster.org/xmlBlaster/doc/requirements/client.csharp.html
8 */
9 using System;
10 using System.Runtime.InteropServices;
11 using System.Threading;
12 using System.Collections;
13 using org.xmlBlaster.client;
14
15 public class TestPInvoke : I_Callback, I_LoggingCallback, I_ProgressCallback
16 {
17 private I_XmlBlasterAccess xb;
18 private const string callbackSessionId = "secretCb";
19 private String[] argv;
20
21 static void Main(string[] argv)
22 {
23 Console.WriteLine("[TestPInvoke.cs] Startup");
24 new TestPInvoke(argv);
25 }
26
27 public TestPInvoke(string[] argv)
28 {
29 this.argv = argv;
30 runAllMethods();
31 //runIdTest();
32 }
33
34 private void runIdTest() {
35 Console.WriteLine("Hello world");
36 xb = XmlBlasterAccessFactory.CreateInstance();
37 xb.AddLoggingListener(this);
38 xb.Initialize(argv);
39 log("Accessing not IDs");
40 string deviceId = xb.GetDeviceUniqueId();
41 log("deviceId=" + deviceId);
42 //MessageBox.Show("DeviceUniqueId="+deviceId, "Name Entry Error",
43 // MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
44 string emeiId = xb.GetEmeiId();
45 log("EMEI=" + emeiId);
46 //MessageBox.Show("EMEI="+emeiId, "Name Entry Error",
47 // MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
48 }
49
50 private void runAllMethods() {
51 xb = XmlBlasterAccessFactory.CreateInstance();
52 xb.AddLoggingListener(this);
53 xb.Initialize(argv);
54 xb.Initialize(new Hashtable());
55
56 string connectQos = String.Format(
57 "<qos>\n" +
58 " <securityService type='htpasswd' version='1.0'>\n" +
59 " <![CDATA[\n" +
60 " <user>fritz</user>\n" +
61 " <passwd>secret</passwd>\n" +
62 " ]]>\n" +
63 " </securityService>\n" +
64 " <queue relating='callback' maxEntries='50000' maxEntriesCache='10000'>\n" +
65 " <callback type='SOCKET' sessionId='{0}'>\n" +
66 " </callback>\n" +
67 " </queue>\n" +
68 "</qos>", callbackSessionId); //" socket://{1}:{2}"+
69 log("Connecting with:" + connectQos);
70
71 I_Callback callback = this;
72 xb.Connect(connectQos, callback);
73 xb.AddCallbackProgressListener(this);
74
75 for (int run=0; run<2; run++) {
76
77 PublishReturnQos prq = xb.Publish("<key oid='Hello'/>", "publish-1", "<qos/>");
78 log("publish() returned " + prq.GetKeyOid());
79
80 SubscribeReturnQos srq = xb.Subscribe("<key oid='Hello'/>", "<qos><updateOneway/></qos>");
81 log("subscribe() returned " + srq.GetSubscriptionId());
82 GC.Collect();
83 GC.Collect();
84
85 prq = xb.Publish("<key oid='Hello'/>", "publish-2", "<qos/>");
86 log("publish() returned " + prq.GetKeyOid());
87
88 Thread.Sleep(1000);
89 Console.WriteLine("There should be some updates, hit a key to continue ...");
90 Console.ReadLine();
91
92 srq = xb.Subscribe("<key oid='TestPInvoke'/>", "<qos/>");
93 log("subscribe() returned " + srq.GetSubscriptionId());
94
95 srq = xb.Subscribe("<key oid='TestPInvoke'/>", "<qos/>");
96 log("subscribe() returned " + srq.GetSubscriptionId());
97
98 UnSubscribeReturnQos[] urq = xb.UnSubscribe("<key oid='TestPInvoke'/>", "<qos/>");
99 log("unSubscribe() returned");
100 for (int i = 0; i < urq.Length; i++)
101 log("unSubscribeReturn #" + i + ": " + urq[i].GetSubscriptionId());
102 GC.Collect();
103 GC.Collect();
104
105 prq = xb.Publish("<key oid='C#C#C#'/>", "more publishes", "<qos/>");
106 log("publish() returned " + prq.GetKeyOid());
107
108 MsgUnit[] arr = new MsgUnit[6];
109 for (int i=0; i<arr.Length; i++)
110 arr[i] = new MsgUnit("<key oid='C#C#'/>", "oneway-"+i, "<qos/>");
111 xb.PublishOneway(arr);
112 log("publishOneway() send " + arr.Length + " messages");
113
114 prq = xb.Publish("<key oid='C#'/>", "HIIIHAAAA", "<qos/>");
115 log("publish() returned " + prq.GetRcvTimeNanos());
116
117 MsgUnit[] msgs = xb.Get("<key oid='C#C#'/>", "<qos><history numEntries='4'/></qos>");
118 log("get(C#C#) returned " + msgs.Length + " messages (get was limited to 4)");
119 for (int i = 0; i < msgs.Length; i++)
120 log(msgs[i].ToString());
121
122 msgs = xb.Get("<key oid='unknown'/>", "<qos><history numEntries='6'/></qos>");
123 log("get(unknown) returned " + msgs.Length + " messages");
124
125 EraseReturnQos[] erq = xb.Erase("<key queryType='XPATH'>//key</key>", "<qos/>");
126 log("erase() returned");
127 for (int i = 0; i < erq.Length; i++)
128 log("eraseReturn #" + i + ": " + erq[i].GetKeyOid());
129
130 string p = xb.Ping("<qos/>");
131 StatusQos pp = new StatusQos(p);
132 log("ping() returned " + pp.GetState());
133
134 bool b = xb.IsConnected();
135 log("isConnected() returned " + b);
136 }
137
138 bool drq = xb.Disconnect("<qos/>");
139 log("disconnect() returned " + drq);
140
141 log("DONE");
142 }
143
144 #region I_Callback Members
145 public string OnUpdate(string cbSessionId, MsgUnitUpdate msgUnit)
146 {
147 log("OnUpdate() received "+(msgUnit.IsOneway()?"oneway ":"")+"message from xmlBlaster:");
148 if (callbackSessionId != cbSessionId)
149 log("Not authorized");
150 log(msgUnit.ToString());
151 return "<qos><state id='OK'/></qos>";
152 //throw new XmlBlasterException("user.update.illegalArgument", "A test exception from OnUpdate()");
153 }
154 #endregion
155
156 #region I_LoggingCallback Members
157 public void OnLogging(XmlBlasterLogLevel logLevel, string location, string message)
158 {
159 log(logLevel.ToString() + " " + location + ": " + message);
160 }
161 #endregion
162
163 #region I_ProgressCallback Members
164 public void OnData(bool read, int currBytesRead, int nbytes)
165 {
166 Console.WriteLine("Read " + currBytesRead + "/" + nbytes + " bytes");
167 }
168 #endregion
169
170 void log(String str)
171 {
172 Console.WriteLine(str);
173 System.Diagnostics.Debug.WriteLine(str);
174 }
175 }
syntax highlighted by Code2HTML, v. 0.9.1