//cyklicznie wysyła PING oraz DISCOVER private void Cyclic() { seconds_elapsed_since_last_pings = 0; while (true) { Tuple <double, double> osc_info = null; if (OscillatorUpdator.oscillation_info_domestic.Count > 0) { osc_info = OscillatorUpdator.GetOscInfoDomestic(); } if (connectedInterfaces.Count > 0) { foreach (NetInterface x in interfaces) { if (!x.IsAvailable() && osc_info != null) { x.sendSyncPacket(osc_info); } } } if (seconds_elapsed_since_last_pings > 10) { NetPacket cyclic; if (connectedInterfaces.Count > 0) { foreach (NetInterface x in interfaces) { foreach (NetPacket y in connectedInterfaces) { if (x.GetTargetEndpoint() != null && y.receiver_IP.ToString().Equals(x.GetTargetEndpoint().Address.ToString())) { cyclic = new NetPacket(y); cyclic.data = ""; cyclic.operation = Operations.PING; AddAwaitingToSendPacket(cyclic); } } if (!x.IsAvailable()) { /*if (seconds_to_disconnect[interfaces.IndexOf(x)+1] <= 0) * { * int indexToDelete = -1; * foreach(NetPacket y in connectedInterfaces) * { * if (y.receiver_IP.ToString().Equals(x.GetTargetEndpoint().Address.ToString())) * { * //connectedInterfaces.Remove(y); * indexToDelete = connectedInterfaces.IndexOf(y); * } * } * if (indexToDelete != -1) * { * connectedInterfaces.RemoveAt(indexToDelete); * } * RemoveOfferedInterface(x.interfaceNumber); * x.TerminateConnection(); * }*/ } } } else { cyclic = MakeDiscoverPacket(); AddAwaitingToSendPacket(cyclic); } seconds_elapsed_since_last_pings = 0; } else { seconds_elapsed_since_last_pings++; for (int i = 1; i < seconds_to_disconnect.Length; i++) { seconds_to_disconnect[i]--; } Thread.Sleep(1000); } } }
private void PendulumThread() { //tu będzie oscylacja //nie wiem czy to zadziała //jak bedzie GUI to sie przekonamy while (true) { if (OscillatorUpdator.oscillation_info_foreign.Count > 0) { Tuple <double, double> rcvd_info; rcvd_info = OscillatorUpdator.GetOscInfoForeign(); wychylenie = (wychylenie + rcvd_info.Item1) / 2; frequency = (frequency + rcvd_info.Item2) / 2; } Thread.Sleep((int)(1000 / (frequency * 1000))); wychylenie += (0.001 * kierunek); if (wychylenie > 1 || wychylenie < -1) { kierunek *= -1; if (wychylenie > 1) { wychylenie = 1; } else { wychylenie = -1; } form.TrySendOscillationInformation(); } else { //obsluga w oknie, domyslnie dwa progress bary - jeden normalny "przyklejony" to drugiego //drugi z ustawionym rightToLeft = true, yes, whtvr oscInfoMutex.WaitOne(); if (wychylenie > 0) { if (form.IsHandleCreated) { form.Invoke (new Action(() => { form.progressBar1.Value = (int)(wychylenie * 1000); form.SetPitch(wychylenie); form.SetFrequency(frequency); form.progressBar2.Value = 0; })); } } if (wychylenie < 0) { if (form.IsHandleCreated) { form.Invoke (new Action(() => { form.progressBar2.Value = (-1) * (int)(wychylenie * 1000); form.SetPitch(wychylenie); form.SetFrequency(frequency); form.progressBar1.Value = 0; })); } } oscInfoMutex.ReleaseMutex(); } } }