|
1 | 1 | function matlabserver(socket_address)
|
2 |
| -% This function takes a socket address as input and initiates a ZMQ session |
3 |
| -% over the socket. I then enters the listen-respond mode until it gets an |
4 |
| -% "exit" command |
5 |
| - |
6 |
| -json.startup |
7 |
| -messenger('init', socket_address); |
8 |
| - |
9 |
| -while true |
10 |
| - % don't let any errors escape (and crash the server) |
11 |
| - try |
12 |
| - msg_in = messenger('listen'); |
13 |
| - req = json.load(msg_in); |
14 |
| - |
15 |
| - switch(req.cmd) |
16 |
| - case {'connect'} |
17 |
| - messenger('respond', 'connected'); |
18 |
| - |
19 |
| - case {'exit'} |
20 |
| - messenger('exit'); |
21 |
| - clear mex; |
22 |
| - break; |
23 |
| - |
24 |
| - case {'call'} |
25 |
| - fhandle = str2func('pymat_call') |
26 |
| - resp = feval(fhandle, req) |
27 |
| - messenger('respond', resp) |
28 |
| - |
29 |
| - case {'eval'} |
30 |
| - fhandle = str2func('pymat_eval'); |
31 |
| - resp = feval(fhandle, req); |
32 |
| - messenger('respond', resp); |
33 |
| - |
34 |
| - case {'get_var'} |
35 |
| - fhandle = str2func('pymat_get_variable'); |
36 |
| - resp = feval(fhandle, req); |
37 |
| - messenger('respond', resp); |
38 |
| - |
39 |
| - otherwise |
40 |
| - messenger('respond', 'unrecognized command'); |
| 2 | +% MATLABSERVER Run a Matlab server to handle requests from Python over ZMQ |
| 3 | +% |
| 4 | +% MATLABSERVER(SOCKET_ADDRESS) initiates a ZMQ session over the provided |
| 5 | +% SOCKET_ADDRESS. Once started, it executes client requests and returns |
| 6 | +% the response. |
| 7 | +% |
| 8 | +% The recognized requests are: |
| 9 | +% 'ping': Elicit a response from the server |
| 10 | +% 'exit': Request the server to shutdown |
| 11 | +% 'call': Call a Matlab function with the provdided arguments |
| 12 | + |
| 13 | + json.startup |
| 14 | + messenger('init', socket_address); |
| 15 | + |
| 16 | + while true |
| 17 | + % don't let any errors escape (and crash the server) |
| 18 | + try |
| 19 | + msg_in = messenger('listen'); |
| 20 | + req = json.load(msg_in); |
| 21 | + |
| 22 | + switch(req.cmd) |
| 23 | + case {'ping'} |
| 24 | + messenger('respond', 'pong'); |
| 25 | + |
| 26 | + case {'exit'} |
| 27 | + messenger('exit'); |
| 28 | + clear mex; |
| 29 | + break; |
| 30 | + |
| 31 | + case {'call'} |
| 32 | + resp = call(req); |
| 33 | + json_resp = json.dump(resp); |
| 34 | + messenger('respond', json_resp); |
| 35 | + |
| 36 | + otherwise |
| 37 | + throw(MException('MATLAB:matlabserver', ['Unrecognized command ' req.cmd])) |
| 38 | + end |
| 39 | + |
| 40 | + catch exception |
| 41 | + % format the exception and pass it back to the client |
| 42 | + resp.success = false; |
| 43 | + resp.result = exception.identifier; |
| 44 | + resp.message = exception.message; |
| 45 | + |
| 46 | + json_resp = json.dump(resp); |
| 47 | + messenger('respond', json_resp); |
41 | 48 | end
|
| 49 | + end |
| 50 | +end |
| 51 | + |
| 52 | + |
| 53 | +function resp = call(req) |
| 54 | +% CALL Call a Matlab function |
| 55 | +% |
| 56 | +% RESPONSE = CALL(REQUEST) calls Matlab's FEVAL function, intelligently |
| 57 | +% handling the number of input and output arguments so that the argument |
| 58 | +% spec is satisfied. |
| 59 | +% |
| 60 | +% The REQUEST is a struct with three fields: |
| 61 | +% 'func': The name of the function to execute |
| 62 | +% 'args': A cell array of args to expand into the function arguments |
| 63 | +% 'nout': The number of output arguments requested |
| 64 | + |
| 65 | + % function of no arguments |
| 66 | + if ~isfield(req, 'args') |
| 67 | + req.args = {} |
| 68 | + end |
42 | 69 |
|
43 |
| - catch exception |
44 |
| - response.success = false; |
45 |
| - response.result = exception.identifier; |
46 |
| - response.message = exception.message; |
| 70 | + % determine the number of output arguments |
| 71 | + % TODO: What should the default behaviour be? |
| 72 | + func = str2func(req.func); |
| 73 | + nout = req.nout; |
| 74 | + if isempty(nout) |
| 75 | + try |
| 76 | + nout = min(abs(nargout(func)), 1); |
| 77 | + catch |
| 78 | + nout = 1; |
| 79 | + end |
| 80 | + end |
47 | 81 |
|
48 |
| - json_response = json.dump(response); |
49 |
| - messenger('respond', 'json_response'); |
| 82 | + % call the function, taking care of broadcasting outputs |
| 83 | + switch nout |
| 84 | + case 0 |
| 85 | + func(req.args{:}); |
| 86 | + case 1 |
| 87 | + resp.result = func(req.args{:}); |
| 88 | + otherwise |
| 89 | + [resp.result{1:nout}] = func(req.args{:}); |
50 | 90 | end
|
| 91 | + |
| 92 | + % build the response |
| 93 | + resp.success = true; |
| 94 | + resp.message = 'Successfully completed request'; |
51 | 95 | end
|
0 commit comments