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 org.xmlBlaster.client;
 13 
 14 public class TestPInvoke : I_Callback, I_LoggingCallback, I_ProgressCallback
 15 {
 16    private I_XmlBlasterAccess xb;
 17    private const string callbackSessionId = "secretCb";
 18    private String[] argv;
 19 
 20    static void Main(string[] argv)
 21    {
 22       Console.WriteLine("[TestPInvoke.cs] Startup");
 23       new TestPInvoke(argv);
 24    }
 25 
 26    public TestPInvoke(string[] argv)
 27    {
 28       this.argv = argv;
 29       runAllMethods();
 30       //runIdTest();

 31    }
 32 
 33    private void runIdTest() {
 34       Console.WriteLine("Hello world");
 35       xb = XmlBlasterAccessFactory.CreateInstance();
 36       xb.AddLoggingListener(this);
 37       xb.Initialize(argv);
 38       log("Accessing not IDs");
 39       string deviceId = xb.GetDeviceUniqueId();
 40       log("deviceId=" + deviceId);
 41       //MessageBox.Show("DeviceUniqueId="+deviceId, "Name Entry Error",

 42       //   MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);

 43       string emeiId = xb.GetEmeiId();
 44       log("EMEI=" + emeiId);
 45       //MessageBox.Show("EMEI="+emeiId, "Name Entry Error",

 46       //   MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);

 47    }
 48 
 49    private void runAllMethods() {
 50       xb = XmlBlasterAccessFactory.CreateInstance();
 51       xb.AddLoggingListener(this);
 52       xb.Initialize(argv);
 53 
 54       string connectQos = String.Format(
 55          "<qos>\n" +
 56          " <securityService type='htpasswd' version='1.0'>\n" +
 57          "  <![CDATA[\n" +
 58          "   <user>fritz</user>\n" +
 59          "   <passwd>secret</passwd>\n" +
 60          "  ]]>\n" +
 61          " </securityService>\n" +
 62          " <queue relating='callback' maxEntries='50000' maxEntriesCache='10000'>\n" +
 63          "   <callback type='SOCKET' sessionId='{0}'>\n" +
 64          "   </callback>\n" +
 65          " </queue>\n" +
 66          "</qos>", callbackSessionId);  //"    socket://{1}:{2}"+

 67       log("Connecting with:" + connectQos);
 68 
 69       I_Callback callback = this;
 70       xb.Connect(connectQos, callback);
 71       xb.AddCallbackProgressListener(this);
 72 
 73       for (int run=0; run<2; run++) {
 74 
 75          PublishReturnQos prq = xb.Publish("<key oid='Hello'/>", "publish-1", "<qos/>");
 76          log("publish() returned " + prq.GetKeyOid());
 77 
 78          SubscribeReturnQos srq = xb.Subscribe("<key oid='Hello'/>", "<qos><updateOneway/></qos>");
 79          log("subscribe() returned " + srq.GetSubscriptionId());
 80          GC.Collect();
 81          GC.Collect();
 82 
 83          prq = xb.Publish("<key oid='Hello'/>", "publish-2", "<qos/>");
 84          log("publish() returned " + prq.GetKeyOid());
 85 
 86          Thread.Sleep(1000);
 87          Console.WriteLine("There should be some updates, hit a key to continue ...");
 88          Console.ReadLine();
 89 
 90          srq = xb.Subscribe("<key oid='TestPInvoke'/>", "<qos/>");
 91          log("subscribe() returned " + srq.GetSubscriptionId());
 92 
 93          srq = xb.Subscribe("<key oid='TestPInvoke'/>", "<qos/>");
 94          log("subscribe() returned " + srq.GetSubscriptionId());
 95 
 96          UnSubscribeReturnQos[] urq = xb.UnSubscribe("<key oid='TestPInvoke'/>", "<qos/>");
 97          log("unSubscribe() returned");
 98          for (int i = 0; i < urq.Length; i++)
 99             log("unSubscribeReturn #" + i + ": " + urq[i].GetSubscriptionId());
100          GC.Collect();
101          GC.Collect();
102 
103          prq = xb.Publish("<key oid='C#C#C#'/>", "more publishes", "<qos/>");
104          log("publish() returned " + prq.GetKeyOid());
105 
106          MsgUnit[] arr = new MsgUnit[6];
107          for (int i=0; i<arr.Length; i++)
108             arr[i] = new MsgUnit("<key oid='C#C#'/>", "oneway-"+i, "<qos/>");
109          xb.PublishOneway(arr);
110          log("publishOneway() send " + arr.Length + " messages");
111 
112          prq = xb.Publish("<key oid='C#'/>", "HIIIHAAAA", "<qos/>");
113          log("publish() returned " + prq.GetRcvTimeNanos());
114 
115          MsgUnit[] msgs = xb.Get("<key oid='C#C#'/>", "<qos><history numEntries='4'/></qos>");
116          log("get(C#C#) returned " + msgs.Length + " messages (get was limited to 4)");
117          for (int i = 0; i < msgs.Length; i++)
118             log(msgs[i].ToString());
119 
120          msgs = xb.Get("<key oid='unknown'/>", "<qos><history numEntries='6'/></qos>");
121          log("get(unknown) returned " + msgs.Length + " messages");
122 
123          EraseReturnQos[] erq = xb.Erase("<key queryType='XPATH'>//key</key>", "<qos/>");
124          log("erase() returned");
125          for (int i = 0; i < erq.Length; i++)
126             log("eraseReturn #" + i + ": " + erq[i].GetKeyOid());
127 
128          string p = xb.Ping("<qos/>");
129          StatusQos pp = new StatusQos(p);
130          log("ping() returned " + pp.GetState());
131 
132          bool b = xb.IsConnected();
133          log("isConnected() returned " + b);
134       }
135 
136       bool drq = xb.Disconnect("<qos/>");
137       log("disconnect() returned " + drq);
138 
139       log("DONE");
140    }
141 
142    #region I_Callback Members

143    public string OnUpdate(string cbSessionId, MsgUnitUpdate msgUnit)
144    {
145       log("OnUpdate() received "+(msgUnit.IsOneway()?"oneway ":"")+"message from xmlBlaster:");
146       if (callbackSessionId != cbSessionId)
147          log("Not authorized");
148       log(msgUnit.ToString());
149       return "<qos><state id='OK'/></qos>";
150       //throw new XmlBlasterException("user.update.illegalArgument", "A test exception from OnUpdate()");

151    }
152    #endregion

153 
154    #region I_LoggingCallback Members

155    public void OnLogging(XmlBlasterLogLevel logLevel, string location, string message)
156    {
157       log(logLevel.ToString() + " " + location + ": " + message);
158    }
159    #endregion

160 
161    #region I_ProgressCallback Members

162    public void OnData(bool read, int currBytesRead, int nbytes)
163    {
164       Console.WriteLine("Read " + currBytesRead + "/" + nbytes + " bytes");
165    }
166    #endregion

167 
168    void log(String str)
169    {
170       Console.WriteLine(str);
171       System.Diagnostics.Debug.WriteLine(str);
172    }
173 }


syntax highlighted by Code2HTML, v. 0.9.1