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\TntPhysics.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\LevelChunkStream.cs" />
<Compile Include="Network\Packets\Packet.BlockDefs.cs" />
@ -600,7 +600,6 @@
<Compile Include="Server\Backup.cs" />
<Compile Include="Server\BackupDB.cs" />
<Compile Include="Server\Extra\UPnP.cs" />
<Compile Include="Network\IBeat.cs" />
<Compile Include="Levels\Level.cs" />
<Compile Include="Server\Logger.cs" />
<Compile Include="Player\Player.cs" />

View File

@ -23,15 +23,14 @@ using Newtonsoft.Json;
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 proxyUrl;
public string URL { get { return url; } }
public override string URL { get { return url; } }
public bool Persistance { get { return true; } }
public void Init() {
public override void Init() {
try {
IPAddress[] addresses = Dns.GetHostAddresses("www.classicube.net");
EnsureIPv4Url(addresses);
@ -64,7 +63,7 @@ namespace MCGalaxy {
#endif
}
public string PrepareBeat() {
public override string GetHeartbeatData() {
string name = Server.name;
Server.zombie.OnHeartbeat(ref name);
Server.lava.OnHeartbeat(ref name);
@ -92,7 +91,7 @@ namespace MCGalaxy {
return count;
}
public void OnRequest(HttpWebRequest request) {
public override void OnRequest(HttpWebRequest request) {
#if !NET_20
request.Host = "www.classicube.net";
#else
@ -101,22 +100,23 @@ namespace MCGalaxy {
#endif
}
bool foundUrl = false;
public void OnResponse(string line) {
if (String.IsNullOrEmpty(line.Trim())) return;
string newHash = line.Substring(line.LastIndexOf('/') + 1);
public override void OnResponse(string response) {
if (String.IsNullOrEmpty(response.Trim())) return;
// 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
if (String.IsNullOrEmpty(Server.Hash) || !newHash.Equals(Server.Hash)) {
Server.Hash = newHash;
Server.URL = line;
if (String.IsNullOrEmpty(Server.Hash) || hash != Server.Hash) {
Server.Hash = hash;
Server.URL = response;
if (!Server.URL.Contains("\"errors\": [")) {
Server.s.UpdateUrl(Server.URL);
File.WriteAllText("text/externalurl.txt", Server.URL);
if (!foundUrl) {
Server.s.Log("ClassiCube URL found: " + Server.URL);
foundUrl = true;
}
Server.s.Log("ClassiCube URL found: " + Server.URL);
} else {
Response resp = JsonConvert.DeserializeObject<Response>(Server.URL);
if (resp.errors != null && resp.errors.Length > 0 && resp.errors[0].Length > 0)

View File

@ -14,7 +14,7 @@
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;
using System.IO;
using System.Net;
@ -24,48 +24,47 @@ using System.Threading;
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;
/// <summary> Gets or sets a value indicating whether this instance can beat. </summary>
/// <value> <c>true</c> if this instance can beat; otherwise, <c>false</c>. </value>
public static bool CanBeat { get; set; }
/// <summary> Gets the URL the heartbeat is sent to. </summary>
public abstract string URL { get; }
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[] {
new ClassiCubeBeat()
}; //Keep these in order
/// <summary> Initialises data for this heartbeat. </summary>
public abstract void Init();
static Heart() {
timer = new Timer(OnBeat, null, 30000, 30000);
}
/// <summary> Gets the data to be sent for a heartbeat. </summary>
public abstract string GetHeartbeatData();
static void OnBeat(object state) {
for (int i = 0; i < Beats.Length; i++) {
if (Beats[i].Persistance) Pump(Beats[i]);
}
}
/// <summary> Called when a request is about to be send to the web server. </summary>
public abstract void OnRequest(HttpWebRequest request);
/// <summary> Inits this instance. </summary>
public static void Init() {
/// <summary> Called when a response is received from the web server. </summary>
public abstract void OnResponse(string response);
/// <summary> Initialises all heartbeats. </summary>
public static void InitHeartbeats() {
if (Server.logbeat && !File.Exists("heartbeat.log")) {
using (File.Create("heartbeat.log")) { }
}
CanBeat = true;
canBeat = true;
for (int i = 0; i < Beats.Length; i++) {
Beats[i].Init();
Pump(Beats[i]);
}
}
/// <summary> Pumps the specified beat. </summary>
public static void Pump(IBeat beat) {
if (!CanBeat) return;
byte[] data = Encoding.ASCII.GetBytes(beat.PrepareBeat());
/// <summary> Pumps the specified heartbeat. </summary>
public static void Pump(Heartbeat beat) {
byte[] data = Encoding.ASCII.GetBytes(beat.GetHeartbeatData());
for (int i = 0; i < MAX_RETRIES; i++) {
try {
@ -74,9 +73,9 @@ namespace MCGalaxy {
req.ContentType = "application/x-www-form-urlencoded";
req.CachePolicy = new RequestCachePolicy(RequestCacheLevel.NoCacheNoStore);
req.Timeout = 15000;
req.ContentLength = data.Length;
beat.OnRequest(req);
req.ContentLength = data.Length;
using (Stream w = req.GetRequestStream()) {
w.Write(data, 0, data.Length);
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.");
}
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() {
try {
Heart.Init();
Heartbeat.InitHeartbeats();
} catch (Exception e) {
Server.ErrorLog(e);
}