Display Progress Monitor for HTTP Message
This example shows how to implement a progress monitor,
MyProgressMonitor
, that displays a progress bar for data
transferred to and from a website. The monitor displays a progress bar in a window
created by the MATLAB®
waitbar
function. It uses set.Direction
and
set.Value
methods to monitor changes to the
Direction
and Value
properties.
Each time MATLAB sets the Direction
property,
MyProgressMonitor
creates a progress bar window and displays
either a sending or a receiving message.
Create the following MyProgressMonitor
class file.
The class initializes the Interval
property to
.001
seconds because the example sends only 1 MB of data.
The small interval allows you to observe the progress bar.
classdef MyProgressMonitor < matlab.net.http.ProgressMonitor properties ProgHandle Direction matlab.net.http.MessageType Value uint64 NewDir matlab.net.http.MessageType = matlab.net.http.MessageType.Request end methods function obj = MyProgressMonitor obj.Interval = .001; end function done(obj) obj.closeit(); end function delete(obj) obj.closeit(); end function set.Direction(obj, dir) obj.Direction = dir; obj.changeDir(); end function set.Value(obj, value) obj.Value = value; obj.update(); end end methods (Access = private) function update(obj,~) % called when Value is set import matlab.net.http.* if ~isempty(obj.Value) if isempty(obj.Max) % no maximum means we don't know length, so message % changes on every call value = 0; if obj.Direction == MessageType.Request msg = sprintf('Sent %d bytes...', obj.Value); else msg = sprintf('Received %d bytes...', obj.Value); end else % maximum known, update proportional value value = double(obj.Value)/double(obj.Max); if obj.NewDir == MessageType.Request % message changes only on change of direction if obj.Direction == MessageType.Request msg = 'Sending...'; else msg = 'Receiving...'; end end end if isempty(obj.ProgHandle) % if we don't have a progress bar, display it for first time obj.ProgHandle = ... waitbar(value, msg, 'CreateCancelBtn', ... @(~,~)cancelAndClose(obj)); obj.NewDir = MessageType.Response; elseif obj.NewDir == MessageType.Request || isempty(obj.Max) % on change of direction or if no maximum known, change message waitbar(value, obj.ProgHandle, msg); obj.NewDir = MessageType.Response; else % no direction change else just update proportional value waitbar(value, obj.ProgHandle); end end function cancelAndClose(obj) % Call the required CancelFcn and then close our progress bar. % This is called when user clicks cancel or closes the window. obj.CancelFcn(); obj.closeit(); end end function changeDir(obj,~) % Called when Direction is set or changed. Leave the progress % bar displayed. obj.NewDir = matlab.net.http.MessageType.Request; end end methods (Access=private) function closeit(obj) % Close the progress bar by deleting the handle so % CloseRequestFcn isn't called, because waitbar calls % cancelAndClose(), which would cause recursion. if ~isempty(obj.ProgHandle) delete(obj.ProgHandle); obj.ProgHandle = []; end end end end
To start the operation, specify the progress monitor.
opt = matlab.net.http.HTTPOptions(... 'ProgressMonitorFcn',@MyProgressMonitor,... 'UseProgressMonitor',true);
Create the data.
x = ones(1000000,1,'uint8');
body = matlab.net.http.MessageBody(x);
Create the message. The httpbin.org/put
service returns
data received in a PUT
message.
url = matlab.net.URI('http://httpbin.org/put');
method = matlab.net.http.RequestMethod.PUT;
req = matlab.net.http.RequestMessage(method,[],body);
Send the message.
[resp,~,hist] = req.send(url,opt);