|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceProcess;
using System.Net.Sockets;
using System.Threading;
namespace Versioner
{
public class Service : ServiceBase
{
public static readonly string Name = "Versioner";
public Service()
{
this.ServiceName = Name;
}
SimplyListen listener;
protected override void OnStart(string[] args)
{
listener = SimplyListen.Create(2313, s =>
{
try
{
string name, version;
if (SimplyListen.ReadHttpRequestBuffer(s, out name, out version))
{
var response = UpdateVersions(name, Increment(name, version));
// Convert to byte array and send.
var byteDateLine = System.Text.Encoding.ASCII.GetBytes(response);
s.Send(byteDateLine, byteDateLine.Length, 0);
}
}
catch (Exception x)
{
System.Diagnostics.EventLog.WriteEntry("Versioner", x.Message);
}
finally
{
s.Close();
}
});
base.OnStart(args);
}
protected override void OnStop()
{
listener.Dispose();
base.OnStop();
}
static readonly string _versionFilename = @"\Versioner\versions.txt";
static Versioner vers = Versioner.Create(_versionFilename);
private static string UpdateVersions(string name, string response)
{
if (!string.IsNullOrEmpty(response))
{
if (vers.Versions.ContainsKey(name))
vers.Versions[name] = response;
else
vers.Versions.Add(name, response);
vers.Save(_versionFilename);
}
else if (!vers.Versions.TryGetValue(name, out response))
{
response = string.Empty;
}
return response;
}
private static string Increment(string name, string version)
{
string response = version;
if (!string.IsNullOrEmpty(version))
{
int plusCount = version.Count(c => '+' == c);
if (plusCount > 0)
{
if (!vers.Versions.TryGetValue(name, out response))
response = "1.0.0.0";
response = Versioner.Increment(response, plusCount);
}
}
return response;
}
public void Run(string[] args)
{
OnStart(args);
while(true)
Thread.Sleep(100000);
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
My professional career began as a developer fixing bugs on Microsoft Word97 and I've been fixing bad habits ever since. Now I do R&D work writing v1 line of business applications mostly in C#/.Net.
I've been an avid pilot/instructor for 13+ years, I've built two airplanes and mostly fly gliders now for fun. I commute in an all-electric 1986 BMW 325 conversion.
I'd like to get back to my academic roots of programming 3D analysis applications to organize complex systems.