static Guid FindUserId() { // Ensure we assign only 1 ID to each user; even when he has multiple UDC databases because there // are multiple SharpDevelop versions installed. We do this by reading out the userID GUID from // the existing databases in any neighbor config directory. string[] otherSharpDevelopVersions; try { otherSharpDevelopVersions = Directory.GetDirectories(Path.Combine(PropertyService.ConfigDirectory, "..")); } catch (IOException) { otherSharpDevelopVersions = new string[0]; } catch (UnauthorizedAccessException) { otherSharpDevelopVersions = new string[0]; } LoggingService.Debug("Looking for existing UDC database in " + otherSharpDevelopVersions.Length + " directories"); foreach (string path in otherSharpDevelopVersions) { string dbFileName = Path.Combine(path, "usageData.dat"); if (File.Exists(dbFileName)) { LoggingService.Info("Found existing UDC database: " + dbFileName); Guid?guid = UsageDataSessionWriter.RetrieveUserId(dbFileName); if (guid.HasValue) { LoggingService.Info("Found GUID in existing UDC database: " + guid.Value); return(guid.Value); } } } LoggingService.Info("Did not find existing UDC database; creating new GUID."); return(Guid.NewGuid()); }
public void CloseSession() { lock (lockObj) { if (session != null) { session.Dispose(); session = null; } } }
/// <summary> /// Opens the database connection, updates the database if required. /// Will start an upload to the server, if required. /// </summary> public void OpenSession() { IEnumerable <UsageDataEnvironmentProperty> appEnvironmentProperties = GetAppProperties(); bool sessionOpened = false; lock (lockObj) { if (session == null) { try { session = new UsageDataSessionWriter(dbFileName, FindUserId); } catch (IncompatibleDatabaseException ex) { if (ex.ActualVersion < ex.ExpectedVersion) { LoggingService.Info("AnalyticsMonitor: " + ex.Message + ", removing old database"); Guid?oldUserId = UsageDataSessionWriter.RetrieveUserId(dbFileName); // upgrade database by deleting the old one TryDeleteDatabase(); try { session = new UsageDataSessionWriter(dbFileName, () => (oldUserId ?? FindUserId())); } catch (IncompatibleDatabaseException ex2) { LoggingService.Warn("AnalyticsMonitor: Could not upgrade database: " + ex2.Message); } } else { LoggingService.Warn("AnalyticsMonitor: " + ex.Message); } } if (session != null) { session.OnException = MessageService.ShowException; session.AddEnvironmentData(appEnvironmentProperties); sessionOpened = true; } } } if (sessionOpened) { UsageDataUploader uploader = new UsageDataUploader(dbFileName, ProductName); uploader.EnvironmentDataForDummySession = appEnvironmentProperties; ThreadPool.QueueUserWorkItem(delegate { uploader.StartUpload(UploadUrl); }); } }
/// <summary> /// Opens the database connection, updates the database if required. /// Will start an upload to the server, if required. /// </summary> public void OpenSession() { IEnumerable<UsageDataEnvironmentProperty> appEnvironmentProperties = GetAppProperties(); bool sessionOpened = false; lock (lockObj) { if (session == null) { try { session = new UsageDataSessionWriter(dbFileName); } catch (IncompatibleDatabaseException ex) { if (ex.ActualVersion < ex.ExpectedVersion) { LoggingService.Info("AnalyticsMonitor: " + ex.Message + ", removing old database"); // upgrade database by deleting the old one TryDeleteDatabase(); try { session = new UsageDataSessionWriter(dbFileName); } catch (IncompatibleDatabaseException ex2) { LoggingService.Warn("AnalyticsMonitor: Could upgrade database: " + ex2.Message); } } else { LoggingService.Warn("AnalyticsMonitor: " + ex.Message); } } if (session != null) { session.OnException = MessageService.ShowException; session.AddEnvironmentData(appEnvironmentProperties); sessionOpened = true; } } } if (sessionOpened) { UsageDataUploader uploader = new UsageDataUploader(dbFileName, ProductName); uploader.EnvironmentDataForDummySession = appEnvironmentProperties; ThreadPool.QueueUserWorkItem(delegate { uploader.StartUpload(UploadUrl); }); } }
internal FeatureUse(UsageDataSessionWriter writer, DateTime startTime) { this.startTime = startTime; this.writer = writer; }
/// <summary> /// Starts the upload of the usage data. /// </summary> /// <exception cref="IncompatibleDatabaseException">The database version is not compatible with this /// version of the AnalyticsSessionWriter.</exception> public void StartUpload(Binding binding, EndpointAddress endpoint) { UsageDataMessage message; bool addDummySession = false; using (SQLiteConnection connection = OpenConnection()) { using (SQLiteTransaction transaction = connection.BeginTransaction()) { CheckDatabaseVersion(connection); HasUploadedTodayStatus status = HasAlreadyUploadedToday(connection); if (status == HasUploadedTodayStatus.Yes) { message = null; } else { message = FetchDataForUpload(connection, false); if (status == HasUploadedTodayStatus.NeverUploaded) { addDummySession = true; } } transaction.Commit(); } } if (message != null) { string commaSeparatedSessionIDList = GetCommaSeparatedIDList(message.Sessions); if (addDummySession) { // A dummy session is used for the first upload to notify the server of the user's environment. // Without this, we couldn't tell the version of a user who tries SharpDevelop once but doesn't use it long // enough for an actual session to be uploaded. UsageDataSession dummySession = new UsageDataSession(); dummySession.SessionID = 0; dummySession.StartTime = DateTime.UtcNow; dummySession.EnvironmentProperties.AddRange(UsageDataSessionWriter.GetDefaultEnvironmentData()); if (this.EnvironmentDataForDummySession != null) { dummySession.EnvironmentProperties.AddRange(this.EnvironmentDataForDummySession); } message.Sessions.Add(dummySession); } DataContractSerializer serializer = new DataContractSerializer(typeof(UsageDataMessage)); byte[] data; using (MemoryStream ms = new MemoryStream()) { using (GZipStream zip = new GZipStream(ms, CompressionMode.Compress)) { serializer.WriteObject(zip, message); } data = ms.ToArray(); } UdcProxy.UDCUploadServiceClient client = new UdcProxy.UDCUploadServiceClient(binding, endpoint); try { client.BeginUploadUsageData(productName, new MemoryStream(data), ar => UsageDataUploaded(ar, client, commaSeparatedSessionIDList), null); } catch (CommunicationException) { // ignore error (maybe currently not connected to network) } catch (TimeoutException) { // ignore error (network connection trouble?) } } }