Exemplo n.º 1
0
 public static void Send(WraperHeader header, TcpClient client)
 {
     byte[] byteArray = ByteArrayUtils.ToByteArray(header);
     Log.Information("Sending {0} bytes!", byteArray.Length);
     client.GetStream().Write(BitConverter.GetBytes(byteArray.Length), 0, sizeof(int));
     client.GetStream().Write(byteArray, 0, byteArray.Length);
 }
Exemplo n.º 2
0
        public static void RegisterToMaster()
        {
            if (StorageProvider.GetInstance().GetTokenAsSlave() != "")
            {
                return;
            }

            RegisterHeader rh = new RegisterHeader
            {
                Ip   = ConfigReader.GetInstance().Config.PredefinedIP,
                Port = ConfigReader.GetInstance().Config.Port
            };

            WraperHeader wraperHeader = new WraperHeader
            {
                Data = ByteArrayUtils.ToByteArray(rh),
                Type = HeaderTypes.RegisterHeader
            };

            TcpClient client = new TcpClient(ConfigReader.GetInstance().Config.Ip, ConfigReader.GetInstance().Config.MastersPort);

            Sender.Send(wraperHeader, client);

            new Reciever().ProcessClient(client);
        }
Exemplo n.º 3
0
        /// <summary>
        /// "Factory" method that creates the right request based on the <see cref="WraperHeader.type"/>
        /// </summary>
        /// <param name="wh"><see cref="WraperHeader"/> header with all the information</param>
        /// <returns><see cref="Request"/> with all the information inside</returns>
        internal static Request Factory(WraperHeader wh)
        {
            switch (wh.Type)
            {
            case HeaderTypes.ClientHelloHeader:
                return(new RequestWithHeader <ClientHelloHeader>(wh.Data));

            case HeaderTypes.ServerHelloHeader:
                return(new RequestWithHeader <ServerHelloHeader>(wh.Data));

            case HeaderTypes.JobHeader:
                return(new RequestWithHeader <JobHeader>(wh.Data));

            case HeaderTypes.ServerAvailableHeader:
                return(new RequestWithHeader <ServerAvailableHeader>(wh.Data));

            case HeaderTypes.JobResultHeader:
                return(new RequestWithHeader <JobResultHeader>(wh.Data));

            case HeaderTypes.RegisterHeader:
                return(new RequestWithHeader <RegisterHeader>(wh.Data));

            case HeaderTypes.RegisterResponseHeader:
                return(new RequestWithHeader <RegisterResponseHeader>(wh.Data));

            case HeaderTypes.RequestFinishedOrderHeader:
                return(new RequestWithHeader <RequestFinishedOrderHeader>(wh.Data));

            case HeaderTypes.FinishedOrderHeader:
                return(new RequestWithHeader <FinishedOrderHeader>(wh.Data));

            default:
                return(new Request(wh.Data));
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Continuation of <see cref="ProcessClient"/>
        /// </summary>
        /// <param name="data">List of bytes read from the connection</param>
        /// <param name="client">the client that it was read from</param>
        public void ProcessData(List <byte> data, TcpClient client)
        {
            WraperHeader wh = ByteArrayUtils.FromByteArray <WraperHeader>(data.ToArray());

            if (!CheckRequest(wh))
            {
                return;
            }
            Request req = RequestFactory.Factory(wh);

            req.Client = client;
            Runner.Start(req);
        }
Exemplo n.º 5
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);
        }
Exemplo n.º 6
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
        }
        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());
            }
        }
Exemplo n.º 8
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);
        }
Exemplo n.º 9
0
        public static void RequestResultFromMaster(string sha)
        {
            RequestFinishedOrderHeader rfo = new RequestFinishedOrderHeader()
            {
#if DEBUG
                ClientToken = "",
#else
                ClientToken = StorageProvider.GetInstance().GetClientsToken(),
#endif
                Ip  = ConfigReader.GetInstance().Config.PredefinedIP == "" ? Networking.GetIPAddress() : ConfigReader.GetInstance().Config.PredefinedIP,
                Sha = sha
            };

            WraperHeader wh = new WraperHeader()
            {
                Data = ByteArrayUtils.ToByteArray(rfo),
                Type = HeaderTypes.RequestFinishedOrderHeader
            };
            TcpClient client = new TcpClient(ConfigReader.GetInstance().Config.Ip, ConfigReader.GetInstance().Config.Port);

            Sender.Send(wh, client);

            new Reciever().ProcessClient(client);
        }
Exemplo n.º 10
0
 public bool CheckRequest(WraperHeader wh)
 {
     return(AllowedRequests.GetType(ConfigReader.GetInstance().Config.Type).Contains(wh.Type));
 }