1 '------------------------------------------------------------------------------
  2 ' XmlBlaster access with asynchronous callbacks from Visual Basic .net
  3 ' Calls are routed over ActiveX encapsulating a Java client bean which
  4 ' connects to xmlBlaster
  5 ' @file xmlBlaster/demo/activex/VisualBasic3.vb
  6 ' @author Marcel Ruff, xmlBlaster@marcelruff.info (2004-03-17)
  7 ' @see http://www.xmlBlaster.org/xmlBlaster/doc/requirements/client.activex.html

  8 ' @see org.xmlBlaster.client.activex.XmlScriptAccess
  9 '------------------------------------------------------------------------------
 10 Imports System
 11 
 12 Module HelloWorld3
 13    Private WithEvents xmlBlaster As XmlScriptAccess.XmlScriptAccessClass
 14    Dim ascii As System.Text.ASCIIEncoding = New System.Text.ASCIIEncoding
 15 
 16    Sub Main()
 17       Call HelloWorld3()
 18    End Sub
 19 
 20    '---------------------------------------------------------------------------
 21    ' This method is called asynchronously from java delivering a message. 
 22    ' As events from java into ActiveX can't deliver a return value
 23    ' or an exception back we need to call either
 24    '    setUpdateReturn()        -> passes a return value to the server
 25    ' or
 26    '    setUpdateException()     -> throws an XmlBlasterException
 27    ' If you forget this the update thread of the java bean will block forever
 28    '---------------------------------------------------------------------------
 29    Private Sub XmlScriptAccess_update(ByVal msg As Object) _
 30                Handles xmlBlaster.XmlScriptAccessSource_Event_update
 31       Try
 32          Dim age As String
 33          age = msg.getQos().getClientProperty("myAge", "-99")
 34          Console.WriteLine("SUCCESS: Update arrived: " & msg.getCbSessionId() & _
 35                  ", oid=" & msg.getKey().getOid() & _
 36                  ", content=" & msg.getContentStr() & _
 37                  ", state=" & msg.getQos().getState() & _
 38                  ", myAge=" & age)
 39 
 40          ' Dim len As Int32 = msg.getContent().length
 41          ' How to pass a byte[]:
 42          Dim str As String
 43          Dim contentSBytes As SByte()
 44          contentSBytes = msg.getContent()
 45          Dim contentBytes As Byte() = New Byte(contentSBytes.Length) {}
 46          Buffer.BlockCopy(contentSBytes, 0, contentBytes, 0, contentSBytes.Length)
 47          str = System.Text.Encoding.ASCII.GetString(contentBytes)
 48          MsgBox("Success, message arrived:" & str)
 49 
 50          xmlBlaster.setUpdateReturn("<qos><state id='OK'/></qos>")
 51       Catch e As SystemException
 52          Console.WriteLine("Exception in update:" & e.ToString())
 53          xmlBlaster.setUpdateException("user.update.internalError", e.ToString())
 54       End Try
 55    End Sub
 56 
 57    '---------------------------------------------------------------------------
 58    ' Connect to xmlBlaster and try all possible methods
 59    '---------------------------------------------------------------------------
 60    Sub HelloWorld3()
 61       Dim key, qos As String
 62 
 63       xmlBlaster = New XmlScriptAccess.XmlScriptAccessClass
 64 
 65       Dim prop As Object = xmlBlaster.createPropertiesInstance()
 66       prop.setProperty("protocol", "SOCKET")
 67       prop.setProperty("-trace", "false")
 68       xmlBlaster.initialize(prop)
 69 
 70       Try
 71          ' Connect to the server
 72          qos = "<qos>" & _
 73                "  <securityService type='htpasswd' version='1.0'>" & _
 74                "   <![CDATA[" & _
 75                "   <user>HelloWorld3</user>" & _
 76                "   <passwd>secret</passwd>" & _
 77                "   ]]>" & _
 78                "  </securityService>" & _
 79                "</qos>"
 80          Dim connectReturnQos As Object
 81          connectReturnQos = xmlBlaster.connect(qos)
 82          Console.WriteLine("Connected to xmlBlaster, sessionId=" & _
 83                            connectReturnQos.getSecretSessionId())
 84 
 85          ' Publish a message
 86          key = "<key oid='HelloWorld3' contentMime='text/xml'>" & _
 87                "  <org.xmlBlaster><demo/></org.xmlBlaster>" & _
 88                "</key>"
 89          Dim content As Byte() = ascii.GetBytes("Hi")
 90          qos = "<qos>" & _
 91                "<clientProperty name='myAge' type='int'>18</clientProperty>" & _
 92                "</qos>"
 93          Dim publishReturnQos As Object
 94          publishReturnQos = xmlBlaster.publishBlob(key, content, qos)
 95          Console.WriteLine("Published message id=" & _
 96                        publishReturnQos.getRcvTimestamp().toXml("", True))
 97 
 98          ' Get synchronous the above message
 99          Dim getMsgArr As Object()
100          getMsgArr = xmlBlaster.get("<key oid='HelloWorld3'/>", "<qos/>")
101          Dim msg As Object
102          For Each msg In getMsgArr
103             Console.WriteLine("Get returned:" & msg.toXml())
104          Next
105 
106          ' Subscribe
107          Dim subscribeReturnQos As Object
108          subscribeReturnQos = xmlBlaster.subscribe("<key oid='HelloWorld3'/>", "<qos/>")
109          Console.WriteLine("Got subscribe response:" & _
110                            subscribeReturnQos.getSubscriptionId())
111 
112          ' Give control to the main loop to receive the update event
113          System.Windows.Forms.Application.DoEvents()
114 
115          ' Publish again, message arrives asynchronously in
116          ' Sub XmlScriptAccess_update() (see above)
117          publishReturnQos = xmlBlaster.publishStr(key, "Ho", qos)
118          Console.WriteLine("Got publish response:" & publishReturnQos.toXml())
119 
120          ' MsgBox("Waiting on messages, try 'java javaclients.HelloWorldPublish -oid HelloWorld3 -content OOO' ...")
121 
122          ' Give control to the main loop to receive the update event
123          System.Windows.Forms.Application.DoEvents()
124 
125          ' UnSubscribe
126          Dim k As String = "<key oid='" & subscribeReturnQos.getSubscriptionId() & "'/>"
127          xmlBlaster.unSubscribe(k, "<qos/>")
128 
129          ' Destroy the topic "HelloWorld3"
130          xmlBlaster.erase("<key oid='HelloWorld3'/>", "<qos/>")
131 
132          ' Leave the server, cleanup resources
133          xmlBlaster.disconnect("<qos/>")
134 
135          ' Pass control to eventLoop ...
136          MsgBox("Click me to finish ...")
137 
138       Catch e As SystemException
139          Console.WriteLine("Exception:" & e.ToString())
140       End Try
141    End Sub
142 End Module


syntax highlighted by Code2HTML, v. 0.9.1