public void distributeParallelCode(int start, int end, string data, string body, string range)
{
try
{
rng = range;
ParallelPortions = new Hashtable();
ParallelResults = new Hashtable();
ParallelForBody = body;
ParallelForBody = ParallelForBody.Replace('<', '%');
ParallelForBody = ParallelForBody.Replace('>', '@');
ParallelForData = data;
int numberofStatements = end - start + 1;
numberOfIterations = numberofStatements;
int numberofComputers = permissions.Count;
List<int> lhs = new List<int>();
List<int> rhs = new List<int>();
int numberOfTasks = splitIndex(start, end, numberofComputers, out lhs, out rhs);
Console.WriteLine("Tasks are splitted as follows:");
for (int i = 0; i < lhs.Count; ++i)
{
Console.WriteLine(lhs[i].ToString() + " " + rhs[i].ToString());
}
try
{
int count = 0;
foreach (DictionaryEntry Permitted in permissions)
{
if (count < numberOfTasks)
{
string key = lhs[count].ToString() + ":" + rhs[count].ToString();
ParallelPortions.Add(key, Permitted.Key.ToString());
++count;
}
}
}
catch (Exception e)
{
System.Windows.Forms.MessageBox.Show(e.Message, "first for.");
}
//int StatementPerComputer;
//if ((numberofStatements % numberofComputers) == 0)
// StatementPerComputer = (int)(numberofStatements / numberofComputers);
//else
// StatementPerComputer = (int)(numberofStatements / numberofComputers + 1);
////split the codes
//try
//{
// foreach (DictionaryEntry Permitted in permissions)
// {
// for (int i = 0; i < StatementPerComputer; i++)
// {
// ParallelPortions[start.ToString()] = Permitted.Key.ToString();
// if (start > end)
// break;
// else
// start++;
// }
// if (start > end)
// break;
// }
//}
//catch (Exception e)
//{ System.Windows.Forms.MessageBox.Show(e.Message, "first for."); }
//List<string> calculationComputers = new List<string>();
//int totalCPU = 0;
//foreach (DictionaryEntry allPeers in IPtoCPU)
//{
// foreach (DictionaryEntry permitted in permissions)
// {
// if (allPeers.Key.ToString() == permitted.Key.ToString())
// {
// //this should be a struct
// totalCPU = totalCPU + int.Parse(allPeers.Value.ToString());
// calculationComputers.Add(permitted.Key.ToString());
// }
// }
//}
////=======================================================
//int LinePerCPU = (int)(numberofStatements / totalCPU + 1);
//for (int k = 0; k < calculationComputers.Count; k++)
//{
// int numberofCPU = (int)IPtoCPU[calculationComputers[k].ToString()];
// for (int a = 0; a < numberofCPU * LinePerCPU; a++)
// {
// ParallelPortions[start] = calculationComputers[k].ToString();
// start++;
// if (start > end)
// {
// break;
// }
// }
// if (start > end)
// {
// break;
// }
//}
//=======================================================
//send the codes
try
{
List<string> allIP = new List<string>();
List<string> allPort = new List<string>();
List<string> allKey = new List<string>();
foreach (DictionaryEntry item in ParallelPortions)
{
allIP.Add(item.Value.ToString().Substring(0, item.Value.ToString().IndexOf(':')));
allPort.Add(item.Value.ToString().Substring(item.Value.ToString().IndexOf(':') + 1));
allKey.Add(item.Key.ToString());
}
//foreach (DictionaryEntry item in ParallelPortions)
//{
// string theIP = item.Value.ToString();
// theIP = theIP.Substring(0, theIP.IndexOf(':'));
// string thePort = item.Value.ToString();
// thePort = thePort.Substring(thePort.IndexOf(':') + 1);
// AsynchronousClient client = new AsynchronousClient();
// client.SetSingleMsg(theIP, thePort, createParallelDistributionMessage(item.Key.ToString()));
// Thread t = new Thread(new ThreadStart(client.SendSingleClient));
// t.IsBackground = true;
// t.Start();
// Thread.Sleep(5);
//}
for (int i = 0; i < lhs.Count; i++)
{
AsynchronousClient client = new AsynchronousClient();
client.SetSingleMsg(allIP[i], allPort[i], createParallelDistributionMessage(lhs[i].ToString(), rhs[i].ToString()));
Thread t = new Thread(new ThreadStart(client.SendSingleClient));
t.IsBackground = true;
t.Start();
Thread.Sleep(5);
}
}
catch (Exception e)
{ System.Windows.Forms.MessageBox.Show(e.Message, "seconde for."); }
}
catch (Exception e) { System.Windows.Forms.MessageBox.Show(e.Message, "distributeParallelCode."); }
}