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 Hello.cs util/*.cs client/*.cs
  8                     gmcs /unsafe /d:XMLBLASTER_MONO /d:FORCE_PINVOKECE_PLUGIN -debug+ -out:Hello.exe PInvokeCE.cs Hello.cs util/*.cs client/*.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 PInvokeCE.cs Hello.cs util\*.cs client\*.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 System.Collections;
 27 using org.xmlBlaster.client;
 28 
 29 public class Hello : I_Callback, I_ConnectionStateListener
 30 {
 31    const string callbackSessionId = "secretCb";
 32    
 33    static void Main(string[] argv) {
 34       new Hello(argv);
 35    }
 36    
 37    public Hello(string[] argv) {
 38       I_XmlBlasterAccess nc = XmlBlasterAccessFactory.CreateInstance();
 39       nc.Initialize(argv);
 40       nc.Initialize(new Hashtable());
 41       nc.RegisterConnectionListener(this);
 42 
 43       string connectQos = String.Format(
 44          "<qos>\n"+
 45          " <securityService type='htpasswd' version='1.0'>\n"+
 46          "  <![CDATA[\n"+
 47          "   <user>fritz</user>\n"+
 48          "   <passwd>secret</passwd>\n"+
 49          "  ]]>\n"+
 50          " </securityService>\n"+
 51          " <queue relating='callback' maxEntries='50000' maxEntriesCache='10000'>\n"+
 52          "   <callback type='SOCKET' sessionId='{0}'>\n"+
 53          "   </callback>\n"+
 54          " </queue>\n"+
 55          "</qos>", callbackSessionId);  //"    socket://{1}:{2}"+
 56       Console.WriteLine("Connecting with:" + connectQos);
 57 
 58       I_Callback callback = this;
 59       ConnectReturnQos qos = nc.Connect(connectQos, callback);
 60 
 61       Console.WriteLine("Connected." + qos.GetSessionName());
 62 
 63       for (int i=0; i<5; i++) {
 64          SubscribeReturnQos srq = nc.Subscribe("<key oid='Hello'/>", "<qos/>");
 65          Console.WriteLine("subscribe() returned " + srq.GetSubscriptionId());
 66          
 67          //nc.publish("<key oid='Hello'/>", "HIII", "<qos/>");
 68          PublishReturnQos prq = nc.Publish("<key oid='C#C#C#'/>", "HIIIHAAAA", "<qos/>");
 69          Console.WriteLine("publish() returned " + prq.GetKeyOid());         
 70          
 71          prq = nc.Publish("<key oid='C#C#C#'/>", "HIIIHOOO", "<qos/>");
 72          Console.WriteLine("publish() returned " + prq.GetKeyOid());         
 73          
 74          MsgUnitGet[] msgs = nc.Get("<key oid='C#C#C#'/>", "<qos><history numEntries='6'/></qos>");
 75          Console.WriteLine("get() returned " + msgs.Length + " messages");         
 76          
 77          string p = nc.Ping("<qos/>");
 78          Console.WriteLine("ping() returned " + p);         
 79          
 80          bool b = nc.IsConnected();
 81          Console.WriteLine("isConnected() returned " + b);         
 82          
 83          UnSubscribeReturnQos[] urq = nc.UnSubscribe("<key oid='Hello'/>", "<qos/>");
 84          Console.WriteLine("unSubscribe() returned " + urq[0].GetSubscriptionId());         
 85          
 86          EraseReturnQos[] erq = nc.Erase("<key oid='C#C#C#'/>", "<qos/>");
 87          Console.WriteLine("erase() returned " + erq[0].GetKeyOid());
 88          
 89          Console.WriteLine("\nHit a key " + i);
 90          Console.ReadLine();
 91       }
 92       bool drq = nc.Disconnect("<qos/>");
 93       Console.WriteLine("disconnect() returned " + drq);
 94       
 95       Console.WriteLine("DONE");
 96    }
 97    
 98    #region I_Callback Members
 99    public string OnUpdate(string cbSessionId, MsgUnitUpdate msgUnit) {
100       Console.WriteLine("OnUpdate() invoked START ==================");
101       if (callbackSessionId != cbSessionId)
102          Console.WriteLine("Not authorized");
103       Console.WriteLine(msgUnit.GetUpdateKey().ToXml());
104       Console.WriteLine(msgUnit.GetContentStr());
105       Console.WriteLine(msgUnit.GetUpdateQos().ToXml());
106       string ret = "<qos><state id='OK'/></qos>";
107       Console.WriteLine("OnUpdate() invoked DONE ===================");
108       //throw new XmlBlasterException("user.update.illegalArgument", "A test exception from OnUpdate()");
109       return ret;
110    }
111    #endregion
112    #region I_ConnectionStateListener Members
113    public void reachedAlive(ConnectionStateEnum oldState, I_XmlBlasterAccess connection) {
114       Console.WriteLine("reachedAlive() ...");
115    }
116    public void reachedPolling(ConnectionStateEnum oldState, I_XmlBlasterAccess connection) {
117       Console.WriteLine("reachedPolling() ...");
118    }
119    public void reachedDead(ConnectionStateEnum oldState, I_XmlBlasterAccess connection) {
120       Console.WriteLine("reachedDead() ...");
121    }
122    #endregion
123 }


syntax highlighted by Code2HTML, v. 0.9.1