/// <summary> /// Called while initializing OppCommHandler, to load neighbor records from file. /// </summary> private void readNeighborHistory() { lock (neighborRecords) { neighborRecords.Clear(); StreamReader reader = null; if (File.Exists(neighborFilePath) == false) return; reader = File.OpenText(neighborFilePath); string lastLogDateTime = reader.ReadLine(); while (reader.EndOfStream == false) { //Reading MyDeviceID string deviceID = reader.ReadLine().Trim(); NeighborRecord neighbor = new NeighborRecord(deviceID); //Reading and loading applications string[] applications = reader.ReadLine().Trim().Split(new char[] { ' ' }); foreach (string appID in applications) neighbor.Applications.Add(appID); //Reading and loading RSSI values & qualities string[] rssiValues = reader.ReadLine().Trim().Split(new char[] { ' ' }); string[] rssiQualities = reader.ReadLine().Trim().Split(new char[] { ' ' }); if (rssiValues.Length != rssiQualities.Length) { Logger.addEntry("[OppCommHandler]Internal Error: RSSI Value & Quality not matching"); } for (int i = 0; i < rssiValues.Length; i++) neighbor.addRssi(Int32.Parse(rssiValues[i]), NeighborRecord.getStrengthType(rssiQualities[i])); //Adding neighbor to in-program data strcture neighborRecords.Add(neighbor); } reader.Close(); } }
private void PerformDiscovery(Object placeHolder) { if (radioController.isWiFiRadioON() == true) { //If DiscoveryFlag is true and SSID is not associated with Device ID, SSID has to be re-associated //Reason: If device discovery thread is not involved inn session processing, it should be visible. if (DiscoveryFlag == true && wiFiCard.getAssociatedAP().Equals(makeSSID()) == false) //Disconnected from my SSID { Logger.addEntry("Connecting to proper AP"); bool success = false; int i = 0; do { success = wiFiCard.connectToAP(makeSSID()); i++; } while (success == false && i < DiscoverySSIDTries); if (success == false) Logger.addEntry("Failed to advertise device!"); } else //perform normal device discovery { List<AdHocAccessPoint> neighbors = wiFiCard.getAdHocNeighborCollection(); foreach (AdHocAccessPoint neighbor in neighbors) { string[] neighborSSIDParts = getneighborID(neighbor.Name); if (neighborSSIDParts != null) { string neighborDevID = neighborSSIDParts[0]; //Proceed if only it is not my own device ID if (neighborDevID.Equals(MyDeviceID) == false) { lock (neighborRecords) { NeighborRecord foundRecord = neighborRecords.Find(delegate(NeighborRecord record) { return record.DeviceID.Equals(neighborDevID); }); string[] neighborAppIDs = new string[neighborSSIDParts.Length - 1]; Array.Copy(neighborSSIDParts, 1, neighborAppIDs, 0, neighborAppIDs.Length); if (foundRecord != null) //foundRecord will be null if record not found? YES. { //Getting only the AppIDs foundRecord.updateApps(neighborAppIDs); foundRecord.addRssi(neighbor.SignalStrengthInDecibels, neighbor.SignalStrengthQuality); //Updating 'neighborRecords' not required. Updating on 'foundRecord' updates the list also. } else { //Add new neighbor to database NeighborRecord newRecord = new NeighborRecord(neighborDevID); newRecord.updateApps(neighborAppIDs); newRecord.addRssi(neighbor.SignalStrengthInDecibels, neighbor.SignalStrengthQuality); neighborRecords.Add(newRecord); } } } } } lock (neighborRecords) { //Checks for a neighborRecord to see whether it was found in currently scanned/discovered neighbors. //If it was not found, update its RSSI to NoSignal in 'neighborRecords' List<NeighborRecord> removalList = new List<NeighborRecord>(); foreach (NeighborRecord neighborRecord in neighborRecords) { bool neighborScannedPresently = neighbors.Exists(delegate(AdHocAccessPoint ap) { string[] neighborSSIDParts = getneighborID(ap.Name); if (neighborSSIDParts != null) { string neighborDevID = neighborSSIDParts[0]; return neighborRecord.DeviceID.Equals(neighborDevID); } return false; }); if (neighborScannedPresently == false) { neighborRecord.addRssi(NeighborRecord.NoSignalRSSI, StrengthType.NoSignal); bool neighborFarGone = true; foreach (StrengthType rssiQ in neighborRecord.RssiQualityHistory) { if (rssiQ != StrengthType.NoSignal) { neighborFarGone = false; break; } } if (neighborFarGone == true) removalList.Add(neighborRecord); } } //Delete refrences to old neighbors from 'neighborRecords' foreach (NeighborRecord neighbor in removalList) neighborRecords.Remove(neighbor); } } } else { Logger.addEntry("Radio Error: Restarting Radio.."); radioController.setWiFiRadio(true); } }