Commits

Nick Wade committed ae69a5d

Tried refactoring worker.ml to get reduce to work

Comments (0)

Files changed (1)

worker_server/worker.ml

                     else failwith "Connection error!"
                   )
                 | (None, err) ->
-                  ( if (send_response client (Mapper (None, err))) then
+                  ( if (send_response client (Mapper (None, "error error!"))) then
                       handle_request client
                     else
                       failwith "Connection error!"                  
                     else failwith "Connection error!"
                   )
                 | (None, err) ->
-                  ( if (send_response client (Reducer (None, err))) then
+                  ( if (send_response client (Reducer (None, "error error!"))) then
                       handle_request client
                     else
                       failwith "Connection error!"                  
             end
             
         | MapRequest (id, k, v) -> 
-            if (Hashtbl.mem mappers id) then 
-            ( begin
-                match Program.run id (k,v) with
-                | Some v ->
-                  ( if (send_response client (MapResults (id, v))) then
-                    handle_request client
-                  )
-                | None ->
-                  ( if (send_response client (RuntimeError (id, "error"))) then
-                    handle_request client
-                  )              
-              end
-            )            
-            else if (send_response client (InvalidWorker id)) then 
-              handle_request client
-                        
+            let m_result =
+              if (Hashtbl.mem mappers id) then
+                begin
+                  let prog_result = Program.run id (k,v) in
+                  match prog_result with
+                    | None -> RuntimeError (id, "error")
+                    | Some v -> MapResults (id, v)
+                end
+              else InvalidWorker id in
+              
+              if (send_response client m_result) then handle_request client
+              else (); (* Nothing left to do *)
           
         | ReduceRequest (id, k, v) -> 
-            if (Hashtbl.mem reducers id) then 
-            ( begin
-                match Program.run id (k,v) with
-                | Some v ->
-                  ( if (send_response client (ReduceResults (id, v))) then
-                    handle_request client
-                  )
-                | None ->
-                  ( if (send_response client (RuntimeError (id, "error"))) then
-                    handle_request client
-                  )              
-              end
-            )            
-            else if (send_response client (InvalidWorker id)) then 
-              handle_request client        
+            let r_result =
+              if (Hashtbl.mem reducers id) then
+                begin
+                  let prog_result = Program.run id (k,v) in
+                  match prog_result with
+                    | None -> (RuntimeError (id, "error");)
+                    | Some x -> (ReduceResults (id, x))
+                end
+              else InvalidWorker id in
+              
+              if (send_response client r_result) then handle_request client
+              else (); (* Nothing left to do *)  
     end
   | None ->
       Connection.close client;