-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathRtdServer.cs
More file actions
187 lines (153 loc) · 5.35 KB
/
RtdServer.cs
File metadata and controls
187 lines (153 loc) · 5.35 KB
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
using Microsoft.Office.Interop.Excel;
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace TestExcelRTDServer {
[
Guid("B6AF4673-200B-413c-8536-1F778AC14DE1"),
ProgId("My.Sample.RtdServer"),
ComVisible(true)
]
public class RtdServer : IRtdServer {
IRTDUpdateEvent m_callback;
Timer m_timer;
Dictionary<int, TopicData> m_topics;
Dictionary<string, CompanyData> companies;
public RtdServer() {
this.companies = new Dictionary<string, CompanyData>();
this.companies.Add("MSFT", new CompanyData("MSFT", 40, 176));
this.companies.Add("FB", new CompanyData("FB", 60, 210));
this.companies.Add("YHOO", new CompanyData("YHOO", 36, 54));
this.companies.Add("NOK", new CompanyData("NOK", 50, 100));
UpdatePrices();
}
public int ServerStart(IRTDUpdateEvent callback) {
m_callback = callback;
m_timer = new Timer();
m_timer.Tick += new EventHandler(TimerEventHandler);
m_timer.Interval = 500;
m_topics = new Dictionary<int, TopicData>();
return 1;
}
public void ServerTerminate() {
if (null != m_timer) {
m_timer.Dispose();
m_timer = null;
}
}
public object ConnectData(int topicId,
ref Array strings,
ref bool newValues) {
if (2 != strings.Length)
return "Two parameters are required";
string symbol = strings.GetValue(0).ToString();
string type = strings.GetValue(1).ToString();
TopicData data = new TopicData(symbol, type);
m_topics[topicId] = data;
m_timer.Start();
return GetNextValue(data);
}
public void DisconnectData(int topicId) {
m_topics.Remove(topicId);
}
public Array RefreshData(ref int topicCount) {
object[,] data = new object[2, m_topics.Count];
int index = 0;
foreach (int topicId in m_topics.Keys) {
data[0, index] = topicId;
data[1, index] = GetNextValue(m_topics[topicId]);
++index;
}
topicCount = m_topics.Count;
m_timer.Start();
return data;
}
public int Heartbeat() {
return 1;
}
void TimerEventHandler(object sender,
EventArgs args) {
m_timer.Stop();
UpdatePrices();
m_callback.UpdateNotify();
}
object GetNextValue(TopicData data) {
CompanyData companyData;
if (companies.TryGetValue(data.Symbol, out companyData)) {
switch (data.Type) {
case "PRICE":
return companyData.Price;
case "CHANGE":
return companyData.Change;
case "SHARES":
return companyData.Shares;
}
}
return "#Invalid";
}
void UpdatePrices() {
foreach (CompanyData companyData in companies.Values)
companyData.UpdatePrice();
}
}
}
public class TopicData {
public TopicData(string symbol, string type) {
Symbol = symbol;
Type = type;
}
public string Symbol { get; }
public string Type { get; }
}
public class CompanyData {
static Random random = new Random();
public CompanyData(string symbol, double quote, int shares) {
this.Symbol = symbol;
this.Quote = quote;
this.Shares = shares;
}
double Quote { get; }
public string Symbol { get; }
public int Shares { get; }
public double Price { get; private set; }
public double Change { get; private set; }
public void UpdatePrice() {
this.Price = Quote + (random.NextDouble() * 10.0 - 5.0);
this.Change = (this.Price - this.Quote) / this.Quote;
}
}
namespace Microsoft.Office.Interop.Excel {
[Guid("A43788C1-D91B-11D3-8F39-00C04F3651B8")]
[InterfaceType(ComInterfaceType.InterfaceIsDual)]
[ComImport()]
[TypeIdentifier]
[ComVisible(true)]
public interface IRTDUpdateEvent {
void UpdateNotify();
// int HeartbeatInterval { get; set; }
// void Disconnect();
}
[Guid("EC0E6191-DB51-11D3-8F3E-00C04F3651B8")]
[InterfaceType(ComInterfaceType.InterfaceIsDual)]
[ComImport()]
[TypeIdentifier]
[ComVisible(true)]
public interface IRtdServer {
[DispId(10)]
int ServerStart(IRTDUpdateEvent callback);
[DispId(11)]
object ConnectData(int topicId,
[MarshalAs(UnmanagedType.SafeArray, SafeArraySubType = VarEnum.VT_VARIANT)] ref Array strings,
ref bool newValues);
[DispId(12)]
[return: MarshalAs(UnmanagedType.SafeArray, SafeArraySubType = VarEnum.VT_VARIANT)]
Array RefreshData(ref int topicCount);
[DispId(13)]
void DisconnectData(int topicId);
[DispId(14)]
int Heartbeat();
[DispId(15)]
void ServerTerminate();
}
}