public static int ExecuteViewAndReturnExitCode(ViewOptions options)
        {
            Console.WriteLine($"Attempting to View {options.FileName} From Online Repository");

            XmlSerializer toServerSerialize     = new XmlSerializer(typeof(ClientToServerDTO));
            XmlSerializer fromServerDeserialize = new XmlSerializer(typeof(ServerToClientDTO));

            ClientToServerDTO toServerDTO = new ClientToServerDTO();

            toServerDTO.Filename   = options.FileName;
            toServerDTO.Password   = options.Password;
            toServerDTO.UserAction = UserAction.view;
            toServerDTO.Expiration = "NULL";
            toServerDTO.Created    = "NULL";

            ServerToClientDTO fromServerDTO = new ServerToClientDTO();

            try
            {
                TcpClient clientConnection = new TcpClient("127.0.0.1", 1234);
                using (NetworkStream stream = clientConnection.GetStream())
                {
                    toServerSerialize.Serialize(stream, toServerDTO);
                    clientConnection.Client.Shutdown(SocketShutdown.Send);
                    fromServerDTO = (ServerToClientDTO)fromServerDeserialize.Deserialize(stream);
                };

                Console.WriteLine($"{fromServerDTO.Filename} Created {fromServerDTO.Created} Expires {fromServerDTO.Expiration}");
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            return(0);
        }
Пример #2
0
        public static int ExecuteDownloadAndReturnExitCode(DownloadOptions options)
        {
            Console.WriteLine($"Attempting to Fetch {options.FileName} From the Online Repository");

            XmlSerializer Serializer   = new XmlSerializer(typeof(ClientToServerDTO));
            XmlSerializer Deserializer = new XmlSerializer(typeof(ServerToClientDTO));

            ClientToServerDTO toServerDTO = new ClientToServerDTO();

            toServerDTO.Filename   = options.FileName;
            toServerDTO.Password   = options.Password;
            toServerDTO.UserAction = UserAction.download;
            toServerDTO.Created    = "NULL";
            toServerDTO.Expiration = "NULL";

            ServerToClientDTO fromServerDTO = new ServerToClientDTO();

            try
            {
                TcpClient clientConnection = new TcpClient("127.0.0.1", 1234);
                using (NetworkStream stream = clientConnection.GetStream())
                {
                    Serializer.Serialize(stream, toServerDTO);
                    clientConnection.Client.Shutdown(SocketShutdown.Send);
                    fromServerDTO = (ServerToClientDTO)Deserializer.Deserialize(stream);
                };

                FileInfo file = new FileInfo(options.FileName);
                using (FileStream stream = new FileStream(file.FullName, FileMode.OpenOrCreate, FileAccess.ReadWrite))
                {
                    stream.Write(fromServerDTO.FileData, 0, fromServerDTO.FileData.Length);
                };
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }

            /*  try
             * {
             *    XmlSerializer Deserializer = new XmlSerializer(typeof(Client));
             *    XmlSerializer Serializer = new XmlSerializer(typeof(Files));
             *
             *
             *
             *    TcpClient clientConnection = new TcpClient("127.0.0.1", 1234);
             *    using (NetworkStream stream = clientConnection.GetStream())
             *    {
             *        Files DTO = new Files();
             *
             *        DTO.FileName = options.FileName;
             *        DTO.Password = options.Password;
             *
             *        Serializer.Serialize(stream, DTO);
             *    }
             *
             * }
             * catch (Exception e)
             * {
             *    Console.WriteLine(e.Message);
             * }*/
            return(0);
        }
Пример #3
0
        static void Main(string[] args)
        {
            XmlSerializer fromClientDeserialize = new XmlSerializer(typeof(ClientToServerDTO));

            ClientToServerDTO fromClientDTO = new ClientToServerDTO();

            IPEndPoint ingress = new IPEndPoint(0, 1234);

            SmartShareContext DBContext = new SmartShareContext();

            TcpListener mainSocket = new TcpListener(ingress);

            mainSocket.Server.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
            mainSocket.Start();

            while (true)
            {
                try
                {
                    TcpClient clientConnection = mainSocket.AcceptTcpClient();
                    Console.WriteLine("Connection Received");

                    Task.Run(() =>
                    {
                        SmartShareContext dbContext = new SmartShareContext();
                        DbSet <SmartShareFile> smartShareFileTable = dbContext.SmartShareFileTable;
                        SmartShareFile newDataEntry = new SmartShareFile();

                        using (NetworkStream stream = clientConnection.GetStream())
                        {
                            fromClientDTO = (ClientToServerDTO)fromClientDeserialize.Deserialize(stream);

                            bool exists = (from i in smartShareFileTable where i.Filename == fromClientDTO.Filename select i).Any();

                            if (fromClientDTO.UserAction == UserAction.upload)
                            {
                                if (!exists)
                                {
                                    newDataEntry.Filename         = fromClientDTO.Filename;
                                    newDataEntry.Password         = fromClientDTO.Password;
                                    newDataEntry.MaximumDownloads = fromClientDTO.MaxDown;
                                    newDataEntry.Expiration       = fromClientDTO.Expiration;
                                    newDataEntry.DownloadCount    = 0;
                                    newDataEntry.FileData         = fromClientDTO.FileData;

                                    dbContext.Add(newDataEntry);
                                    dbContext.SaveChanges();
                                }
                            }

                            if (fromClientDTO.UserAction == UserAction.download || fromClientDTO.UserAction == UserAction.view)
                            {
                                XmlSerializer toClientSerialize = new XmlSerializer(typeof(ServerToClientDTO));
                                ServerToClientDTO toClientDTO   = new ServerToClientDTO();


                                if (exists)
                                {
                                    SmartShareFile fetchedDownload = (from i in smartShareFileTable
                                                                      where i.Filename == fromClientDTO.Filename
                                                                      select i).First();

                                    if (fetchedDownload.Password == fromClientDTO.Password && DateTime.Parse(fetchedDownload.Expiration) > DateTime.Now && fetchedDownload.DownloadCount < fetchedDownload.MaximumDownloads)
                                    {
                                        toClientDTO.Filename   = fetchedDownload.Filename;
                                        toClientDTO.MaxDown    = fetchedDownload.MaximumDownloads;
                                        toClientDTO.DownCount  = ++fetchedDownload.DownloadCount;
                                        toClientDTO.Expiration = fetchedDownload.Expiration;
                                        toClientDTO.FileData   = fetchedDownload.FileData;
                                        toClientDTO.Created    = fetchedDownload.Created;

                                        if (fromClientDTO.UserAction != UserAction.view)
                                        {
                                            dbContext.Update(fetchedDownload);
                                            dbContext.SaveChanges();
                                        }

                                        --toClientDTO.DownCount;

                                        toClientSerialize.Serialize(stream, toClientDTO);
                                    }
                                    else if (DateTime.Parse(fetchedDownload.Expiration) <= DateTime.Now || fetchedDownload.MaximumDownloads <= fetchedDownload.DownloadCount)
                                    {
                                        dbContext.SmartShareFileTable.Remove(fetchedDownload);
                                        dbContext.SaveChanges();
                                    }
                                }
                            }
                        }

                        dbContext.Dispose();
                    });
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
            }
        }