Example #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();
        }
Example #2
0
        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;
                }
            }
        }