/// <summary> /// Standart Constructor for easy data set. /// </summary> /// <param name="device">Synchronization Device with which it is going to synchronize.</param> /// <param name="synch">Backend Synchronize instance that support behavior.</param> internal SynchForm(SynchronizationDevice device, Synchronization synch) { this.synch = synch; synch.CallbackError += ErrorCallBack; InitializeComponent(); this.Text = "Synchronizace: " + device.Name + " - " + device.IP.ToString(); string lastsynch = device.LastSynchDate == DateTime.MinValue ? "Nikdy" : device.LastSynchDate.ToString(Constants.UserFriendlyDateTimeFormat); labelIP.Text = device.IP.ToString(); labelLastSynch.Text = lastsynch; labelName.Text = device.Name; labelID.Text = device.DeviceID; DialogResult = DialogResult.Cancel; synchronizing = false; }
/// <summary> /// Start synchronizing with device. /// </summary> /// <param name="device">Which device it is going to synchronize.</param> public void SynchronizeWith(SynchronizationDevice device) { clearAndResetDatas(); receiver = new ChangeReceiver(device, this, resultChanges); SendConnectRequest(device.IP); localChangeFinder.Reset(device.LastSynchDate, resultChanges); localChangeFinderThread = new Thread(localChangeFinder.DoWork); localChangeFinderThread.IsBackground = true; localChangeFinderThread.Name = " - Local change Adder Thread"; localChangeFinderThread.Start(LocalListBoxData); DialogResult res; using (SynchForm form = new SynchForm(device, this)) { res = form.ShowDialog(); } if (res == DialogResult.OK) { device.LastSynchDate = DateTime.Now.AddSeconds(5); db.UpdateSynchronizationDevice(device); } }
/// <summary> /// Find devices, it should be called in separate Thread. /// </summary> /// <param name="data">Queue where new devices should be enqued.</param> private void findDevicesParallel(object data) { client = null; try { Queue<SynchronizationDevice> devicesData = (Queue<SynchronizationDevice>)data; HashSet<string> devices = new HashSet<string>(); client = new UdpClient(); var sendData = Encoding.UTF8.GetBytes("Send AppInfo"); var deviceEp = new IPEndPoint(IPAddress.Any, 0); DateTime startFinding = DateTime.Now; client.EnableBroadcast = true; for (int i = 0; i < 4; i++) client.Send(sendData, sendData.Length, new IPEndPoint(IPAddress.Broadcast, Constants.UDPPort)); client.Client.ReceiveTimeout = 10000; while (startFinding.AddSeconds(8) > DateTime.Now) { if (stopRequested) return; byte[] instanceResponseData; try { instanceResponseData = client.Receive(ref deviceEp); } catch (SocketException) { continue; } var instanceResponse = Encoding.UTF8.GetString(instanceResponseData); string[] responseParams = instanceResponse.Split(',', '='); if (responseParams[3] == instanceID || devices.Contains(responseParams[3])) continue; devices.Add(responseParams[3]); SynchronizationDevice device = db.GetSynchronizationDeviceInfo(responseParams[3]); if (device == null) { device = new SynchronizationDevice(responseParams[1], responseParams[3], false, DateTime.MinValue, deviceEp.Address, responseParams[5]); db.InsertSynchronizationDevice(device); } else { device.IP = deviceEp.Address; device.GroupID = responseParams[5]; if (device.Name != responseParams[1]) { device.Name = responseParams[1]; db.UpdateSynchronizationDevice(device); } } lock (devicesData) devicesData.Enqueue(device); } lock (devicesData) devicesData.Enqueue(null); } catch (SocketException) { } finally { if (client != null) lock (client) client.Close(); } }
/// <summary> /// Standart Constructor for easy data set. /// </summary> /// <param name="device">Device with which one this should be synchronized.</param> /// <param name="synch">Reference to synchronization.</param> public ChangeReceiver(SynchronizationDevice device, Synchronization synch, Dictionary<string, Synchronization.ChangeSide> resultChanges) { this.synch = synch; bypassWaiting = false; closed = false; this.resultChanges = resultChanges; listenerThread = new Thread(receivingParallel); listenerThread.IsBackground = true; listenerThread.Name = " - Listener Thread"; listenerThread.Start(device.LastSynchDate); }