Fix heartbeat url not showing up changed in gui when IP changes

This commit is contained in:
UnknownShadow200 2017-04-27 22:47:21 +10:00
parent 10fb3c4657
commit 8d31a01d7c
5 changed files with 68 additions and 95 deletions

View File

@ -550,7 +550,7 @@
<Compile Include="Blocks\Physics\TrainPhysics.cs" /> <Compile Include="Blocks\Physics\TrainPhysics.cs" />
<Compile Include="Blocks\Physics\TntPhysics.cs" /> <Compile Include="Blocks\Physics\TntPhysics.cs" />
<Compile Include="Blocks\Physics\ZombiePhysics.cs" /> <Compile Include="Blocks\Physics\ZombiePhysics.cs" />
<Compile Include="Network\Heart.cs" /> <Compile Include="Network\Heartbeat.cs" />
<Compile Include="Network\IRCBot.cs" /> <Compile Include="Network\IRCBot.cs" />
<Compile Include="Network\LevelChunkStream.cs" /> <Compile Include="Network\LevelChunkStream.cs" />
<Compile Include="Network\Packets\Packet.BlockDefs.cs" /> <Compile Include="Network\Packets\Packet.BlockDefs.cs" />
@ -600,7 +600,6 @@
<Compile Include="Server\Backup.cs" /> <Compile Include="Server\Backup.cs" />
<Compile Include="Server\BackupDB.cs" /> <Compile Include="Server\BackupDB.cs" />
<Compile Include="Server\Extra\UPnP.cs" /> <Compile Include="Server\Extra\UPnP.cs" />
<Compile Include="Network\IBeat.cs" />
<Compile Include="Levels\Level.cs" /> <Compile Include="Levels\Level.cs" />
<Compile Include="Server\Logger.cs" /> <Compile Include="Server\Logger.cs" />
<Compile Include="Player\Player.cs" /> <Compile Include="Player\Player.cs" />

View File

@ -23,15 +23,14 @@ using Newtonsoft.Json;
namespace MCGalaxy { namespace MCGalaxy {
public sealed class ClassiCubeBeat : IBeat { /// <summary> Heartbeat to ClassiCube.net's web server. </summary>
public sealed class ClassiCubeBeat : Heartbeat {
string url = "http://www.classicube.net/heartbeat.jsp"; string url = "http://www.classicube.net/heartbeat.jsp";
string proxyUrl; string proxyUrl;
public string URL { get { return url; } } public override string URL { get { return url; } }
public bool Persistance { get { return true; } } public override void Init() {
public void Init() {
try { try {
IPAddress[] addresses = Dns.GetHostAddresses("www.classicube.net"); IPAddress[] addresses = Dns.GetHostAddresses("www.classicube.net");
EnsureIPv4Url(addresses); EnsureIPv4Url(addresses);
@ -64,7 +63,7 @@ namespace MCGalaxy {
#endif #endif
} }
public string PrepareBeat() { public override string GetHeartbeatData() {
string name = Server.name; string name = Server.name;
Server.zombie.OnHeartbeat(ref name); Server.zombie.OnHeartbeat(ref name);
Server.lava.OnHeartbeat(ref name); Server.lava.OnHeartbeat(ref name);
@ -92,7 +91,7 @@ namespace MCGalaxy {
return count; return count;
} }
public void OnRequest(HttpWebRequest request) { public override void OnRequest(HttpWebRequest request) {
#if !NET_20 #if !NET_20
request.Host = "www.classicube.net"; request.Host = "www.classicube.net";
#else #else
@ -101,22 +100,23 @@ namespace MCGalaxy {
#endif #endif
} }
bool foundUrl = false; public override void OnResponse(string response) {
public void OnResponse(string line) { if (String.IsNullOrEmpty(response.Trim())) return;
if (String.IsNullOrEmpty(line.Trim())) return;
string newHash = line.Substring(line.LastIndexOf('/') + 1); // in form of http://www.classicube.net/server/play/<hash>/
if (response.EndsWith("/"))
response = response.Substring(0, response.Length - 1);
string hash = response.Substring(response.LastIndexOf('/') + 1);
// Run this code if we don't already have a hash or if the hash has changed // Run this code if we don't already have a hash or if the hash has changed
if (String.IsNullOrEmpty(Server.Hash) || !newHash.Equals(Server.Hash)) { if (String.IsNullOrEmpty(Server.Hash) || hash != Server.Hash) {
Server.Hash = newHash; Server.Hash = hash;
Server.URL = line; Server.URL = response;
if (!Server.URL.Contains("\"errors\": [")) { if (!Server.URL.Contains("\"errors\": [")) {
Server.s.UpdateUrl(Server.URL); Server.s.UpdateUrl(Server.URL);
File.WriteAllText("text/externalurl.txt", Server.URL); File.WriteAllText("text/externalurl.txt", Server.URL);
if (!foundUrl) {
Server.s.Log("ClassiCube URL found: " + Server.URL); Server.s.Log("ClassiCube URL found: " + Server.URL);
foundUrl = true;
}
} else { } else {
Response resp = JsonConvert.DeserializeObject<Response>(Server.URL); Response resp = JsonConvert.DeserializeObject<Response>(Server.URL);
if (resp.errors != null && resp.errors.Length > 0 && resp.errors[0].Length > 0) if (resp.errors != null && resp.errors.Length > 0 && resp.errors[0].Length > 0)

View File

@ -24,48 +24,47 @@ using System.Threading;
namespace MCGalaxy { namespace MCGalaxy {
public static class Heart { /// <summary> Sends a heartbeat (optionally repeatedly every certain interval) to a web server. </summary>
public abstract class Heartbeat {
/// <summary> The max number of retries it runs for a beat </summary> /// <summary> The max number of retries attempted for a heartbeat. </summary>
public const int MAX_RETRIES = 3; public const int MAX_RETRIES = 3;
/// <summary> Gets or sets a value indicating whether this instance can beat. </summary> /// <summary> Gets the URL the heartbeat is sent to. </summary>
/// <value> <c>true</c> if this instance can beat; otherwise, <c>false</c>. </value> public abstract string URL { get; }
public static bool CanBeat { get; set; }
static Timer timer; /// <summary> Gets whether this heartbeat periodically repeats beats. </summary>
public virtual bool Persistent { get { return true; } }
readonly static IBeat[] Beats = new IBeat[] { /// <summary> Initialises data for this heartbeat. </summary>
new ClassiCubeBeat() public abstract void Init();
}; //Keep these in order
static Heart() { /// <summary> Gets the data to be sent for a heartbeat. </summary>
timer = new Timer(OnBeat, null, 30000, 30000); public abstract string GetHeartbeatData();
}
static void OnBeat(object state) { /// <summary> Called when a request is about to be send to the web server. </summary>
for (int i = 0; i < Beats.Length; i++) { public abstract void OnRequest(HttpWebRequest request);
if (Beats[i].Persistance) Pump(Beats[i]);
}
}
/// <summary> Inits this instance. </summary> /// <summary> Called when a response is received from the web server. </summary>
public static void Init() { public abstract void OnResponse(string response);
/// <summary> Initialises all heartbeats. </summary>
public static void InitHeartbeats() {
if (Server.logbeat && !File.Exists("heartbeat.log")) { if (Server.logbeat && !File.Exists("heartbeat.log")) {
using (File.Create("heartbeat.log")) { } using (File.Create("heartbeat.log")) { }
} }
CanBeat = true; canBeat = true;
for (int i = 0; i < Beats.Length; i++) { for (int i = 0; i < Beats.Length; i++) {
Beats[i].Init(); Beats[i].Init();
Pump(Beats[i]); Pump(Beats[i]);
} }
} }
/// <summary> Pumps the specified beat. </summary> /// <summary> Pumps the specified heartbeat. </summary>
public static void Pump(IBeat beat) { public static void Pump(Heartbeat beat) {
if (!CanBeat) return; byte[] data = Encoding.ASCII.GetBytes(beat.GetHeartbeatData());
byte[] data = Encoding.ASCII.GetBytes(beat.PrepareBeat());
for (int i = 0; i < MAX_RETRIES; i++) { for (int i = 0; i < MAX_RETRIES; i++) {
try { try {
@ -74,9 +73,9 @@ namespace MCGalaxy {
req.ContentType = "application/x-www-form-urlencoded"; req.ContentType = "application/x-www-form-urlencoded";
req.CachePolicy = new RequestCachePolicy(RequestCacheLevel.NoCacheNoStore); req.CachePolicy = new RequestCachePolicy(RequestCacheLevel.NoCacheNoStore);
req.Timeout = 15000; req.Timeout = 15000;
req.ContentLength = data.Length;
beat.OnRequest(req); beat.OnRequest(req);
req.ContentLength = data.Length;
using (Stream w = req.GetRequestStream()) { using (Stream w = req.GetRequestStream()) {
w.Write(data, 0, data.Length); w.Write(data, 0, data.Length);
if (Server.logbeat) Server.s.Log("Beat " + beat + " was sent"); if (Server.logbeat) Server.s.Log("Beat " + beat + " was sent");
@ -96,5 +95,21 @@ namespace MCGalaxy {
if (Server.logbeat) Server.s.Log("Beat: " + beat + " failed."); if (Server.logbeat) Server.s.Log("Beat: " + beat + " failed.");
} }
static Timer timer;
static bool canBeat = false;
readonly static Heartbeat[] Beats = new Heartbeat[] { new ClassiCubeBeat() };
static Heartbeat() {
timer = new Timer(OnBeat, null, 30000, 30000);
}
static void OnBeat(object state) {
if (!canBeat) return;
for (int i = 0; i < Beats.Length; i++) {
if (Beats[i].Persistent) Pump(Beats[i]);
}
}
} }
} }

View File

@ -1,41 +0,0 @@
/*
Copyright 2012 MCForge
Dual-licensed under the Educational Community License, Version 2.0 and
the GNU General Public License, Version 3 (the "Licenses"); you may
not use this file except in compliance with the Licenses. You may
obtain a copy of the Licenses at
http://www.opensource.org/licenses/ecl2.php
http://www.gnu.org/licenses/gpl-3.0.html
Unless required by applicable law or agreed to in writing,
software distributed under the Licenses are distributed on an "AS IS"
BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
or implied. See the Licenses for the specific language governing
permissions and limitations under the Licenses.
*/
using System.Net;
namespace MCGalaxy {
public interface IBeat {
/// <summary> Gets the URL. </summary>
string URL { get; }
/// <summary> Gets whether this IBeat has periodically repeating beats. </summary>
bool Persistance { get; }
/// <summary> Initialises persistent data for this beat instance. </summary>
void Init();
/// <summary> Prepares the data for the next beat of this this instance. </summary>
string PrepareBeat();
/// <summary> Called when a response is recieved. </summary>
void OnRequest(HttpWebRequest request);
/// <summary> Called when a response is recieved. </summary>
void OnResponse(string resonse);
}
}

View File

@ -95,7 +95,7 @@ namespace MCGalaxy {
void InitHeartbeat() { void InitHeartbeat() {
try { try {
Heart.Init(); Heartbeat.InitHeartbeats();
} catch (Exception e) { } catch (Exception e) {
Server.ErrorLog(e); Server.ErrorLog(e);
} }