Connection utilities (or: copying and pasting code is bad)
authorClinton Ebadi <clinton@unknownlamer.org>
Tue, 6 May 2014 23:17:46 +0000 (19:17 -0400)
committerClinton Ebadi <clinton@unknownlamer.org>
Tue, 6 May 2014 23:17:46 +0000 (19:17 -0400)
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).

src/connect.sig [new file with mode: 0644]
src/connect.sml [new file with mode: 0644]
src/sources

diff --git a/src/connect.sig b/src/connect.sig
new file mode 100644 (file)
index 0000000..fdbb9e9
--- /dev/null
@@ -0,0 +1,29 @@
+(* HCoop Domtool (http://hcoop.sourceforge.net/)
+ * Copyright (c) 2014 Clinton Ebadi <clinton@unknownlamer.org>
+ *
+ * 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 (file)
index 0000000..2f3d25c
--- /dev/null
@@ -0,0 +1,58 @@
+(* HCoop Domtool (http://hcoop.sourceforge.net/)
+ * Copyright (c) 2014 Clinton Ebadi <clinton@unknownlamer.org>
+ *
+ * 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
index 17a9fd4..195d1bb 100644 (file)
@@ -69,6 +69,9 @@ msg.sml
 domain.sig
 domain.sml
 
+connect.sig
+connect.sml
+
 plugins/bind.sig
 plugins/bind.sml