summaryrefslogtreecommitdiff
path: root/dotnet/client-010/addins/ExcelAddIn/ExcelAddIn.cs
blob: 66c9b7a8f9f554f6c4335cfaae376e1badc5d196 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/*
*
* 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.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using Microsoft.Office.Interop.Excel;
using org.apache.qpid.client;
using org.apache.qpid.transport;

namespace ExcelAddIn
{
    public delegate string ProcessMessage(IMessage m);

    /// <summary>   
    /// This interface must be implemented so to use a user defined message processor 
    /// </summary>
    public interface MessageProcessor
    {
        string ProcessMessage(IMessage m);
    }

    [ComVisible(true), ProgId("Qpid")]
    public class ExcelAddIn : IRtdServer 
    {
        private IRTDUpdateEvent _onMessage;
        private readonly Dictionary<int, IMessage> _topicMessages = new Dictionary<int, IMessage>();
        private readonly Dictionary<string, QpidListener> _queueListener = new Dictionary<string, QpidListener>();
        private readonly Dictionary<int, string> _topicQueueName = new Dictionary<int, string>();
        private IClient _client;
        private IClientSession _session;
        private ProcessMessage _messageProcessor;

        #region properties

        public IRTDUpdateEvent OnMessage
        {
            get { return _onMessage; }
        }

        public Dictionary<int, IMessage>  TopicMessages
        {
            get { return _topicMessages; }
        }

        public IClientSession Session
        {
            get { return _session; }
        }

        #endregion


        #region IRtdServer Members

        /// <summary>
        /// Called when Excel requests the first RTD topic for the server. 
        /// Connect to the broker, returns a on success and 0 otherwise
        /// </summary>
        /// <param name="CallbackObject"></param>
        /// <returns></returns>
        public int ServerStart(IRTDUpdateEvent CallbackObject)
        {
            _onMessage = CallbackObject;  
            string host = "localhost";
            string port = "5673";
            string virtualhost = "test";
            string username = "guest";
            string password = "guest";
            _messageProcessor = getMessage;
          
            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"];
            }
            if (ConfigurationManager.AppSettings["ProcessorAssembly"] != null)
            {
                try
                {
                    Assembly a = Assembly.LoadFrom(ConfigurationManager.AppSettings["ProcessorAssembly"]);
                    Object o = a.CreateInstance(ConfigurationManager.AppSettings["ProcessorClass"]);
                    MessageProcessor p = (MessageProcessor) o;
                    _messageProcessor = p.ProcessMessage;
                }
                catch (Exception e)
                {
                    System.Windows.Forms.MessageBox.Show("Error: \n" + e.StackTrace);         
                    return 0;
                }
            }

            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;
        }

        /// <summary>
        /// Called whenever Excel requests a new RTD topic from the RealTimeData server.
        /// </summary>
        /// <param name="TopicID"></param>
        /// <param name="Strings"></param>
        /// <param name="GetNewValues"></param>
        /// <returns></returns>
        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";
        }

        /// <summary>
        /// Called whenever Excel no longer requires a specific topic.
        /// </summary>
        /// <param name="TopicID"></param>
        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<int, IMessage> pair in _topicMessages)
            {
                result.SetValue(pair.Key, 0, pair.Key);
                string value = _messageProcessor(pair.Value);
                result.SetValue(value, 1, pair.Key);                
            }
            TopicCount = _topicMessages.Count; 
            return result;
        }

        public void ServerTerminate()
        {
            
        }

        #endregion
        //END IRTDServer METHODS

        private string getMessage(IMessage m)
        {
            string res;
            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();            
            res = enc.GetString(body);           
            return res;
        }

    }

    class QpidListener : IMessageListener
    {
        private readonly ExcelAddIn _excel;       
        private readonly List<int> _topics = new List<int>();

        public QpidListener(ExcelAddIn excel)
        {
            _excel = excel;
        }

        public void addTopic(int topic)
        {
            _topics.Add(topic);
        }

        public void MessageTransfer(IMessage 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();
        }
    }
}