-
Notifications
You must be signed in to change notification settings - Fork 165
Expand file tree
/
Copy pathplotlystream.m
More file actions
278 lines (248 loc) · 11.7 KB
/
plotlystream.m
File metadata and controls
278 lines (248 loc) · 11.7 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
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
classdef plotlystream < handle
% Interface to Plotly's real-time graphing API.
% Initialize a Stream object with a stream_id found in
% {plotly_domain}/settings. Real-time graphs are
% initialized with a call to plotly that embeds your unique
% `stream_id`s in each of the graph's traces. The plotlystream
% class plots data to these traces, as identified with the unique
% stream_id, in real-time. Every viewer of the graph sees
% the same data at the same time.
%----CLASS PROPERTIES----%
properties
Response
Specs
end
properties (Access=private)
URL
ErrorURL
Connection
ErrorConnection
Stream
ErrorStream
end
%----CLASS METHODS----%
methods
%----CONSTRUCTOR---%
function obj = plotlystream(request)
%default stream settings
obj.Specs.Token = '';
%look for specified streaming domain
try
config = loadplotlyconfig;
obj.Specs.Host = config.plotly_streaming_domain;
catch
obj.Specs.Host = 'http://stream.plot.ly';
end
%check if ssl is enabled
if any(strfind(obj.Specs.Host,'https://') == 1)
obj.Specs.SSLEnabled = true;
else
obj.Specs.SSLEnabled = false;
end
%add http if not present on host
if ~obj.Specs.SSLEnabled
if ~any(strfind(obj.Specs.Host,'http://') == 1)
obj.Specs.Host = ['http://' obj.Specs.Host];
end
end
%specify handler
if obj.Specs.SSLEnabled
obj.Specs.Handler = sun.net.www.protocol.https.Handler;
else
obj.Specs.Handler = sun.net.www.protocol.http.Handler;
end
%initialize connection settings
obj.Specs.ReconnectOn = {'','200','408'};
obj.Specs.Timeout = 500;
obj.Specs.Chunklen = 14;
obj.Specs.Closed = true;
obj.Specs.ConnectAttempts = 0;
obj.Specs.ConnectDelay = 1;
obj.Specs.MaxConnectAttempts = 5;
%initialize output response
obj.Response = '';
%check for correct input structure
if nargin > 0
if ischar(request)
obj.Specs.Token = request;
elseif isstruct(request)
%check for tokens (required)
if (isfield(request,'token'))
obj.Specs.Token = request.token;
else
error(['Oops! You did not properly specify a stream token! Please check out the ', ....
'online documentation found @ plot.ly/matlab for more information or contact ',...
'chuck@plot.ly']);
end
if isfield(request,'host')
obj.Specs.Host = request.host;
end
if isfield(request,'timeout')
obj.Specs.Timeout = request.timeout;
end
if isfield(request,'handler')
obj.Specs.Handler= request.handler;
end
if isfield(request,'chunklen')
obj.Specs.Chunklen= request.chunklen;
end
else
error(['Oops! It appears that the specified input argument used to ',...
'initialize the plotlystream object was not of type char or struct. ',...
'Please check out the online documentation found @ plot.ly/matlab ',...
'for more information or contact chuck@plot.ly']);
end
else
error(['Oops! You did not properly specify a stream token! Please check out the ', ....
'online documentation found @ plot.ly/matlab for more information or contact ',...
'chuck@plot.ly']);
end
end
%-----------OPEN STREAM-----------%
function obj = open(obj)
try obj.connect;
%Connection successful!
fprintf('\n[Connection Successful]\n\n');
%update state
obj.resetretries;
obj.Specs.Closed = false;
catch ME
error(['Oops! The following error occurred when trying to write to the stream: ',...
ME.message '. Please check the online documentation ', ...
'found @ plot.ly/matlab for more information or contact chuck@plot.ly']);
end
end
%-----------CONNECT TO STREAM-----------%
function obj = connect(obj)
obj.URL = java.net.URL([],obj.Specs.Host,obj.Specs.Handler);
% Get the proxy information using MathWorks facilities for unified proxy
% preference settings.
mwtcp = com.mathworks.net.transport.MWTransportClientPropertiesFactory.create();
proxy = mwtcp.getProxy();
% Open a connection to the URL.
if isempty(proxy)
obj.Connection = obj.URL.openConnection(); %throws an I/O exception
else
obj.Connection = obj.URL.openConnection(proxy); %throws an I/O exception
end
obj.Connection.setChunkedStreamingMode(obj.Specs.Chunklen)
obj.Connection.setRequestMethod('POST');
obj.Connection.setDoOutput(true);
obj.Connection.setReadTimeout(obj.Specs.Timeout);
obj.Connection.setRequestProperty('plotly-streamtoken', obj.Specs.Token);
obj.Stream = obj.Connection.getOutputStream; %throws an I/O exception
end
%-----------WRITE STREAM-----------%
function obj = write(obj,request)
if nargin ~= 2
error(['Oops! It appears that not enough input arguments were ',...
'specified to the write method of your plotlystream object. ',...
'Please check out the online documentation found @ plot.ly/matlab ',...
'for more information or contact chuck@plot.ly']);
else
if ~isstruct(request)
error(['Oops! It appears that the input argument to the write method ',...
'of your plotlystream object is not a structure array as required. ',...
'Please check out the online documentation found @ plot.ly/matlab ',...
'for more information or contact chuck@plot.ly']);
end
body = request;
%make sure we did not close the stream
if (~obj.Specs.Closed)
try
%write to stream
obj.Stream.write(unicode2native(sprintf([m2json(body) '\n']),''));
catch ME
%error due to stream not being open (creation of Stream object)
if strcmp(ME.message, 'Attempt to reference field of non-structure array.')
error(['Oops! A connection has not yet been established. Please open',...
' a connection by firsting calling the ''open'' method of your',...
' plotlystream object.']);
else
%---reconnect---%
obj.getresponse;
if any(strcmp(obj.Specs.ReconnectOn,obj.Response))
if~strcmp(obj.Response,'')
fprintf(['\n[Connection Failed due to HTTP error: ' obj.Response '] Reconnecting...\n\n']);
else
fprintf('\n[Connection Failed] Reconnecting...\n\n');
end
obj.reconnect;
%add recursion call to not drop data
obj.write(body);
else
error(['Oops! The following error occurred when trying to write to the stream: ',...
ME.message '. No attempt to reconnect was made because the response code ',...
'of: ' obj.Response ' did not match any of the response codes specified in ',...
'the obj.Specs.ReconnectOn parameter. Please check out the online documentation ', ...
'found @ plot.ly/matlab for more information or contact chuck@plot.ly']);
end
end
end
else
error(['Oops! The connection is closed. Please open ',...
'a connection by calling the ''open'' method of your ',...
'plotlystream object.']);
end
end
end
%-----------CLOSE STREAM-----------%
function obj = close(obj)
try
obj.Stream.close;
catch ME
if (strcmp(ME.message, 'Attempt to reference field of non-structure array.'))
error(['Oops! A connection has not yet been established. Please open',...
' a connection by firsting calling the ''open'' method of your',...
' plotlystream object.']);
end
end
%update reconnect state
obj.resetretries;
obj.Specs.Closed = true;
end
%-----------RECONNECT-----------%
function obj = reconnect(obj)
try
obj.Specs.ConnectAttempts = obj.Specs.ConnectAttempts + 1;
%try to connect
obj.connect;
%Connection successful!
fprintf('\n[Connection Successful]\n\n');
%update state
obj.resetretries;
obj.Specs.Closed = false;
catch
if (obj.Specs.ConnectAttempts <= obj.Specs.MaxConnectAttempts)
fprintf(['\n[Connection Failed] Attempt:' num2str(obj.Specs.ConnectAttempts) ' to reconnect...'])
pause(obj.Specs.ConnectDelay);
obj.Specs.ConnectDelay = 2*obj.Specs.ConnectDelay; %delay grows by factor of 2
obj.reconnect;
else
fprintf('\n');
error(['Oops! All attempts to reconnect were unsuccessful. ',...
'Please check out the online documentation found @ plot.ly/matlab ',...
'for more information or contact chuck@plot.ly']);
end
end
end
%-----------GET RESPONSE-----------%
function obj = getresponse(obj)
try
obj.Response = num2str(obj.Connection.getResponseCode);
catch ME
if (strcmp(ME.message, 'Attempt to reference field of non-structure array.'))
error(['Oops! A connection has not yet been established. Please open',...
' a connection by firsting calling the ''open'' method of your',...
' plotlystream object.']);
end
end
end
%-----------RESET RETRIES-----------%
function obj = resetretries(obj)
%reset the connect counter and delay
obj.Specs.ConnectAttempts = 0;
obj.Specs.ConnectDelay = 1;
end
end
end