Skip to content

Commit

Permalink
Make LastSeen concurrent
Browse files Browse the repository at this point in the history
  • Loading branch information
veniware committed May 5, 2024
1 parent 255ae73 commit 8de072d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 13 deletions.
7 changes: 3 additions & 4 deletions Protest/Protocols/Icmp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,10 +180,10 @@ private static async Task<string> PingAsync(string hostname, string id, int time
(int)IPStatus.DestinationHostUnreachable or
(int)IPStatus.DestinationNetworkUnreachable => id + ((char)127).ToString() + "Unreachable",

(int)IPStatus.Success => id + ((char)127).ToString() + reply.RoundtripTime.ToString(),
(int)IPStatus.Success => id + ((char)127).ToString() + reply.RoundtripTime.ToString(),
(int)IPStatus.TimedOut => id + ((char)127).ToString() + "Timed out",
11050 => id + ((char)127).ToString() + "General failure",
_ => id + ((char)127).ToString() + reply.Status.ToString(),
11050 => id + ((char)127).ToString() + "General failure",
_ => id + ((char)127).ToString() + reply.Status.ToString(),
};
}
catch (ArgumentException) {
Expand Down Expand Up @@ -224,5 +224,4 @@ private static async Task<string> ArpPingAsync(string name, string id) {
}
}


}
40 changes: 31 additions & 9 deletions Protest/Tasks/LastSeen.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
using System.IO;
using System.Collections.Concurrent;
using System.Net.NetworkInformation;
using System.Threading;

namespace Protest.Tasks {
internal static class LastSeen {

private static ConcurrentDictionary<string, object> mutexes = new ConcurrentDictionary<string, object>();

public static void Seen(in string ip) {
string filename = $"{Data.DIR_LASTSEEN}\\{ip}.txt";
try {
string filename = $"{Data.DIR_LASTSEEN}\\{ip}.txt";
File.WriteAllText(filename, DateTime.Now.ToString(Data.DATETIME_FORMAT_LONG));
//File.WriteAllText(filename, DateTime.UtcNow.ToString());
if (!mutexes.TryGetValue(ip, out object mutex)) {
mutex = new object();
mutexes[ip] = mutex;
}

lock (mutex) {
File.WriteAllText(filename, DateTime.Now.ToString(Data.DATETIME_FORMAT_LONG));
//File.WriteAllText(filename, DateTime.UtcNow.ToString());
}
}
catch (Exception ex) {
Logger.Error(ex);
Expand All @@ -17,17 +29,18 @@ public static void Seen(in string ip) {
public static string HasBeenSeen(in string[] para, bool recordOnly = false) {
string ip = null;
for (int i = 1; i < para.Length; i++) {
if (para[i].StartsWith("ip="))
if (para[i].StartsWith("ip=")) {
ip = para[i][3..];
}
}

return HasBeenSeen(ip, recordOnly);
}

public static string HasBeenSeen(string ip, bool recordOnly = false) {
if (ip is null)
return null;
if (ip is null) return null;

if (!recordOnly)
if (!recordOnly) {
try {
using Ping p = new Ping();
if (p.Send(ip, 1000).Status == IPStatus.Success) {
Expand All @@ -36,12 +49,21 @@ public static string HasBeenSeen(string ip, bool recordOnly = false) {
}
}
catch { }
}

string filename = $"{Data.DIR_LASTSEEN}\\{ip}.txt";

try {
if (File.Exists(filename))
return File.ReadAllText(filename);
if (File.Exists(filename)) {
if (!mutexes.TryGetValue(ip, out object mutex)) {
mutex = new object();
mutexes[ip] = mutex;
}

lock (mutex) {
return File.ReadAllText(filename);
}
}
}
catch { }

Expand Down

0 comments on commit 8de072d

Please sign in to comment.