From cd78f5c69d70b43e5bf82fa9125eb876bc3bbc42 Mon Sep 17 00:00:00 2001 From: Arnaud Simon Date: Thu, 25 Sep 2008 10:05:27 +0000 Subject: QPID-1293: Added an IRtdServer based Excel AddIn that allows receiving messages within Excel git-svn-id: https://svn.apache.org/repos/asf/incubator/qpid/trunk/qpid@698914 13f79535-47bb-0310-9956-ffa450edef68 --- dotnet/client-010/addins/ExcelAddIn/ExcelAddIn.cs | 259 +++++++++++++++++++++ .../client-010/addins/ExcelAddIn/ExcelAddIn.csproj | 59 +++++ .../addins/ExcelAddIn/Properties/AssemblyInfo.cs | 35 +++ .../ExcelAddInProducer/ExcelAddInProducer.csproj | 53 +++++ .../addins/ExcelAddInProducer/Program.cs | 56 +++++ .../ExcelAddInProducer/Properties/AssemblyInfo.cs | 33 +++ dotnet/client-010/client/Client.csproj | 1 + dotnet/client-010/client/client.suo | Bin 130048 -> 204288 bytes 8 files changed, 496 insertions(+) create mode 100644 dotnet/client-010/addins/ExcelAddIn/ExcelAddIn.cs create mode 100644 dotnet/client-010/addins/ExcelAddIn/ExcelAddIn.csproj create mode 100644 dotnet/client-010/addins/ExcelAddIn/Properties/AssemblyInfo.cs create mode 100644 dotnet/client-010/addins/ExcelAddInProducer/ExcelAddInProducer.csproj create mode 100644 dotnet/client-010/addins/ExcelAddInProducer/Program.cs create mode 100644 dotnet/client-010/addins/ExcelAddInProducer/Properties/AssemblyInfo.cs diff --git a/dotnet/client-010/addins/ExcelAddIn/ExcelAddIn.cs b/dotnet/client-010/addins/ExcelAddIn/ExcelAddIn.cs new file mode 100644 index 0000000000..3db0ec287e --- /dev/null +++ b/dotnet/client-010/addins/ExcelAddIn/ExcelAddIn.cs @@ -0,0 +1,259 @@ +/* +* +* Licensed to the Apache Software Foundation (ASF) under one +* or more contributor license agreements. See the NOTICE file +* distributed with this work for additional information +* regarding copyright ownership. The ASF licenses this file +* to you under the Apache License, Version 2.0 (the +* "License"); you may not use this file except in compliance +* with the License. You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, +* software distributed under the License is distributed on an +* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +* KIND, either express or implied. See the License for the +* specific language governing permissions and limitations +* under the License. +* +*/ + +using System; +using System.Collections.Generic; +using System.Configuration; +using System.IO; +using System.Runtime.InteropServices; +using System.Text; +using client.client; +using Microsoft.Office.Interop.Excel; +using org.apache.qpid.client; +using org.apache.qpid.transport; + +namespace ExcelAddIn +{ + [ComVisible(true), ProgId("Qpid")] + public class ExcelAddIn : IRtdServer + { + private IRTDUpdateEvent _onMessage; + private readonly Dictionary _topicMessages = new Dictionary(); + private readonly Dictionary _queueListener = new Dictionary(); + private readonly Dictionary _topicQueueName = new Dictionary(); + private Client _client; + private ClientSession _session; + + #region properties + + public IRTDUpdateEvent OnMessage + { + get { return _onMessage; } + } + + public Dictionary TopicMessages + { + get { return _topicMessages; } + } + + public ClientSession Session + { + get { return _session; } + } + + #endregion + + + #region IRtdServer Members + + /// + /// Called when Excel requests the first RTD topic for the server. + /// Connect to the broker, returns a on success and 0 otherwise + /// + /// + /// + public int ServerStart(IRTDUpdateEvent CallbackObject) + { + _onMessage = CallbackObject; + string host = "localhost"; + string port = "5673"; + string virtualhost = "test"; + string username = "guest"; + string password = "guest"; + if( ConfigurationManager.AppSettings["Host"] != null ) + { + host = ConfigurationManager.AppSettings["Host"]; + } + if (ConfigurationManager.AppSettings["Port"] != null) + { + port = ConfigurationManager.AppSettings["Port"]; + } + if (ConfigurationManager.AppSettings["VirtualHost"] != null) + { + virtualhost = ConfigurationManager.AppSettings["VirtualHost"]; + } + if (ConfigurationManager.AppSettings["UserName"] != null) + { + username = ConfigurationManager.AppSettings["UserName"]; + } + if (ConfigurationManager.AppSettings["Password"] != null) + { + password = ConfigurationManager.AppSettings["Password"]; + } + System.Windows.Forms.MessageBox.Show("Connection parameters: \n host: " + host + "\n port: " + + port + "\n user: " + username); + try + { + _client = new Client(); + _client.connect(host, Convert.ToInt16(port), virtualhost, username, password); + // create a session + _session = _client.createSession(0); + } + catch (Exception e) + { + System.Windows.Forms.MessageBox.Show("Error: \n" + e.StackTrace); + return 0; + } + + // always successful + return 1; + } + + /// + /// Called whenever Excel requests a new RTD topic from the RealTimeData server. + /// + /// + /// + /// + /// + public object ConnectData(int TopicID, ref Array Strings, ref bool GetNewValues) + { + try + { + string queuename = "defaultExcelAddInQueue"; + string destinationName = "ExcelAddIn-" + queuename; + if( Strings.Length > 0 ) + { + queuename = (string) Strings.GetValue(0); + } + // Error message if the queue does not exist + QueueQueryResult result = (QueueQueryResult)_session.queueQuery(queuename).Result; + if( result.getQueue() == null ) + { + System.Windows.Forms.MessageBox.Show("Error: \n queue " + queuename + " does not exist"); + return "error"; + } + + QpidListener listener; + _topicMessages.Add(TopicID, null); + _topicQueueName.Add(TopicID, queuename); + if (_queueListener.ContainsKey(queuename)) + { + listener = _queueListener[queuename]; + listener.addTopic(TopicID); + } + else + { + listener = new QpidListener(this); + listener.addTopic(TopicID); + _queueListener.Add(queuename, listener); + _session.attachMessageListener(listener, destinationName); + _session.messageSubscribe(queuename, destinationName, MessageAcceptMode.EXPLICIT, + MessageAcquireMode.PRE_ACQUIRED, null, 0, null); + // issue credits + _session.messageSetFlowMode(destinationName, MessageFlowMode.WINDOW); + _session.messageFlow(destinationName, MessageCreditUnit.BYTE, ClientSession.MESSAGE_FLOW_MAX_BYTES); + _session.messageFlow(destinationName, MessageCreditUnit.MESSAGE, 1000); + _session.sync(); + } + } + catch (Exception e) + { + System.Windows.Forms.MessageBox.Show("Error: \n" + e.StackTrace); + return "error"; + } + return "waiting"; + } + + /// + /// Called whenever Excel no longer requires a specific topic. + /// + /// + public void DisconnectData(int TopicID) + { + _topicMessages.Remove(TopicID); + string queueName = _topicQueueName[TopicID]; + if (_topicQueueName.Remove(TopicID) && !_topicQueueName.ContainsValue(queueName)) + { + _session.messageStop("ExcelAddIn-" + queueName); + _session.MessageListeners.Remove("ExcelAddIn-" + queueName); + } + } + + public int Heartbeat() + { + return 1; + } + + public Array RefreshData(ref int TopicCount) + { + Array result = new object[2, _topicMessages.Count]; + foreach (KeyValuePair pair in _topicMessages) + { + result.SetValue(pair.Key, 0, pair.Key); + string value = gerMessage(pair.Value); + result.SetValue(value, 1, pair.Key); + } + TopicCount = _topicMessages.Count; + return result; + } + + public void ServerTerminate() + { + + } + + #endregion + //END IRTDServer METHODS + + private string gerMessage(MessageTransfer m) + { + BinaryReader reader = new BinaryReader(m.Body, Encoding.UTF8); + byte[] body = new byte[m.Body.Length - m.Body.Position]; + reader.Read(body, 0, body.Length); + ASCIIEncoding enc = new ASCIIEncoding(); + return enc.GetString(body); + } + + } + + class QpidListener : MessageListener + { + private readonly ExcelAddIn _excel; + private readonly List _topics = new List(); + + public QpidListener(ExcelAddIn excel) + { + _excel = excel; + } + + public void addTopic(int topic) + { + _topics.Add(topic); + } + + public void messageTransfer(MessageTransfer m) + { + foreach (int i in _topics) + { + if (_excel.TopicMessages.ContainsKey(i)) + { + _excel.TopicMessages[i] = m; + } + } + // ack this message + RangeSet rs = new RangeSet(); + rs.add(m.Id); + _excel.Session.messageAccept(rs); + _excel.OnMessage.UpdateNotify(); + } + } +} \ No newline at end of file diff --git a/dotnet/client-010/addins/ExcelAddIn/ExcelAddIn.csproj b/dotnet/client-010/addins/ExcelAddIn/ExcelAddIn.csproj new file mode 100644 index 0000000000..e6f1bd020b --- /dev/null +++ b/dotnet/client-010/addins/ExcelAddIn/ExcelAddIn.csproj @@ -0,0 +1,59 @@ + + + Debug + AnyCPU + 8.0.50727 + 2.0 + {85EFD719-39F6-4471-90FF-9E621430344B} + Library + Properties + ExcelAddIn + Qpid Excel AddIn + + + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + true + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + {B911FFD7-754F-4735-A188-218D5065BE79} + Client + + + + + \ No newline at end of file diff --git a/dotnet/client-010/addins/ExcelAddIn/Properties/AssemblyInfo.cs b/dotnet/client-010/addins/ExcelAddIn/Properties/AssemblyInfo.cs new file mode 100644 index 0000000000..3c80ece77c --- /dev/null +++ b/dotnet/client-010/addins/ExcelAddIn/Properties/AssemblyInfo.cs @@ -0,0 +1,35 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("Qpid Excel AddIn")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("Apache Software Foundation")] +[assembly: AssemblyProduct("Qpid Excel AddIn")] +[assembly: AssemblyCopyright("Copyright © Apache Software Foundation 2008")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("3bbd4414-60df-407f-9c64-c14b221167af")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Revision and Build Numbers +// by using the '*' as shown below: +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/dotnet/client-010/addins/ExcelAddInProducer/ExcelAddInProducer.csproj b/dotnet/client-010/addins/ExcelAddInProducer/ExcelAddInProducer.csproj new file mode 100644 index 0000000000..1d6863332a --- /dev/null +++ b/dotnet/client-010/addins/ExcelAddInProducer/ExcelAddInProducer.csproj @@ -0,0 +1,53 @@ + + + Debug + AnyCPU + 8.0.50727 + 2.0 + {80F00C3B-EB38-4B3B-9F77-68977A30B155} + Exe + Properties + ExcelAddInProducer + Qpid Excel AddIn Producer + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + {B911FFD7-754F-4735-A188-218D5065BE79} + Client + + + + + \ No newline at end of file diff --git a/dotnet/client-010/addins/ExcelAddInProducer/Program.cs b/dotnet/client-010/addins/ExcelAddInProducer/Program.cs new file mode 100644 index 0000000000..51016f7dc5 --- /dev/null +++ b/dotnet/client-010/addins/ExcelAddInProducer/Program.cs @@ -0,0 +1,56 @@ +/* +* +* Licensed to the Apache Software Foundation (ASF) under one +* or more contributor license agreements. See the NOTICE file +* distributed with this work for additional information +* regarding copyright ownership. The ASF licenses this file +* to you under the Apache License, Version 2.0 (the +* "License"); you may not use this file except in compliance +* with the License. You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, +* software distributed under the License is distributed on an +* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +* KIND, either express or implied. See the License for the +* specific language governing permissions and limitations +* under the License. +* +*/ +using System; +using System.Text; +using System.Threading; +using org.apache.qpid.client; +using org.apache.qpid.transport; +using org.apache.qpid.transport.util; + +namespace ExcelAddInProducer +{ + class Program + { + static void Main(string[] args) + { + Client client = new Client(); + Console.WriteLine("Client created"); + client.connect("192.168.1.14", 5672, "test", "guest", "guest"); + Console.WriteLine("Connection established"); + + ClientSession ssn = client.createSession(50000); + Console.WriteLine("Session created"); + ssn.queueDeclare("queue1", null, null); + ssn.exchangeBind("queue1", "amq.direct", "queue1", null); + + for (int i = 0; i < 100; i++) + { + ssn.messageTransfer("amq.direct", MessageAcceptMode.NONE, MessageAcquireMode.PRE_ACQUIRED, + new Header(new DeliveryProperties().setRoutingKey("queue1"), + new MessageProperties().setMessageId(UUID.randomUUID())), + Encoding.UTF8.GetBytes("test: " + i)); + Thread.Sleep(1000); + } + + client.close(); + } + } +} diff --git a/dotnet/client-010/addins/ExcelAddInProducer/Properties/AssemblyInfo.cs b/dotnet/client-010/addins/ExcelAddInProducer/Properties/AssemblyInfo.cs new file mode 100644 index 0000000000..226709ca0b --- /dev/null +++ b/dotnet/client-010/addins/ExcelAddInProducer/Properties/AssemblyInfo.cs @@ -0,0 +1,33 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("Qpid Excel AddIn Producer")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("Apache Software Foundation")] +[assembly: AssemblyProduct("Qpid Excel AddIn Producer")] +[assembly: AssemblyCopyright("Copyright © Apache Software Foundation 2008")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("3416a5c2-eb70-4d77-b401-dfa659bd419e")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/dotnet/client-010/client/Client.csproj b/dotnet/client-010/client/Client.csproj index 0f532bea88..a54e6a4d61 100644 --- a/dotnet/client-010/client/Client.csproj +++ b/dotnet/client-010/client/Client.csproj @@ -172,6 +172,7 @@ + diff --git a/dotnet/client-010/client/client.suo b/dotnet/client-010/client/client.suo index f32ed7f95f..7b9aa32631 100644 Binary files a/dotnet/client-010/client/client.suo and b/dotnet/client-010/client/client.suo differ -- cgit v1.2.1