public static void RunAsMaster(RequestWithHeader <ServerAvailableHeader> sah) { if (sah.header.UniqueToken != "") { StorageProvider.GetInstance().UpdateSlave(sah.header.UniqueToken, sah.header.Available); } }
/// <summary> /// Runs the logic for when Server Hello Header was recieved /// </summary> /// <param name="ch"><see cref="ServerHello"/> containing all the necessary information</param> public static void Run(RequestWithHeader <ServerHelloHeader> ch) { if (!ch.header.AcceptedRequest) { // there is one pending, check status Log.Warning(ch.header.Message); StorageProvider.GetInstance().DeleteLatestClientOrder(); return; } if (ch.header.Message.Contains("Token:")) { // the server generated a new token for the client // store it in the database StorageProvider.GetInstance().RegisterClientToken("localhost", ch.header.Message.Split(':')[1]); } Log.Information("Recieved sha {0}!", ch.header.Sha); // if the order request was accepted // send the zip string path = ConfigReader.GetInstance().Config.SavePath + "\\zipToSend\\temp.zip"; Sender.SendZip(path, ch.Client.GetStream()); // Done, now register the sha to the local database for future operations StorageProvider.GetInstance().UpdateSha(ch.header.Sha); }
public static void Run(RequestWithHeader <JobHeader> jr) { // After the JobHeader the master will send the job OrderReciever.RecieveJob(jr); // Register job StorageProvider.GetInstance().MasterRegisteredJob(jr); }
public static void Run(RequestWithHeader <ServerAvailableHeader> sah) { if (ConfigReader.GetInstance().Config.Type == 0) { } else { RunAsSlave(sah); } }
public static void Run(RequestWithHeader <FinishedOrderHeader> foh) { Log.Warning(foh.header.Message); if (foh.header.Size == 0) { Log.Warning(foh.header.Message); return; } OrderReciever.RecieveFinishedOrder(foh); }
public static void RunAsSlave(RequestWithHeader <ServerAvailableHeader> sah) { ServerAvailableHeader sahresp = new ServerAvailableHeader() { Available = StorageProvider.GetInstance().NumberOfJobsToBeDone() == 0 ? true : false, UniqueToken = StorageProvider.GetInstance().GetTokenAsSlave() }; WraperHeader wraper = new WraperHeader() { Type = HeaderTypes.ServerAvailableHeader, Data = ByteArrayUtils.ToByteArray(sahresp) }; Sender.Send(wraper, sah.Client); }
/// <summary> /// Runs the logic for Client Hello /// </summary> /// <param name="ch"><see cref="ClientHello"/></param> public static void Run(RequestWithHeader <ClientHelloHeader> ch) { #if DEBUG Tuple <string, bool> auth = new Tuple <string, bool>("", true); #else Tuple <string, bool> auth = Authentification.AuthenticateClient(ch.header.ClientIP, ch.header.ClientToken); #endif string sha = ""; bool aproval = false; if (auth.Item2 == true) { aproval = DecideAproval(ch.header); if (aproval == false) { Log.Warning("Order was not approved, fetching already existing sha!"); sha = GetExistingSha(ch.header); auth = new Tuple <string, bool>("Error: Existing order with sha:" + sha, false); } else { sha = CreateSha(ch.header); Log.Information("Order was approved, new sha :{0}", sha); } } if (auth.Item1.Contains("Token:")) { StorageProvider.GetInstance().RegisterClientToken(ch.header.ClientIP, auth.Item1.Split(':')[1]); } // Wrap the ServerHello Header WraperHeader wraper = new WraperHeader { Type = HeaderTypes.ServerHelloHeader, Data = ByteArrayUtils.ToByteArray(CreateServerHelloHeader(aproval, sha, auth.Item1)) }; Sender.Send(wraper, ch.Client); // Wait for the orderpost if (aproval == true) { OrderReciever.ReicieveClientHello(ch, sha); } // If aproval is false, the connection will close }
public static void Run(RequestWithHeader <RequestFinishedOrderHeader> rfo) { #if DEBUG Tuple <string, bool> auth = new Tuple <string, bool>("", true); #else Tuple <string, bool> auth = Authentification.AuthenticateClient(rfo.header.Ip, rfo.header.ClientToken); #endif FinishedOrderHeader foh = new FinishedOrderHeader(); bool sendData = auth.Item2 == true && !StorageProvider.GetInstance().ClientHasOrder(rfo.header.Ip); string orderPath = Path.Combine(ConfigReader.GetInstance().Config.SavePath, rfo.header.Sha); string resultPath = Path.Combine(orderPath, "result"); string zipPath = Path.Combine(orderPath, "result.zip"); if (sendData) { if (!File.Exists(zipPath)) { ZipFile.CreateFromDirectory(resultPath, zipPath); } long size = new FileInfo(zipPath).Length; foh.Size = size; foh.Message = rfo.header.Sha; } else { foh.Size = 0; foh.Message = auth.Item1 == "" ? "You already have an order in progress" : auth.Item1; } WraperHeader wh = new WraperHeader() { Data = ByteArrayUtils.ToByteArray(foh), Type = HeaderTypes.FinishedOrderHeader }; Sender.Send(wh, rfo.Client); if (sendData) { Sender.SendZip(zipPath, rfo.Client.GetStream()); } }
public void MasterRegisteredJob(RequestWithHeader <JobHeader> jr) { lock (connection) { using (var transaction = connection.BeginTransaction()) { var command = connection.CreateCommand(); command.CommandText = @"INSERT INTO orders (ip, time, size, sha, version, status, created_at) VALUES($ip, $time, $size, $sha, $version, $status, $created_at);"; command.Parameters.AddWithValue("$ip", ConfigReader.GetInstance().Config.Ip); command.Parameters.AddWithValue("$time", DateTimeOffset.UtcNow.ToUnixTimeSeconds()); command.Parameters.AddWithValue("$size", jr.header.Size); command.Parameters.AddWithValue("$sha", jr.header.JobID); command.Parameters.AddWithValue("$version", "TODO"); command.Parameters.AddWithValue("$status", "just started"); command.Parameters.AddWithValue("$created_at", DateTimeOffset.UtcNow.ToUnixTimeSeconds()); command.ExecuteNonQuery(); transaction.Commit(); } } }
public void ClientRegisteredOrder(RequestWithHeader <ClientHelloHeader> ch, string sha) { lock (connection) { using (var transaction = connection.BeginTransaction()) { var command = connection.CreateCommand(); command.CommandText = @"INSERT INTO orders (ip, time, size, sha, version, status, created_at) VALUES($ip, $time, $size, $sha, $version, $status, $created_at);"; command.Parameters.AddWithValue("$ip", ch.header.ClientIP); command.Parameters.AddWithValue("$time", new DateTimeOffset(ch.header.ClientUTCTime).ToUnixTimeSeconds()); command.Parameters.AddWithValue("$size", ch.header.SizeInBytes); command.Parameters.AddWithValue("$sha", sha); command.Parameters.AddWithValue("$version", ch.header.ClientVersion); command.Parameters.AddWithValue("$status", "just started"); command.Parameters.AddWithValue("$created_at", DateTimeOffset.UtcNow.ToUnixTimeSeconds()); command.ExecuteNonQuery(); transaction.Commit(); } } }
public static void Run(RequestWithHeader <RegisterHeader> rh) { string token = ""; if (StorageProvider.GetInstance().SlaveExists(rh.header.Ip) == "") { token = Guid.NewGuid().ToString(); Slave slave = new Slave(rh.header.Ip, rh.header.Port, 0, token); StorageProvider.GetInstance().RegisterSlave(slave); } RegisterResponseHeader rrh = new RegisterResponseHeader { Token = token }; WraperHeader wh = new WraperHeader() { Data = ByteArrayUtils.ToByteArray(rrh), Type = HeaderTypes.RegisterResponseHeader }; Sender.Send(wh, rh.Client); }
public static void Run(RequestWithHeader <RegisterResponseHeader> rrh) { Slave slave = new Slave("", 0, 1, rrh.header.Token); StorageProvider.GetInstance().RegisterSlave(slave); }
public static void Run(RequestWithHeader <JobResultHeader> jr) { OrderReciever.RecieveFinishedJob(jr); }
public static void ReicieveClientHello(RequestWithHeader <ClientHelloHeader> ch, string sha) { Recieve(ch.header.SizeInBytes, ch.Client.GetStream(), sha); // Register order to database StorageProvider.GetInstance().ClientRegisteredOrder(ch, sha); }
public static void RecieveJob(RequestWithHeader <JobHeader> jr) { Recieve(jr.header.Size, jr.Client.GetStream(), jr.header.JobID); }
public static void RecieveFinishedJob(RequestWithHeader <JobResultHeader> jrh) { Recieve(jrh.header.Size, jrh.Client.GetStream(), Path.Combine(Scheduler.GetOrderPath, "result", jrh.header.JobID)); }
public static void RecieveFinishedOrder(RequestWithHeader <FinishedOrderHeader> foh) { Recieve(foh.header.Size, foh.Client.GetStream(), Path.Combine(ConfigReader.GetInstance().Config.SavePath, foh.header.Message)); }