public static void TestUdpForNumUser(IPEndPoint RemoteEndPoint, SerializationProtocolType ProtocolType, int NumRequestPerUser, int NumUser, String Title, Action <int, int, ClientContext, IApplicationClient, Action> Test) { Console.Write("{0}: ", Title); Console.Out.Flush(); var tll = new Object(); var tl = new List <Task>(); var bcl = new List <UdpClient>(); var ccl = new List <ClientContext>(); var vConnected = new LockedVariable <int>(0); var vCompleted = new LockedVariable <int>(0); var Check = new AutoResetEvent(false); var vError = new LockedVariable <int>(0); for (int k = 0; k < NumUser; k += 1) { Action Completed = null; var n = k; var Lockee = new Object(); IApplicationClient ac; IStreamedVirtualTransportClient vtc; if (ProtocolType == SerializationProtocolType.Binary) { var a = new BinarySerializationClientAdapter(); ac = a.GetApplicationClient(); vtc = new BinaryCountPacketClient(a); } else if (ProtocolType == SerializationProtocolType.Json) { var a = new JsonSerializationClientAdapter(); ac = a.GetApplicationClient(); vtc = new JsonLinePacketClient(a); } else { throw new InvalidOperationException(); } var bc = new UdpClient(RemoteEndPoint, vtc, QueueUserWorkItem); var cc = new ClientContext(); ac.Error += e => { var m = e.Message; Console.WriteLine(m); }; bc.Connect(); Action <Exception> UnknownFaulted = ex => { int OldValue = 0; vError.Update(v => { OldValue = v; return(v + 1); }); if (OldValue <= 10) { Console.WriteLine(String.Format("{0}:{1}", n, ex.Message)); } vCompleted.Update(i => i + 1); Check.Set(); }; bc.ReceiveAsync ( a => { a(); }, UnknownFaulted ); ac.ServerTime(new ServerTimeRequest { }).ContinueWith(tt => { vConnected.Update(i => i + 1); Check.Set(); }); Action f = () => { Test(NumUser, n, cc, ac, Completed); }; var t = new Task(f); lock (tll) { tl.Add(t); } bcl.Add(bc); ccl.Add(cc); int RequestCount = NumRequestPerUser; Completed = () => { if (RequestCount > 0) { RequestCount -= 1; var tt = new Task(f); lock (tll) { tl.Add(t); } tt.Start(); return; } ac.Quit(new QuitRequest { }).ContinueWith(tt => { vCompleted.Update(i => i + 1); Check.Set(); }); }; } while (vConnected.Check(i => i != NumUser)) { Check.WaitOne(); } var Time = Environment.TickCount; lock (tll) { foreach (var t in tl) { t.Start(); } } while (vCompleted.Check(i => i != NumUser)) { Check.WaitOne(); } var TimeDiff = Environment.TickCount - Time; Task.WaitAll(tl.ToArray()); foreach (var t in tl) { t.Dispose(); } foreach (var bc in bcl) { bc.Dispose(); } var NumError = vError.Check(v => v); if (NumError > 0) { Console.WriteLine("{0} Errors", NumError); } Console.WriteLine("{0} Users, {1} Request/User, {2} ms", NumUser, NumRequestPerUser, TimeDiff); }
public static void TestUdpForNumUser(IPEndPoint RemoteEndPoint, SerializationProtocolType ProtocolType, int NumUser, String Title, Action <int, int, ClientContext, IApplicationClient, Action> Test, Action <int, int, ClientContext, IApplicationClient, Action> InitializeClientContext = null, Action <ClientContext[]> FinalCheck = null) { Console.Write("{0}: ", Title); Console.Out.Flush(); var tl = new List <Task>(); var bcl = new List <UdpClient>(); var ccl = new List <ClientContext>(); var tmrl = new List <Timer>(); var vConnected = new LockedVariable <int>(0); var vCompleted = new LockedVariable <int>(0); var Check = new AutoResetEvent(false); var bAbondon = new LockedVariable <Boolean>(false); var vError = new LockedVariable <int>(0); for (int k = 0; k < NumUser; k += 1) { var n = k; var Lockee = new Object(); IApplicationClient ac; IStreamedVirtualTransportClient vtc; if (ProtocolType == SerializationProtocolType.Binary) { var a = new BinarySerializationClientAdapter(); ac = a.GetApplicationClient(); vtc = new BinaryCountPacketClient(a); } else if (ProtocolType == SerializationProtocolType.Json) { var a = new JsonSerializationClientAdapter(); ac = a.GetApplicationClient(); vtc = new JsonLinePacketClient(a); } else { throw new InvalidOperationException(); } var bc = new UdpClient(RemoteEndPoint, vtc, QueueUserWorkItem); var cc = new ClientContext(); var bCompleted = new LockedVariable <Boolean>(false); Action Completed; Action FaultedCompleted; if (Test == TestQuit) { Completed = () => { bCompleted.Update(b => true); vCompleted.Update(i => i + 1); Check.Set(); }; FaultedCompleted = Completed; } else { Completed = () => { ac.Quit(new QuitRequest { }).ContinueWith(tt => { bCompleted.Update(b => true); vCompleted.Update(i => i + 1); Check.Set(); }); }; FaultedCompleted = () => { bCompleted.Update(b => true); vCompleted.Update(i => i + 1); Check.Set(); }; } ac.Error += e => { var m = e.Message; Console.WriteLine(m); }; if (InitializeClientContext != null) { InitializeClientContext(NumUser, n, cc, ac, Completed); } bc.Connect(); Action <Exception> UnknownFaulted = ex => { int OldValue = 0; vError.Update(v => { OldValue = v; return(v + 1); }); if (OldValue <= 10) { Console.WriteLine(String.Format("{0}:{1}", n, ex.Message)); } FaultedCompleted(); }; bc.ReceiveAsync ( a => { a(); }, UnknownFaulted ); ac.ServerTime(new ServerTimeRequest { }).ContinueWith(tt => { vConnected.Update(i => i + 1); Check.Set(); }); var t = new Task ( () => { Test(NumUser, n, cc, ac, Completed); } ); var tmr = new Timer ( o => { if (!bAbondon.Check(b => b)) { return; } if (bCompleted.Check(b => b)) { return; } int OldValue = 0; vError.Update(v => { OldValue = v; return(v + 1); }); if (OldValue <= 10) { Console.WriteLine(String.Format("{0}:{1}", n, "Timedout")); } FaultedCompleted(); }, null, 10000, 10000 ); tl.Add(t); bcl.Add(bc); ccl.Add(cc); tmrl.Add(tmr); } while (vConnected.Check(i => i != NumUser)) { Check.WaitOne(); } var Time = Environment.TickCount; foreach (var t in tl) { t.Start(); } while (vCompleted.Check(i => i != NumUser)) { if (!Check.WaitOne(10000)) { if (vCompleted.Check(i => i > 0)) { bAbondon.Update(b => true); break; } } } var NumMutualWaiting = NumUser - vCompleted.Check(i => i); while (vCompleted.Check(i => i != NumUser)) { Check.WaitOne(); } foreach (var tmr in tmrl) { tmr.Dispose(); } var TimeDiff = Environment.TickCount - Time; Task.WaitAll(tl.ToArray()); foreach (var t in tl) { t.Dispose(); } foreach (var bc in bcl) { bc.Dispose(); } if (FinalCheck != null) { FinalCheck(ccl.ToArray()); } var NumError = vError.Check(v => v); if (NumError > 0) { Console.WriteLine("{0} Errors", NumError); } if (NumMutualWaiting > 0) { Console.WriteLine("{0} Users, {1} ms, {2} MutualWaiting", NumUser, TimeDiff, NumMutualWaiting); } else { Console.WriteLine("{0} Users, {1} ms", NumUser, TimeDiff); } }
async static Task Main(String[] args) // args contains 4 inputs, scene filename, output filename, width, height // We need to figure out where we are getting the IP addresses and port numbers { var width = Int32.Parse(args[2]); var height = Int32.Parse(args[3]); List <int> rowsCompleted = new List <int>(); for (int y = 0; y < height; y++) { rowsCompleted.Add(y); } byte[,,] bitmap = new byte[width, height, 3]; IPEndPoint[] serverArray = ReadServerList("SERVER_LIST"); Queue <IPEndPoint> availableMachines = new Queue <IPEndPoint>(); byte[][] sceneArray = GenerateSceneArray(args[0], width, height); UTF8Encoding utf8 = new UTF8Encoding(); UdpClient client = new UdpClient(3333); //there were specific instructions about what port to use // NEED TO READ SERVER LIST HERE!!!!!(@#(%*^@#(*%)))ac // IPEndPoint testip = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 3334); for (int i = 0; i < sceneArray.Length; i++) { foreach (IPEndPoint ip in serverArray) { client.Send(sceneArray[i], sceneArray[i].Length, ip); } // client.Send(sceneArray[i], sceneArray[i].Length, testip); } var listener = client.ReceiveAsync(); var timeout = Task.Delay(500); var notFinished = true; IPEndPoint queueIP = null; int currentRow = -1; byte[] traceRequest = new byte[8]; while (notFinished) { var action = Task.WaitAny(listener, timeout); switch (action) { case 0: var serverResponse = await listener; var bufferedResponse = serverResponse.Buffer; var packetLength = bufferedResponse.Length; var incomingIP = serverResponse.RemoteEndPoint; if (packetLength == 4) { var missingLine = UnpackMissingLine(bufferedResponse); client.Send(sceneArray[missingLine], sceneArray[missingLine].Length, incomingIP); } else if (packetLength == 0) // Confirmation { if (!availableMachines.Contains(incomingIP)) { availableMachines.Enqueue(incomingIP); Console.WriteLine("Confirmed"); } } else if (packetLength == (4 + width * 3)) // Receiving update { rowsCompleted.Remove(UpdateBitMap(bufferedResponse, bitmap, width)); } listener = client.ReceiveAsync(); break; case 1: timeout = Task.Delay(500); break; default: break; } if (currentRow < rowsCompleted.Count - 1) { currentRow++; } else { currentRow = 0; } if (availableMachines.Count > 0) { if (rowsCompleted.Count > 0) { traceRequest = PackLineRequest(rowsCompleted[currentRow]); queueIP = availableMachines.Dequeue(); availableMachines.Enqueue(queueIP); client.Send(traceRequest, traceRequest.Length, queueIP); } else { notFinished = false; } } } foreach (IPEndPoint ip in serverArray) { client.Send(new byte[0], 0, ip);//tell servers that job is finished } WritePPM(args[1], bitmap); }