コード例 #1
0
        //public static void PrioritizeMultiCastInterface(ref UdpClient udpClient)
        //{
        //        //udpClient.Client.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastInterface, (int)IPAddress.HostToNetworkOrder(p.Index));
        //        //udpClient.Client.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastInterface, p.Index);
        //}

        static void Main(String[] args)
        {
            Globals.Initialize();
            DownloadMetaData.Initialize();

            //receive thread
            Globals.receiveThread = new Thread(Receive.receive);
            Globals.receiveThread.Start();

            //Sync Nodes
            SyncNodes.Sync(Globals.nodeId);
            Thread.Sleep(1000);

            while (true)
            {
                Console.Write("Do you want to download file >> ");
                string resp = Console.ReadLine(); //yes or no
                if (resp == "yes")
                {
                    Console.Write("Enter file URL >> ");
                    string url = Console.ReadLine();
                    bool   iswebsiteSupported = Downloader.Downloader.IsWebsiteSupported(url);
                    Thread.Sleep(1000);
                    if (iswebsiteSupported)
                    {
                        Console.WriteLine("file supported");
                        SendData.Send(new DataObject("request", url, Globals.nodeId, ""));
                        Tuple <string, IEnumerable <int> > tuple = Downloader.Downloader.GetNameAndPartSizes(url, Globals.numofnodes);
                        DownloadMetaData.AllocateParts(tuple.Item2);
                        DownloadMetaData.ShareParts();
                        Thread.Sleep(1000);
                        Downloader.Downloader.DownloadFile(url, DownloadMetaData.downloadrange[0].Item1, DownloadMetaData.downloadrange[0].Item2);
                        Console.WriteLine("file downloaded");
                        Thread.Sleep(1000);
                        Console.WriteLine("starting file sharing");
                        SendData.Send(new DataObject("datasharing", "", Globals.nodeId, ""));
                    }
                    else
                    {
                    }
                }
                else
                {
                    Console.WriteLine("now handling downloads for others.");
                    break;
                }
            }
            //DataObject dataObject = new DataObject("request", "hello there", Globals.nodeId, "");
            //SendData.Send(dataObject);
            //send thread
            // while (true)
            // {
            // Byte[] data = udpClient.Receive(ref localEndPoint);
            // String strData = Encoding.Unicode.GetString(data);
            // Console.WriteLine(strData);


            // }
            Globals.receiveThread.Join();
        }
コード例 #2
0
 public static void ShareParts()
 {
     partsandnodes.Add(Tuple.Create(0, Globals.nodeId));
     for (int i = 1; i < downloadrange.Count; i++)
     {
         string temp = downloadrange[i].Item1 + " " + downloadrange[i].Item2;
         partsandnodes.Add(Tuple.Create(i, Globals.nodes[i]));
         DataObject dataObject = new DataObject("partallocation", temp, Globals.nodeId, Globals.nodes[i]);
         SendData.Send(dataObject);
     }
 }
コード例 #3
0
 public static void RecognizeNode(DataObject dataObject)
 {
     if (!Globals.nodes.Contains(dataObject.sender))
     {
         Globals.nodes.Add(dataObject.sender);
         Globals.numofnodes += 1;
         string temp = string.Join(",", Globals.nodes.ToArray());
         SendData.Send(new DataObject("syncresponse", temp, Globals.nodeId, dataObject.sender));
         Console.WriteLine("send sync response");
     }
 }
コード例 #4
0
 public static void FindTokenResponse()
 {
     if (tokenHolder == null)
     {
         SendData.Send(new DataObject("findtokenresponse", "", Globals.nodeId, ""));
     }
     else
     {
         SendData.Send(new DataObject("findtokenresponse", tokenHolder, Globals.nodeId, ""));
     }
 }
コード例 #5
0
 public static void Sync(String nodeId)
 {
     Console.WriteLine("sent sync request");
     SyncRequest = new DataObject("sync", nodeId, nodeId, "");
     SendData.Send(SyncRequest);
 }
コード例 #6
0
        public static void FindTokenHolder()
        {
            DataObject dataObject = new DataObject("findtoken", "", Globals.nodeId, "");

            SendData.Send(dataObject);
        }
コード例 #7
0
ファイル: Receive.cs プロジェクト: sharifahmad2061/DC_Project
        public static void receive()
        {
            while (true)
            {
                Byte[] data    = Globals.udpClient.Receive(ref Globals.remoteEndPoint);
                String strData = Globals.encoding.GetString(data);
                //Console.WriteLine("printed from receive");
                //Console.WriteLine(strData);
                //receive_queue.Enqueue(strData);
                DataObject JsonData = JsonConvert.DeserializeObject <DataObject>(strData);
                switch (JsonData.type)
                {
                case "sync":
                    Console.WriteLine("sync request received");
                    SyncNodes.RecognizeNode(JsonData);
                    break;

                case "syncresponse":
                    Console.WriteLine("sync response");
                    SyncNodes.HandleSyncResponse(JsonData);
                    break;

                case "findtoken":
                    SendData.Send(new DataObject("findtokenresponse", LeaderSelection.tokenHolder, "", ""));
                    break;

                case "findtokenresponse":
                    LeaderSelection.HandleFindTokenResponse(JsonData);
                    break;

                case "request":
                    Console.WriteLine("file download request received");
                    DownloadMetaData.Url = JsonData.data;
                    break;

                case "partallocation":
                    Console.WriteLine("part allocation request received");
                    if (JsonData.receiver == Globals.nodeId)
                    {
                        Console.WriteLine("downloading file");
                        DownloadMetaData.StoreRange(JsonData.data);
                        localfilename = Downloader.Downloader.DownloadFile(DownloadMetaData.Url, DownloadMetaData.downloadrange[0].Item1, DownloadMetaData.downloadrange[0].Item2);
                        Console.WriteLine("file downloaded");
                    }
                    break;

                case "datasharing":
                    client.Start();
                    client.Connect(Globals.multicastAddress.ToString(), 2222);
                    listener.NetworkReceiveEvent += (fromPeer, dataReader) =>
                    {
                        Console.WriteLine("We got: {0}", dataReader.GetString(100));
                        dataReader.Clear();
                    };
                    Console.WriteLine("data sharing request received");
                    SendData.Send(new DataObject("filesending", "", Globals.nodeId, JsonData.sender));
                    Console.WriteLine(localfilename);
                    Globals.udpClient.Client.SendFile(localfilename);
                    break;

                case "filesending":
                    Byte[] file_data = Globals.udpClient.Receive(ref Globals.remoteEndPoint);
                    var    fs        = new FileStream("file1.tmp", FileMode.Create, FileAccess.Write);
                    fs.Write(file_data, 0, file_data.Length);
                    break;

                default:
                    Console.WriteLine("packet of some type else");
                    break;
                }
            }
        }