Skip to content

Commit fa09475

Browse files
committed
Begin adding DLL self-update feature
1 parent 46708ff commit fa09475

9 files changed

Lines changed: 134 additions & 3 deletions

File tree

app/Project Tracker Server/Project Tracker Server/Program.cs

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,8 @@ private static void BackgroundConsoleHelper() {
235235

236236
Console.ForegroundColor = green;
237237
Console.Clear();
238-
Console.WriteLine("Welcome to the Project Tracker server console!");
238+
Console.WriteLine("Server's IP address: " + GetLocalIP());
239+
Console.WriteLine("\nWelcome to the Project Tracker server console!");
239240
Console.WriteLine("Available actions: ");
240241

241242
Console.ForegroundColor = yellow;
@@ -306,14 +307,19 @@ private static void BackgroundConsoleHelper() {
306307
}
307308

308309
private static void BackgroundStatistics() {
310+
int resetTimer = 600;
311+
long lastMinuteSessions = -1;
309312
while (true) {
313+
resetTimer -= 5;
314+
310315
string serverJson = File.ReadAllText(DATA_FILE);
311316
ServerDataManifest.Rootobject serverDataValues =
312317
JsonConvert.DeserializeObject<ServerDataManifest.Rootobject>(serverJson);
313318

314319
DayOfWeek weekDay = DateTime.Today.DayOfWeek;
315320
DateTime date = DateTime.Now;
316321

322+
// Reset weekly statistics
317323
if (weekDay.ToString() == "Monday" && resetWeek == false) {
318324
resetWeek = true;
319325

@@ -324,26 +330,55 @@ private static void BackgroundStatistics() {
324330
resetWeek = false;
325331
}
326332

333+
// Reset monthly statistics
327334
if (date.Month.ToString() != lastMonth) {
328335
lastMonth = date.Month.ToString();
329336
serverDataValues.MonthlyOpens = 0;
330337
}
331338

339+
// Reset yearly statistics
332340
if (date.DayOfYear == 1) {
333341
serverDataValues.YearlyOpens = 0;
334342
}
335343

344+
// Reset active statistics
345+
if (resetTimer == 0) {
346+
lastMinuteSessions = serverDataValues.CurrentlyOpenSessions;
347+
serverDataValues.CurrentlyOpenSessions = 0;
348+
}
349+
350+
if (serverDataValues.CurrentlyOpenSessions < 0) {
351+
serverDataValues.CurrentlyOpenSessions = 0;
352+
}
353+
336354
if (viewStatistics) {
337355
Console.Clear();
338356
Console.WriteLine("You're currently viewing statistics.");
339357
Console.WriteLine("Press E to go back to the main console menu.\n");
340358

341-
Console.WriteLine("Total opens: " + serverDataValues.AmountOpened);
359+
int secondsUntilReset = resetTimer;
360+
int minutesUntilReset = 0;
361+
362+
while (secondsUntilReset > 59) {
363+
minutesUntilReset++;
364+
secondsUntilReset -= 60;
365+
}
366+
367+
Console.WriteLine("Time until open sessions reset: {0}m {1}s\n", minutesUntilReset, secondsUntilReset);
368+
Console.WriteLine("Currently open sessions: " + serverDataValues.CurrentlyOpenSessions);
369+
if (lastMinuteSessions == -1) {
370+
Console.WriteLine("Last 10m open sessions: waiting for data...");
371+
}
372+
else {
373+
Console.WriteLine("Last 10m open sessions: " + lastMinuteSessions);
374+
}
375+
376+
377+
Console.WriteLine("\nTotal opens: " + serverDataValues.AmountOpened);
342378
Console.WriteLine("Total projects: " + serverDataValues.ProjectsCount);
343379
Console.WriteLine("Opens this year: " + serverDataValues.YearlyOpens);
344380
Console.WriteLine("Opens this month: " + serverDataValues.MonthlyOpens);
345381
Console.WriteLine("Opens this week: " + serverDataValues.WeeklyOpens);
346-
Console.WriteLine("Currently open sessions: " + serverDataValues.CurrentlyOpenSessions);
347382

348383
Console.WriteLine("-----------------");
349384
foreach (string day in serverDataValues.DaysOpened) {
@@ -391,6 +426,10 @@ private static void BackgroundStatistics() {
391426
File.WriteAllText(DATA_FILE, sw.ToString());
392427
sb.Clear();
393428
sw.Close();
429+
430+
if (resetTimer == 0) {
431+
resetTimer = 600;
432+
}
394433
Thread.Sleep(5000);
395434
}
396435
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
5+
using System.Net;
6+
using System.Runtime.CompilerServices;
7+
using System.Runtime.InteropServices;
8+
using System.Text;
9+
using System.Threading;
10+
using System.Threading.Tasks;
11+
12+
namespace Project_Tracker {
13+
class BackgroundProcesses {
14+
// WARNING: READONLY VALUES. IF YOU CHANGE THESE CHANGE IN OTHER FILES AS WELL
15+
private readonly static string TEMP_SERVER_VERSION_LOCATION = Environment.GetFolderPath
16+
(Environment.SpecialFolder.LocalApplicationData) + "/Project Tracker/dll/temp-server-version.dll";
17+
private readonly static string SERVER_DLL_LOCATION = Environment.GetFolderPath
18+
(Environment.SpecialFolder.LocalApplicationData) + "/Project Tracker/dll/Server Communication DLL.dll";
19+
private readonly static string SERVER_DLL_VERSION_LOCATION = Environment.GetFolderPath
20+
(Environment.SpecialFolder.LocalApplicationData) + "/Project Tracker/dll/server-communication-dll-version.txt";
21+
22+
private static double version = 0.1;
23+
24+
25+
[DllImport("kernel32.dll")]
26+
public static extern IntPtr LoadLibrary(string dllToLoad);
27+
[DllImport("kernel32.dll")]
28+
public static extern IntPtr GetProcAddress(IntPtr hModule, string procedureName);
29+
30+
31+
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
32+
private delegate void SetActivity(bool isActive);
33+
private delegate void Refresh();
34+
35+
36+
private static double ServerVersion(string url) {
37+
try {
38+
WebClient client = new WebClient();
39+
client.DownloadFile(new Uri(url), TEMP_SERVER_VERSION_LOCATION);
40+
}
41+
catch (WebException) {
42+
return 0.0;
43+
}
44+
45+
return 0.0;
46+
}
47+
48+
/// <summary>
49+
/// Reads the DLL version file and sets the version variable.
50+
/// </summary>
51+
/// <param name="versionFile">The version file to read from.</param>
52+
/// <returns>True if an update is necessary, false otherwise.</returns>
53+
private static void ReadVersion(string versionFile) {
54+
if (!File.Exists(versionFile)) {
55+
ServerVersion();
56+
}
57+
string fileVersionText = File.ReadAllText(versionFile);
58+
version = Convert.ToDouble(fileVersionText);
59+
60+
61+
62+
}
63+
64+
public static void DataReporting() {
65+
// Add Server Communication DLL
66+
IntPtr serverDll = BackgroundProcesses.LoadLibrary(SERVER_DLL_LOCATION);
67+
IntPtr serverDllSetActivityFunction = BackgroundProcesses.GetProcAddress(serverDll, "SetActivity");
68+
IntPtr serverDllRefreshFunction = BackgroundProcesses.GetProcAddress(serverDll, "Refresh");
69+
70+
SetActivity activity = (SetActivity)Marshal.GetDelegateForFunctionPointer(serverDllSetActivityFunction, typeof(SetActivity));
71+
Refresh refresh = (Refresh)Marshal.GetDelegateForFunctionPointer(serverDllRefreshFunction, typeof(Refresh));
72+
73+
activity(true); // Set activity to true for this program
74+
75+
ReadVersion(SERVER_DLL_VERSION_LOCATION);
76+
77+
while (true) {
78+
refresh();
79+
80+
Thread.Sleep(60000);
81+
}
82+
}
83+
}
84+
}

app/Project Tracker/Project Tracker/Project Tracker.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
<Generator>MSBuild:Compile</Generator>
7474
<SubType>Designer</SubType>
7575
</ApplicationDefinition>
76+
<Compile Include="BackgroundProcesses.cs" />
7677
<Compile Include="NextVersionManifest.cs" />
7778
<Compile Include="SettingsManifest.cs" />
7879
<Compile Include="UpdateManifest.cs" />

app/Server Communication DLL/Server Communication DLL/SetData.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,5 +100,10 @@ public static void SetActivity(bool isActive) {
100100

101101
FileManager.SaveData();
102102
}
103+
104+
public static void Refresh() {
105+
Connections.CreateConnection();
106+
// Add something here about how long the user has been active
107+
}
103108
}
104109
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
192.168.1.206
File renamed without changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1.2
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)