Ejemplo n.º 1
0
 public static void RunAsMaster(RequestWithHeader <ServerAvailableHeader> sah)
 {
     if (sah.header.UniqueToken != "")
     {
         StorageProvider.GetInstance().UpdateSlave(sah.header.UniqueToken, sah.header.Available);
     }
 }
Ejemplo n.º 2
0
        /// <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);
        }
Ejemplo n.º 3
0
        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);
        }
Ejemplo n.º 4
0
 public static void Run(RequestWithHeader <ServerAvailableHeader> sah)
 {
     if (ConfigReader.GetInstance().Config.Type == 0)
     {
     }
     else
     {
         RunAsSlave(sah);
     }
 }
Ejemplo n.º 5
0
        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);
        }
Ejemplo n.º 6
0
        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);
        }
Ejemplo n.º 7
0
        /// <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
        }
Ejemplo n.º 8
0
        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());
            }
        }
Ejemplo n.º 9
0
 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();
         }
     }
 }
Ejemplo n.º 10
0
 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();
         }
     }
 }
Ejemplo n.º 11
0
        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);
        }
Ejemplo n.º 12
0
        public static void Run(RequestWithHeader <RegisterResponseHeader> rrh)
        {
            Slave slave = new Slave("", 0, 1, rrh.header.Token);

            StorageProvider.GetInstance().RegisterSlave(slave);
        }
Ejemplo n.º 13
0
 public static void Run(RequestWithHeader <JobResultHeader> jr)
 {
     OrderReciever.RecieveFinishedJob(jr);
 }
Ejemplo n.º 14
0
 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);
 }
Ejemplo n.º 15
0
 public static void RecieveJob(RequestWithHeader <JobHeader> jr)
 {
     Recieve(jr.header.Size, jr.Client.GetStream(), jr.header.JobID);
 }
Ejemplo n.º 16
0
 public static void RecieveFinishedJob(RequestWithHeader <JobResultHeader> jrh)
 {
     Recieve(jrh.header.Size, jrh.Client.GetStream(), Path.Combine(Scheduler.GetOrderPath, "result", jrh.header.JobID));
 }
Ejemplo n.º 17
0
 public static void RecieveFinishedOrder(RequestWithHeader <FinishedOrderHeader> foh)
 {
     Recieve(foh.header.Size, foh.Client.GetStream(), Path.Combine(ConfigReader.GetInstance().Config.SavePath, foh.header.Message));
 }