Index: branches/MPI/HeuristicLab.ExtLibs/HeuristicLab.MPInet/MPIAlgorithmRunner/3.3/AlgorithmBroker.cs
===================================================================
--- branches/MPI/HeuristicLab.ExtLibs/HeuristicLab.MPInet/MPIAlgorithmRunner/3.3/AlgorithmBroker.cs	(revision 6388)
+++ branches/MPI/HeuristicLab.ExtLibs/HeuristicLab.MPInet/MPIAlgorithmRunner/3.3/AlgorithmBroker.cs	(revision 6388)
@@ -0,0 +1,115 @@
+﻿#region License Information
+/* HeuristicLab
+ * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
+ *
+ * This file is part of HeuristicLab.
+ *
+ * HeuristicLab 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 3 of the License, or
+ * (at your option) any later version.
+ *
+ * HeuristicLab 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 HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
+ */
+#endregion
+
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Net;
+using System.Net.Sockets;
+using System.ServiceModel;
+using System.ServiceModel.Description;
+using HeuristicLab.Operators.MPISupport;
+using HeuristicLab.Optimization;
+using HeuristicLab.Core;
+using System.Threading;
+                     
+namespace HeuristicLab.MPIAlgorithmRunner {
+  [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
+  class AlgorithmBroker: IAlgorithmBroker {
+    private static int FindFreeTcpPort() {
+      TcpListener l = new TcpListener(IPAddress.Parse("127.0.0.1"), 0);
+      l.Start();
+      int port = ((IPEndPoint)l.LocalEndpoint).Port;
+      l.Stop();
+      return port;
+    }
+
+    private static ServiceHost CreateService(MPI.Communicator communicator) {
+      AlgorithmBroker broker = new AlgorithmBroker(communicator);
+      ServiceHost service = new ServiceHost(broker);
+
+      ContractDescription contract = ContractDescription.GetContract(typeof(IAlgorithmBroker));
+      contract.ContractType = typeof(IAlgorithmBroker);
+      ServiceEndpoint endpoint = new ServiceEndpoint(contract);
+      endpoint.Name = "AlgorithmBrokerEndpoint";
+      NetTcpBinding netTCPBinding = new NetTcpBinding(SecurityMode.None);
+      endpoint.Binding = netTCPBinding;
+      int port = FindFreeTcpPort();
+      endpoint.Address = new EndpointAddress("net.tcp://localhost:" + port + "/AlgorithmBroker");
+      service.AddServiceEndpoint(endpoint);
+
+      return service;
+    }
+
+    public static ServiceHost StartService(MPI.Communicator communicator) {
+      try {
+        ServiceHost service = CreateService(communicator);
+
+        service.Open();
+
+        return service;
+      }
+      catch (AddressAlreadyInUseException) {
+        return StartService(communicator);
+      }
+    }
+
+    private MPI.Communicator communicator;
+
+    public AlgorithmBroker(MPI.Communicator communicator) {
+      this.communicator = communicator;
+      ExitWaitHandle = new EventWaitHandle(false, EventResetMode.ManualReset);
+    }
+
+    #region IAlgorithmBroker Members
+
+    public void TransmitAlgorithm(MPITransportWrapper<IAlgorithm> algorithm) {
+      Console.WriteLine("Transmitting Algorithm...");
+      int clients = MPI.Communicator.world.Group.Size;
+
+      for(int i = 0; i < clients; i++) {
+        int client = i + 1;
+        communicator.Send<MPITransportWrapper<IAlgorithm>>(algorithm, client, 0);
+      }
+    }
+
+    public ItemList<ResultCollection> Results { get; set; }
+
+    public MPITransportWrapper<ItemList<ResultCollection>> GetResults() {
+      Console.WriteLine("Transmitting Results...");
+      return new MPITransportWrapper<ItemList<ResultCollection>>(Results);
+    }
+
+    public EventWaitHandle ExitWaitHandle { get; private set; }
+
+    public bool Terminated { get; set; }
+
+    public bool IsAlgorithmTerminated() {
+      lock (ExitWaitHandle) {
+        if (Terminated)
+          ExitWaitHandle.Set();
+        return Terminated;
+      }
+    }
+    #endregion
+  }
+}
Index: branches/MPI/HeuristicLab.ExtLibs/HeuristicLab.MPInet/MPIAlgorithmRunner/3.3/HeuristicLab.MPIAlgorithmRunner-3.3.csproj
===================================================================
--- branches/MPI/HeuristicLab.ExtLibs/HeuristicLab.MPInet/MPIAlgorithmRunner/3.3/HeuristicLab.MPIAlgorithmRunner-3.3.csproj	(revision 6354)
+++ branches/MPI/HeuristicLab.ExtLibs/HeuristicLab.MPInet/MPIAlgorithmRunner/3.3/HeuristicLab.MPIAlgorithmRunner-3.3.csproj	(revision 6388)
@@ -103,4 +103,7 @@
   </PropertyGroup>
   <ItemGroup>
+    <Reference Include="Microsoft.Hpc.Scheduler, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
+      <HintPath>Microsoft.Hpc.Scheduler.dll</HintPath>
+    </Reference>
     <Reference Include="MPI, Version=1.0.0.0, Culture=neutral, PublicKeyToken=29b4a045737654fe, processorArchitecture=MSIL">
       <HintPath>..\HeuristicLab.ExtLibs\HeuristicLab.MPInet\1.0.0\MPI.dll</HintPath>
@@ -108,4 +111,5 @@
     <Reference Include="System" />
     <Reference Include="System.Core" />
+    <Reference Include="System.ServiceModel" />
     <Reference Include="System.Xml.Linq" />
     <Reference Include="System.Data.DataSetExtensions" />
@@ -115,5 +119,7 @@
   </ItemGroup>
   <ItemGroup>
+    <Compile Include="AlgorithmBroker.cs" />
     <Compile Include="HeuristicLabMPIAlgorithmRunnerPlugin.cs" />
+    <Compile Include="IAlgorithmBroker.cs" />
     <Compile Include="Program.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
Index: branches/MPI/HeuristicLab.ExtLibs/HeuristicLab.MPInet/MPIAlgorithmRunner/3.3/HeuristicLabMPIAlgorithmRunnerPlugin.cs.frame
===================================================================
--- branches/MPI/HeuristicLab.ExtLibs/HeuristicLab.MPInet/MPIAlgorithmRunner/3.3/HeuristicLabMPIAlgorithmRunnerPlugin.cs.frame	(revision 6354)
+++ branches/MPI/HeuristicLab.ExtLibs/HeuristicLab.MPInet/MPIAlgorithmRunner/3.3/HeuristicLabMPIAlgorithmRunnerPlugin.cs.frame	(revision 6388)
@@ -28,8 +28,9 @@
   [Plugin("HeuristicLab.MPIAlgorithmRunner", "3.3.4.$WCREV$")]
   [PluginFile("HeuristicLab.MPIAlgorithmRunner-3.3.exe", PluginFileType.Assembly)]
+  [PluginDependency("HeuristicLab.Collections", "3.3")]
   [PluginDependency("HeuristicLab.Common", "3.3")]
   [PluginDependency("HeuristicLab.Core", "3.3")]
-  [PluginDependency("HeuristicLab.Collections", "3.3")]
   [PluginDependency("HeuristicLab.MPInet", "1.0.0")]
+  [PluginDependency("HeuristicLab.Operators.MPISupport", "3.3")]
   [PluginDependency("HeuristicLab.Optimization", "3.3")]
   [PluginDependency("HeuristicLab.Persistence", "3.3")]
Index: branches/MPI/HeuristicLab.ExtLibs/HeuristicLab.MPInet/MPIAlgorithmRunner/3.3/IAlgorithmBroker.cs
===================================================================
--- branches/MPI/HeuristicLab.ExtLibs/HeuristicLab.MPInet/MPIAlgorithmRunner/3.3/IAlgorithmBroker.cs	(revision 6388)
+++ branches/MPI/HeuristicLab.ExtLibs/HeuristicLab.MPInet/MPIAlgorithmRunner/3.3/IAlgorithmBroker.cs	(revision 6388)
@@ -0,0 +1,43 @@
+﻿#region License Information
+/* HeuristicLab
+ * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
+ *
+ * This file is part of HeuristicLab.
+ *
+ * HeuristicLab 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 3 of the License, or
+ * (at your option) any later version.
+ *
+ * HeuristicLab 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 HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
+ */
+#endregion
+
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.ServiceModel;
+using HeuristicLab.Optimization;
+using HeuristicLab.Operators.MPISupport;
+using HeuristicLab.Core;
+
+namespace HeuristicLab.MPIAlgorithmRunner {
+  [ServiceContract]
+  public interface IAlgorithmBroker {
+    [OperationContract]
+    void TransmitAlgorithm(MPITransportWrapper<IAlgorithm> algorithm);
+
+    [OperationContract]
+    MPITransportWrapper<ItemList<ResultCollection>> GetResults();
+
+    [OperationContract]
+    bool IsAlgorithmTerminated();
+  }
+}
Index: branches/MPI/HeuristicLab.ExtLibs/HeuristicLab.MPInet/MPIAlgorithmRunner/3.3/Program.cs
===================================================================
--- branches/MPI/HeuristicLab.ExtLibs/HeuristicLab.MPInet/MPIAlgorithmRunner/3.3/Program.cs	(revision 6354)
+++ branches/MPI/HeuristicLab.ExtLibs/HeuristicLab.MPInet/MPIAlgorithmRunner/3.3/Program.cs	(revision 6388)
@@ -29,4 +29,7 @@
 using HeuristicLab.Operators.MPISupport;
 using HeuristicLab.Core;
+using Microsoft.Hpc.Scheduler;
+using System.ServiceModel;
+using System.Net;
 
 namespace HeuristicLab.MPIAlgorithmRunner {
@@ -34,28 +37,41 @@
     EventWaitHandle waitHandle = new EventWaitHandle(false, EventResetMode.ManualReset);
 
+    private static void SetJobStatus(string address) {
+      Console.WriteLine("Started service... at " + address);
+
+      // Discover the job's context from the environment
+      String headNodeName = System.Environment.GetEnvironmentVariable("CCP_CLUSTER_NAME");
+      int jobId = System.Convert.ToInt32(System.Environment.GetEnvironmentVariable("CCP_JOBID"));
+
+      // Connect to the head node and get the job
+      IScheduler scheduler = new Scheduler();
+      scheduler.Connect(headNodeName);
+      ISchedulerJob job = scheduler.OpenJob(jobId);
+
+      job.SetCustomProperty("address", address);
+
+      job.Commit();
+    }
+
+    private static string GetAddress(ServiceHost service) {
+      IPHostEntry IPHost = Dns.GetHostByName(Dns.GetHostName());
+      string address = "net.tcp://" + IPHost.AddressList[0].ToString() + ":" + service.ChannelDispatchers[0].Listener.Uri.Port + "/AlgorithmBroker";
+
+      return address;
+    }
+
     static void Main(string[] args) {
-      if (args.Length != 3) {
-        Console.WriteLine("Args:" + args.Length);
+      using (new MPI.Environment(ref args)) {
+        int clients = MPI.Communicator.world.Group.Size;
+        int updateInterval = 5000;
 
-        throw new ArgumentException("You must provide 3 arguments - the algorithm file, the result file and the update interval (in ms)");
-      }
-
-      string fileName = args[0];
-      string resultName = args[1];
-      int updateInterval = int.Parse(args[2]);
-
-      using (new MPI.Environment(ref args)) {
         MPI.Communicator communicator = MPI.Communicator.world.Clone() as MPI.Communicator;
 
         if (communicator.Rank == 0) {
-          IAlgorithm alg = XmlParser.Deserialize<IAlgorithm>(fileName);
-          MPITransportWrapper<IAlgorithm> transport = new MPITransportWrapper<IAlgorithm>(alg);
+          ServiceHost service = AlgorithmBroker.StartService(communicator);
+          AlgorithmBroker broker = (service.SingletonInstance as AlgorithmBroker);
 
-          int clients = communicator.Size - 1;
-          Console.WriteLine("Sending alg to " + clients + " clients");
-          for (int i = 0; i < clients; i++) {
-            int client = i + 1;
-            communicator.Send(transport, client, 0);
-          }
+          string address = GetAddress(service);
+          SetJobStatus(address);
 
           ItemList<ResultCollection> results = new ItemList<ResultCollection>();
@@ -87,10 +103,14 @@
 
             Console.WriteLine("Update results");
-            lock (resultName) {
-              XmlGenerator.Serialize(results, resultName);
-            }
+            broker.Results = results;
           }
 
+          lock (broker.ExitWaitHandle) {
+            broker.Terminated = true;
+          }
+
+          broker.ExitWaitHandle.WaitOne();
           Console.WriteLine("Finished.");
+          service.Close();
         } else {
           Program p = new Program();
