From: Clinton Ebadi Date: Tue, 6 May 2014 23:17:46 +0000 (-0400) Subject: Connection utilities (or: copying and pasting code is bad) X-Git-Tag: release_20140509~8 X-Git-Url: https://git.hcoop.net/hcoop/domtool2.git/commitdiff_plain/2b75bae8ada47b59140ee0eebf7de424c0008431?hp=2462aefc2f65464b9f2812d7c01b81c03d4ab9ff Connection utilities (or: copying and pasting code is bad) Finally get around to factoring out functions to connect to the dispatcher, connect to a worker, and send a "simple" message to workers (one where MsgOk/MsgError are the only valid replies). --- diff --git a/src/connect.sig b/src/connect.sig new file mode 100644 index 0000000..fdbb9e9 --- /dev/null +++ b/src/connect.sig @@ -0,0 +1,29 @@ +(* HCoop Domtool (http://hcoop.sourceforge.net/) + * Copyright (c) 2014 Clinton Ebadi + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + *) + +(* Convenience interface for connecting to workers *) + +signature CONNECT = sig + + val requestDispatcherBio : OpenSSL.context -> OpenSSL.bio + val requestWorkerBio : OpenSSL.context * string -> OpenSSL.bio + + val commandWorker : OpenSSL.context * string * MsgTypes.msg -> bool + (* Tell a node to do something *) + +end diff --git a/src/connect.sml b/src/connect.sml new file mode 100644 index 0000000..2f3d25c --- /dev/null +++ b/src/connect.sml @@ -0,0 +1,58 @@ +(* HCoop Domtool (http://hcoop.sourceforge.net/) + * Copyright (c) 2014 Clinton Ebadi + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + *) + +structure Connect :> CONNECT = struct + +open MsgTypes + +fun requestDispatcherBio context = + OpenSSL.connect true (context, Domain.nodeIp Config.dispatcherName ^ ":" ^ Int.toString Config.dispatcherPort) + +fun requestWorkerBio (context, node) = + OpenSSL.connect true (context, Domain.nodeIp node ^ ":" ^ Int.toString Config.slavePort) + +fun commandWorker (context, node, message) = +(* It might be worth making this context -> node -> message -> bool *) + let + val bio = requestWorkerBio (context, node) + in + Msg.send (bio, message); + (case Msg.recv bio of + NONE => (print "Slave closed connection unexpectedly\n"; + false) + | SOME m => + case m of + MsgOk => (print ("Slave " ^ node ^ " finished\n"); + true) + | MsgError s => (print ("Slave " ^ node + ^ " returned error: " ^ + s ^ "\n"); + false) + | _ => (print ("Slave " ^ node + ^ " returned unexpected command\n"); + false)) + before OpenSSL.close bio + handle OpenSSL.OpenSSL s => (print ("OpenSSL error: " ^ s ^ "\n"); + OpenSSL.close bio; + false + handle OpenSSL.OpenSSL _ => false) + end + handle OpenSSL.OpenSSL s => (print ("OpenSSL error: " ^ s ^ "\n"); + false) + +end diff --git a/src/sources b/src/sources index 17a9fd4..195d1bb 100644 --- a/src/sources +++ b/src/sources @@ -69,6 +69,9 @@ msg.sml domain.sig domain.sml +connect.sig +connect.sml + plugins/bind.sig plugins/bind.sml