Advertisement
Guest User

Untitled

a guest
Nov 30th, 2015
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.38 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.Specialized;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Runtime.Serialization.Formatters.Binary;
  7. using System.Security.Cryptography;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10.  
  11. namespace DataStructures
  12. {
  13.     public enum MSG_HEADER
  14.     {
  15.         LOGIN_REQUEST, //send usr and password
  16.         LOGIN_ACK, //receive if login succeded or not
  17.         FS_STATUS_GET, //send a list of FileStatus corresponding to all files in client fileSystem
  18.         FS_STATUS_ACK, //receive a list of FileStatus corresponding to all files stored on server
  19.         PUSH_FILE,
  20.         PUSH_FILE_ACK,
  21.         RESTORE_FILE,
  22.         RESTORE_FILE_ACK
  23.     }
  24.  
  25.     [Serializable]
  26.     public class CatchallMessageModel
  27.     {
  28.         private MSG_HEADER _header;
  29.         private object _content;
  30.  
  31.         public MSG_HEADER header { get { return _header; } }
  32.         public object content { get { return _content; } }
  33.  
  34.         public CatchallMessageModel(MSG_HEADER header, object content)
  35.         {
  36.             _header = header;
  37.             _content = content;
  38.         }
  39.  
  40.         public static byte[] serialize(CatchallMessageModel message)
  41.         {
  42.             byte[] result;
  43.  
  44.             using (MemoryStream memStream = new MemoryStream())
  45.             {
  46.                 BinaryFormatter serializer = new BinaryFormatter();
  47.                 serializer.Serialize(memStream, message);
  48.                 result = memStream.GetBuffer();
  49.             }
  50.             return result;
  51.         }
  52.  
  53.         public static CatchallMessageModel deserialize(byte[] buffer)
  54.         {
  55.  
  56.             using (MemoryStream memStream = new MemoryStream(buffer))
  57.             {
  58.                 BinaryFormatter deserializer = new BinaryFormatter();
  59.                 object newobj = deserializer.Deserialize(memStream);
  60.                 return newobj as CatchallMessageModel;
  61.             }
  62.         }
  63.  
  64.  
  65.     }
  66.  
  67.     [Serializable]
  68.     public class LoginRequest
  69.     {
  70.         private string _username;
  71.         private string _password;
  72.  
  73.         public string username { get { return _username; } }
  74.         public string password { get { return _password; } }
  75.  
  76.         public LoginRequest(string u, string p)
  77.         {
  78.             _username = u;
  79.             _password = p;
  80.         }
  81.  
  82.     }
  83.  
  84.     [Serializable]
  85.     public class LoginAck
  86.     {
  87.         private bool _loginSucceeded;
  88.  
  89.  
  90.         public bool loginSucceeded { get { return _loginSucceeded; } }
  91.  
  92.  
  93.         public LoginAck(bool success)
  94.         {
  95.             _loginSucceeded = success;
  96.         }
  97.  
  98.     }
  99.  
  100.     [Serializable]
  101.     public enum FILE_POSITION { ON_CLIENT, ON_SERVER, SYNCED }
  102.  
  103.     [Serializable]
  104.     public class FileStatus
  105.     {
  106.         private string _fullName;
  107.         public string fullName { get { return _fullName; } }
  108.  
  109.         private string _fileSystemPath;
  110.  
  111.         private byte[] _checksum;
  112.         public byte[] checksum { get { return _checksum; } }
  113.  
  114.         private DateTime _lastModified;
  115.         public DateTime lastModified { get { return _lastModified; } }
  116.  
  117.         private FILE_POSITION _position;
  118.         public FILE_POSITION position
  119.         {
  120.             get { return _position; }
  121.         }
  122.  
  123.         private bool _isFolder;
  124.         public bool isFolder
  125.         {
  126.             get { return _isFolder; }
  127.         }
  128.  
  129.         private int _fileSize;
  130.         public int fileSize
  131.         {
  132.             get { return _fileSize; }
  133.             set { _fileSize = value; }
  134.         }
  135.  
  136.         public FileStatus(string fullName, string fileSystemPath , FILE_POSITION position, DateTime lastModified, bool isFolder = false)
  137.         {
  138.             _fullName = fullName;
  139.             _fileSystemPath = fileSystemPath;
  140.             _position = position;
  141.             _isFolder = isFolder;
  142.             _lastModified = lastModified;
  143.             computeChecksum();
  144.         }
  145.  
  146.         public FileStatus(string fullName, FILE_POSITION position, bool isFolder = false)
  147.         {
  148.             _fullName = fullName;
  149.             _fileSystemPath = null;
  150.             _position = position;
  151.             _isFolder = isFolder;
  152.             _lastModified = DateTime.MinValue;
  153.             _checksum = null;
  154.         }
  155.  
  156.         public void computeChecksum()
  157.         {
  158.             if (File.Exists(this._fileSystemPath))
  159.  
  160.             {
  161.                 using (MD5 md5 = MD5.Create())
  162.                 {
  163.                     using (Stream fs = File.OpenRead(this._fileSystemPath))
  164.                     {
  165.                         _checksum = md5.ComputeHash(fs);
  166.                     }
  167.                 }
  168.             }
  169.             else if (Directory.Exists(this._fileSystemPath))
  170.             {
  171.                 _isFolder = true;
  172.                 _checksum = null;
  173.             }
  174.             else
  175.             {
  176.                 throw new Exception("File doesn't exist.");
  177.             }
  178.         }
  179.  
  180.         public static bool operator ==(FileStatus f1, FileStatus f2)
  181.         {
  182.             if ((f1.fullName == f2.fullName) && (f1.checksum == f2.checksum))
  183.                 return true;
  184.  
  185.             if ((object)f1 == null && (object)f2 == null)
  186.             {
  187.                 return true;
  188.             }
  189.  
  190.             return false;
  191.  
  192.         }
  193.         public static bool operator !=(FileStatus f1, FileStatus f2)
  194.         {
  195.             if (!(f1 == f2))
  196.                 return true;
  197.             return false;
  198.         }
  199.  
  200.         public override bool Equals(object obj)
  201.         {
  202.             return this == obj as FileStatus;
  203.         }
  204.     }
  205.  
  206.     [Serializable]
  207.     public class FileSystemStatusGet
  208.     {
  209.         private string _root;
  210.         public string root { get { return _root; } }
  211.  
  212.         private SortedList<string, FileStatus> _fileStatusList;
  213.         public SortedList<string, FileStatus> fileStatusList { get { return _fileStatusList; } }
  214.  
  215.         public FileSystemStatusGet(string root)
  216.         {
  217.             _fileStatusList = new SortedList<string, FileStatus>();
  218.             _root = root;
  219.         }
  220.  
  221.     }
  222.  
  223.     [Serializable]
  224.     public class FileSystemStatusAck
  225.     {
  226.         private SortedList<string, FileStatus> _fileStatusList;
  227.         public SortedList<string, FileStatus> fileStatusList { get { return _fileStatusList; } }
  228.  
  229.         public FileSystemStatusAck()
  230.         {
  231.             _fileStatusList = new SortedList<string, FileStatus>();
  232.         }
  233.     }
  234. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement