//************************************* public bool Join_Swarm(string DstIP, string DstPort,string SrcIP, string SrcPort,string Username) { Thread t1 = null; Thread t2 = null; try { t1 = new Thread(new ThreadStart(mSocket.StartListening)); t1.Start(); t1.IsBackground = true; string cpu = "1"; mSocket.SetIP(SrcIP); mSocket.SetPort(SrcPort); mSocket.SetName(Username); mSocket.SetCPU(cpu); MessageGenerator msg = new MessageGenerator(); string mMsg = msg.msgConnectionRequest (mSocket.GetIP(), mSocket.GetPort(), mSocket.GetName(), mSocket.GetCPU()); AsynchronousClient client = new AsynchronousClient(); client.SetSingleMsg(DstIP, DstPort, mMsg); t2 = new Thread(new ThreadStart(client.SendSingleClient)); t2.Start(); t2.IsBackground = true; return true; } catch (Exception ex) { Debug.WriteLine(ex.Message); return false; } }
private void Connection_Request(object sender, EventArgs e) { MessageGenerator msg = new MessageGenerator(); string DstIp = textBox1.Text; string DstPort = textBox2.Text; string mMsg = msg.msgConnectionRequest (mSocket.GetIP(), mSocket.GetPort(), mSocket.GetName(), mSocket.GetCPU()); AsynchronousClient client = new AsynchronousClient(); client.SetSingleMsg(DstIp, DstPort,mMsg); Thread t = null; t = new Thread(new ThreadStart(client.SendSingleClient)); t.Start(); t.IsBackground = true; this.button1.Enabled = false; this.button2.Enabled = false; this.button3.Enabled = true; }
public void distributeParallelCode(int start, int end, string data, string body, string range) { rng = range; ParallelPortions = new Hashtable(); ParallelResults = new Hashtable(); ParallelForBody = body; ParallelForBody = ParallelForBody.Replace('<', '%'); ParallelForBody = ParallelForBody.Replace('>', '@'); ParallelForData = data ; int numberofStatements = end - start + 1; int numberofComputers = permissions.Count; int StatementPerComputer; if((numberofStatements % numberofComputers)==0) StatementPerComputer = (int)(numberofStatements / numberofComputers); else StatementPerComputer = (int)(numberofStatements / numberofComputers + 1); //split the codes 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; } //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 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); } }
public void PeerGone(string IPPort) { List<string> temp = new List<string>(); foreach (DictionaryEntry item in ParallelPortions) { if (item.Value.ToString() == IPPort) { temp.Add(item.Key.ToString()); } } if (temp.Count > 0) { int nextStatement = 0; int numberofStatements = temp.Count; int numberofComputers = permissions.Count; int StatementPerComputer; if ((numberofStatements % numberofComputers) == 0) StatementPerComputer = (int)(numberofStatements / numberofComputers); else StatementPerComputer = (int)(numberofStatements / numberofComputers + 1); //split the codes foreach (DictionaryEntry Permitted in permissions) { for (int i = 0; i < StatementPerComputer; i++) { ParallelPortions[temp[nextStatement]] = Permitted.Key.ToString(); if (nextStatement >= temp.Count) break; else nextStatement++; } if (nextStatement >= temp.Count) break; } foreach (DictionaryEntry item in ParallelPortions) { if (temp.Contains(item.Key.ToString())) { 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(); } } } }
public void outgoingResult(string index, string result) { string[] temp = Master.Split(':'); string theIP = temp[0]; string thePort = temp[1]; AsynchronousClient client = new AsynchronousClient(); client.SetSingleMsg(theIP, thePort, createParallelResultMessage(index, result)); Thread t = new Thread(new ThreadStart(client.SendSingleClient)); t.IsBackground = true; t.Start(); Thread.Sleep(5); }
public bool RunClicked(string myIP, string myPort) { string[] target = owner.Split(':'); string ip = target[0]; string port = target[1]; MessageGenerator mg = new MessageGenerator(); string request = mg.msgRunRequest(Pid, myIP + ":" + myPort); AsynchronousClient client = new AsynchronousClient(); client.SetSingleMsg(ip, port, request); Thread t = new Thread(new ThreadStart(client.SendSingleClient)); t.IsBackground = true; t.Start(); while (StartFlag == "") Thread.Sleep(500); if (StartFlag == "Fail") return false; else { setMaster(myIP + ":" + myPort); setBackUp(selectBackUp()); if (rnBtnClicked != null) rnBtnClicked(true); if (permissions.Count > 1) { List<string> endPoints = Peers(); Hashtable temp = conn.GetIPtoPeer(); Hashtable tempToSend = new Hashtable(); foreach (DictionaryEntry item in temp) { if (endPoints.Contains(item.Key.ToString())) { tempToSend.Add(item.Key, item.Value); } } AsynchronousClient client1 = new AsynchronousClient(); client1.SetMultiMsg(temp, getMasterBackUpXML(), myIP + ":" + myPort); Thread t1 = new Thread(new ThreadStart(client1.SendMultiClient)); t1.IsBackground = true; t1.Start(); } return true; } }
//************************************* public bool Join_Swarm(string DstIP, string DstPort, string SrcIP, string SrcPort, string Username) { Thread t1 = null; Thread t2 = null; try { string cpu = "1"; mSocket.SetIP(SrcIP); mSocket.SetPort(SrcPort); mSocket.SetName(Username); mSocket.SetCPU(cpu); listenerThread = new Thread(new ThreadStart(mSocket.StartListening)); listenerThread.IsBackground = true; listenerThread.Start(); t1 = new Thread(new ThreadStart(mSocket.StarHeartBeat)); t1.IsBackground = true; t1.Start(); MessageGenerator msg = new MessageGenerator(); string mMsg = msg.msgConnectionRequest (mSocket.GetIP(), mSocket.GetPort(), mSocket.GetName(), mSocket.GetCPU()); AsynchronousClient client = new AsynchronousClient(); client.ErrorExcep += new AsynchronousClient.EventHandlerExcep(Error_Changed); client.SetSingleMsg(DstIP, DstPort, mMsg); // t2 = new Thread(new ThreadStart(client.SendSingleClient)); client.SendSingleClient(); // t2.Start(); // t2.IsBackground = true; Thread.Sleep(2000); if (mSocket.GetIPtoPeer().Count > 0) return true; else return false; } catch (Exception ex) { MessageBox.Show(ex.Message, "Failed to join swarm."); return false; } }
//public void PeerGone(string IPPort) //{ // try // { // List<string> temp = new List<string>(); // foreach (DictionaryEntry item in ParallelPortions) // { // if (item.Value.ToString() == IPPort) // { // temp.Add(item.Key.ToString()); // } // } // if (temp.Count > 0) // { // int nextStatement = 0; // int numberofStatements = temp.Count; // int numberofComputers = permissions.Count; // int StatementPerComputer; // if ((numberofStatements % numberofComputers) == 0) // StatementPerComputer = (int)(numberofStatements / numberofComputers); // else // StatementPerComputer = (int)(numberofStatements / numberofComputers + 1); // //split the codes // foreach (DictionaryEntry Permitted in permissions) // { // for (int i = 0; i < StatementPerComputer; i++) // { // ParallelPortions[temp[nextStatement]] = Permitted.Key.ToString(); // if (nextStatement >= temp.Count) // break; // else // nextStatement++; // } // if (nextStatement >= temp.Count) // break; // } // foreach (DictionaryEntry item in ParallelPortions) // { // if (temp.Contains(item.Key.ToString())) // { // 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(); // } // } // } // } // catch (Exception e) // { Console.WriteLine(e.ToString()); } //} // new function for using large message public void PeerGone(string IPPort) { try { List<string> temp = new List<string>(); foreach (DictionaryEntry item in ParallelPortions) { if (item.Value.ToString() == IPPort) { temp.Add(item.Key.ToString()); } } if (temp.Count > 0) { // first get the portion that is not finished string IndexRange = temp[0]; string startIndex = IndexRange.Substring(0, IndexRange.IndexOf(':')); string endIndex = IndexRange.Substring(IndexRange.IndexOf(':') + 1); int startNumber = Int32.Parse(startIndex); int endNumber = Int32.Parse(endIndex); int numberofStatements = endNumber - startNumber + 1; int numberofComputers = permissions.Count; List<int> lhs = new List<int>(); List<int> rhs = new List<int>(); int numberOfTasks = splitIndex(startNumber, endNumber, numberofComputers, out lhs, out rhs); Console.WriteLine("(PeerGone)Tasks are re-splitted as follows:"); for (int i = 0; i < lhs.Count; ++i) { Console.WriteLine(lhs[i].ToString() + " " + rhs[i].ToString()); } Hashtable rescheduleParallelPortions = new Hashtable(); ParallelPortions.Remove(IndexRange); try { int count = 0; foreach (DictionaryEntry Permitted in permissions) { if (count < numberOfTasks) { string key = lhs[count].ToString() + ":" + rhs[count].ToString(); rescheduleParallelPortions.Add(key, Permitted.Key.ToString()); ParallelPortions.Add(key, Permitted.Key.ToString()); ++count; } } } catch (Exception e) { System.Windows.Forms.MessageBox.Show(e.Message, "first for."); } //int nextStatement = 0; //int numberofStatements = temp.Count; //int numberofComputers = permissions.Count; //int StatementPerComputer; //if ((numberofStatements % numberofComputers) == 0) // StatementPerComputer = (int)(numberofStatements / numberofComputers); //else // StatementPerComputer = (int)(numberofStatements / numberofComputers + 1); ////split the codes //foreach (DictionaryEntry Permitted in permissions) //{ // for (int i = 0; i < StatementPerComputer; i++) // { // ParallelPortions[temp[nextStatement]] = Permitted.Key.ToString(); // if (nextStatement >= temp.Count) // break; // else // nextStatement++; // } // if (nextStatement >= temp.Count) // break; //} foreach (DictionaryEntry item in rescheduleParallelPortions) { //if (temp.Contains(item.Key.ToString())) { string theIP = item.Value.ToString(); theIP = theIP.Substring(0, theIP.IndexOf(':')); string thePort = item.Value.ToString(); thePort = thePort.Substring(thePort.IndexOf(':') + 1); string tmpIndexRange = item.Key.ToString(); string tmpStartIndex = tmpIndexRange.Substring(0, tmpIndexRange.IndexOf(':')); string tmpEndIndex = tmpIndexRange.Substring(tmpIndexRange.IndexOf(':') + 1); AsynchronousClient client = new AsynchronousClient(); client.SetSingleMsg(theIP, thePort, createParallelDistributionMessage(tmpStartIndex, tmpEndIndex)); Thread t = new Thread(new ThreadStart(client.SendSingleClient)); t.IsBackground = true; t.Start(); } } } } catch (Exception e) { Console.WriteLine(e.ToString()); } }
public void outgoingResult(string index, string result) { if (!string.IsNullOrEmpty(Master)) { try { // if count < number of tasks for this machine, put the result into the table // if count == number of tasks for this machine, build a big message and send it to master int indexNum = Int32.Parse(index); if (TaskIndexList.Contains(indexNum)) { if (!ResultCollection.ContainsKey(index)) { ResultCollection.Add(index, result); SubResultCollection.Add(index, result); if (SubResultCollection.Count == 1000 || ResultCollection.Count == TaskIndexList.Count) { string[] temp = Master.Split(':'); string theIP = temp[0]; string thePort = temp[1]; if (SubResultCollection.Count == 1000 && ResultCollection.Count != TaskIndexList.Count) { AsynchronousClient client = new AsynchronousClient(); client.SetSingleMsg(theIP, thePort, createParallelResultMessage(SubResultCollection)); Thread t = new Thread(new ThreadStart(client.SendSingleClient)); t.IsBackground = true; t.Start(); SubResultCollection.Clear(); } if (ResultCollection.Count == TaskIndexList.Count) { AsynchronousClient client = new AsynchronousClient(); client.SetSingleMsg(theIP, thePort, createParallelResultMessage(SubResultCollection)); Thread t = new Thread(new ThreadStart(client.SendSingleClient)); t.IsBackground = true; t.Start(); Thread.Sleep(100); TaskIndexList.Clear(); ResultCollection.Clear(); SubResultCollection.Clear(); } } } } } catch (Exception e) { Console.WriteLine(e.ToString()); } } }
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."); } }
private void createInitialData(string IPPort) { string DstIp = IPPort.Substring(0, IPPort.IndexOf(':')); string DstPort = IPPort.Substring(IPPort.IndexOf(':') + 1); AsynchronousClient client = new AsynchronousClient(); client.SetSingleMsg(DstIp, DstPort, InitialXMLString()); Thread t = null; t = new Thread(new ThreadStart(client.SendSingleClient)); t.Start(); t.IsBackground = true; /* *this is a part of Test1 Console.WriteLine(InitialXMLString()); */ }
public void getRunMsg(String msg) { try { MessageGenerator temp = new MessageGenerator(); string pid = " "; string ipport = ""; string reply = ""; XDocument xml = XDocument.Parse(msg); var q = from x in xml.Elements() select x; //var q = from x in xml.Elements().Descendants("root") select x; pid = q.ElementAt(0).Attributes().ElementAt(1).Value; ipport = q.ElementAt(0).Attributes().ElementAt(2).Value; SwarmMemory sm = GetProg(pid.ToString()); if (sm.getRunFlag() == false) { sm.setRunFlag(true); reply = temp.msgRunSucessReply(pid); } else { reply = temp.msgRunFailReply(pid); } if (ipport != "") { string[] target = ipport.Split(':'); string ip = target[0]; string port = target[1]; AsynchronousClient client = new AsynchronousClient(); client.SetSingleMsg(ip, port, reply); Thread t; t = new Thread(new ThreadStart(client.SendSingleClient)); t.IsBackground = true; t.Start(); } } catch (Exception e) { Console.WriteLine(e.ToString()); } }
private void getHeartBeatRequestMsg(String msg) { try { Thread t; string ipport = GetIP() + ":" + GetPort(); string target = " "; string reply = " "; XDocument xml = XDocument.Parse(msg); var q = from x in xml.Descendants() where (x.Name == "root") select x; foreach (var elem in q) target = elem.Value; MessageGenerator temp = new MessageGenerator(); reply = temp.msgHeartBeatReply(ipport); AsynchronousClient client = new AsynchronousClient(); string[] str = target.Split(':'); string ip = str[0]; string port = str[1]; client.SetSingleMsg(ip, port, reply); t = new Thread(new ThreadStart(client.SendSingleClient)); t.IsBackground = true; t.Start(); } catch (Exception e) { Console.WriteLine(e.ToString()); } }
private void getConnectionRequestMsg(String msg) { try { String ip; String port; String name; String cpu; Thread t1 = null; Thread t2 = null; Thread t3 = null; XDocument xml = XDocument.Parse(msg); var q = from x in xml.Elements("root").Descendants() select x; foreach (var elem in q) { ip = elem.Attributes().ElementAt(0).Value; port = elem.Attributes().ElementAt(1).Value; name = elem.Attributes().ElementAt(2).Value; cpu = elem.Attributes().ElementAt(3).Value; if (IPtoPeer.Count > 0) { if (!NametoIP.Contains(name)) { InsertPeer(ip, port, name, cpu); if (IPtoPeer.Count == 2) SetBackup(ip + ":" + port); MessageGenerator temp = new MessageGenerator(); string mMsg = temp.msgIPtoPeer(IPtoPeer); string master = temp.msgMaster(GetMaster()); string backup = temp.msgBackup(GetBackup()); AsynchronousClient client1 = new AsynchronousClient(); client1.SetMultiMsg(IPtoPeer, mMsg, mPeer.mIP + ":" + mPeer.mPort); t1 = new Thread(new ThreadStart(client1.SendMultiClient)); t1.IsBackground = true; t1.Start(); AsynchronousClient client2 = new AsynchronousClient(); client2.SetSingleMsg(ip, port, master); t2 = new Thread(new ThreadStart(client2.SendSingleClient)); t2.IsBackground = true; t2.Start(); AsynchronousClient client3 = new AsynchronousClient(); client3.SetSingleMsg(ip, port, backup); t3 = new Thread(new ThreadStart(client3.SendSingleClient)); t3.IsBackground = true; t3.Start(); } else { MessageGenerator temp = new MessageGenerator(); string error = temp.msgError("10:Username Already Exists"); AsynchronousClient client = new AsynchronousClient(); client.SetSingleMsg(ip, port, error); t1 = new Thread(new ThreadStart(client.SendSingleClient)); t1.IsBackground = true; t1.Start(); } } else { MessageGenerator temp = new MessageGenerator(); string error = temp.msgError("10:Peer is not a part of Swarm"); AsynchronousClient client = new AsynchronousClient(); client.SetSingleMsg(ip, port, error); t1 = new Thread(new ThreadStart(client.SendSingleClient)); t1.IsBackground = true; t1.Start(); } } showTable(); } catch (Exception e) { Console.WriteLine(e.ToString()); } }