private void receiveFileFromPeer(peerClient thisPeer, String fileName)
        {
            String completePathOfFile = Folder + "\\" + fileName;

            if (File.Exists(completePathOfFile))
            {
                File.Delete(completePathOfFile);
            }
            /*creating new file.*/
            Stream dest = File.OpenWrite(completePathOfFile);/*ye received file is client k apne folder me save hogi.*/

            listBox1.Items.Add("receiving file '" + fileName + "' from the peer " + thisPeer.Port + "...");
            while (true)
            {
                byte[] buffer    = new byte[1500000];
                int    msgLength = thisPeer.Connector.Receive(buffer, 0, buffer.Length, 0);
                if (getActualMessage(buffer, msgLength) == "file completed")
                {
                    listBox1.Items.Add("File received successfully, and saved in your folder.");
                    dest.Close();                /*file completely received, so closes the file so that file will be completely received.
                                                  * aur phir is peer se normal messages receive krne shuru kar dein ge.*/
                    thisPeer.receiveStatus = ""; /*ab dubara normal messages receive karein ge.*/
                    return;                      /*returning from this function to normally receiving messages state from this peer.*/
                }
                char[] chars = new char[msgLength];
                Encoding.Default.GetDecoder().GetChars(buffer, 0, msgLength, chars, 0);
                dest.Write(buffer, 0, msgLength);/*buffer me se 0th index se msgLength tak ka sara data 'dest' ki current
                                                  * position (end) per write (append) kar dena he.*/
            }
        }
 void sendFileToPeerIfItAAccepts(peerClient p1, String sourcefileCompleteNameWithPath)
 {
     sendBtn.Enabled     = false;
     downloadBtn.Enabled = false;
     p1.sendStatus       = "*";
     while (p1.sendStatus == "*")
     {
         ;
     }
     if (p1.sendStatus == "1")
     {
         /*this peer accepts the file, and I will send the file to it.*/
         p1.Connector.SendFile(sourcefileCompleteNameWithPath);
         Thread.Sleep(75);
         p1.Connector.Send(Encoding.Default.GetBytes("file completed"));
         listBox1.Items.Add("File sent successfully");
     }
     else/*p1.sendStatus == ""*/
     {
         listBox1.Items.Add("File is rejected by peer " + p1.Port);
     }
     p1.sendStatus       = "";
     sendBtn.Enabled     = true;/*ab me koi aur file bi send kar sakta hu.*/
     downloadBtn.Enabled = true;
 }
Beispiel #3
0
 return(new HastingDiscoveryTest(logger,
                                 peerRepository,
                                 peerSettings,
                                 dns,
                                 peerClient,
                                 peerMessageCorrelationManager,
                                 cancellationTokenProvider,
                                 peerClientObservables,
                                 autoStart,
                                 peerDiscoveryBurnIn,
                                 state,
                                 hastingsCareTaker,
                                 millisecondsTimeout,
                                 hasValidCandidatesCheckMillisecondsFrequency,
                                 scheduler));
 private void continuouslyListeningNewPeers()
 {
     while (true)
     {
         try{
             Listener.Listen(0);/*setting that only 1 new client can come in one time.*/
             Socket temp = Listener.Accept();
             listBox1.Items.Add("New peer connected.");
             peerClient newPeer = new peerClient();
             newPeer.Connector = temp;
             temp = null;
             peer.Add(newPeer);
             Thread newThread2 = new Thread(() => continuouslyReceivingMessagesFromThisPeer(newPeer));
             newThread2.IsBackground = true;
             newThread2.Start();
         }
         catch (Exception ex) { MessageBox.Show(ex.Message); }
     }
 }
 private void ConnectToDestinPeer()
 {
     try{
         /*destination peer k sath directly connect krna he.*/
         String     destinationPeer = destinPortNo.Text;
         peerClient newPeer         = new peerClient();
         /*separating destination IP, and port to connect to destination peer directly.*/
         newPeer.IP   = destinationPeer.Substring(0, destinationPeer.IndexOf(":"));
         newPeer.Port = Convert.ToInt32(destinationPeer.Substring(destinationPeer.IndexOf(':') + 1));
         for (int i = 0; i < peer.Count(); i++)
         {
             if (peer[i].IP == newPeer.IP && peer[i].Port == newPeer.Port)
             {
                 /*I am already connected to destin. peer, tu me dobara is k sath
                  * connection establish nai karu ga.*/
                 listBox1.Items.Add("Already Connected to peer " + newPeer.Port);
                 setConnectedToDestinPeerState();
                 destinPeer.SelectedIndex = destinPeer.FindStringExact(newPeer.Port.ToString());
                 newPeer = null;
                 return;
             }
         }
         listBox1.Items.Add("Connecting to peer:" + newPeer.Port);
         newPeer.Connector.Connect(new IPEndPoint(IPAddress.Parse(newPeer.IP), newPeer.Port));
         peer.Add(newPeer);/*adding a new peer in the peers list.*/
         listBox1.Items.Add("Connected to the peer : " + newPeer.Port);
         setConnectedToDestinPeerState();
         destinPeer.Items.Add(newPeer.Port);
         destinPeer.SelectedIndex = destinPeer.FindStringExact(newPeer.Port.ToString());
         /*now receiving messages from this peer continuously in this thread.*/
         Thread newThread2 = new Thread(() => continuouslyReceivingMessagesFromThisPeer(newPeer));
         newThread2.IsBackground = true;
         newThread2.Start();
         newPeer.Connector.Send(Encoding.Default.GetBytes("My address is:" + meIP + "_" + mePort));
     }
     catch (Exception ex) { MessageBox.Show(ex.Message); }
 }
 private void continuouslyReceivingMessagesFromThisPeer(peerClient thisPeer)
 {
     while (true)
     {
         try{
             byte[] buffer    = new byte[1500];/*maximum aik packet ka size 1500 hoga.*/
             int    msgLength = thisPeer.Connector.Receive(buffer, 0, buffer.Length, 0);
             thisPeer.message = getActualMessage(buffer, msgLength);
             if (thisPeer.message.Contains("I need this file from you:"))
             {
                 /*this peer is requesting for some file from me.*/
                 String fileName = thisPeer.message.Substring(thisPeer.message.IndexOf(':') + 1);
                 if (File.Exists(Folder + "\\" + fileName))
                 {
                     thisPeer.Connector.Send(Encoding.Default.GetBytes("Ok, I am gonna sending you this file:" + fileName));
                     /*asking destin peer that Can I send you this file, having size 'fileSizeInBytes' bytes, and waiting for ack.*/
                     listBox1.Items.Add("Sending this file '" + fileName + "' to peer : " + thisPeer.Port + "...");
                     Thread t1 = new Thread(() => sendFileToPeerIfItAAccepts(thisPeer, Folder + "\\" + fileName));
                     t1.IsBackground = true;
                     t1.Start();
                 }
                 else
                 {
                     thisPeer.Connector.Send(Encoding.Default.GetBytes("I don`t have this file"));/*me us peer ko -ve ack bhej raha hu, k mere paas ye file nai he.*/
                 }
             }
             else if (thisPeer.message.Contains("Ok, I am gonna sending you this file:"))
             {
                 /*me ne us peer ko file ki request ki thi, tu us peer ne ab kaha he, k us k paas file he, aur wo
                  * peer muje file send krne laga he, tu muje ab ye file zaroor accept krni hogi, kiu k download krne k liay
                  * koi prompt nai hota, balke +ve ack hi jae gi.*/
                 String fileName = thisPeer.message.Substring(thisPeer.message.IndexOf(':') + 1);
                 thisPeer.receiveStatus = "*";
                 Thread.Sleep(50);
                 thisPeer.Connector.Send(Encoding.Default.GetBytes("Yes, send this file to me"));/*telling to the peer that OK, ye wali file 'fileName' muje
                                                                                                  * send kar do, aur me is file ko receive kar lu ga.*/
                 thisPeer.receiveStatus = "1";
                 receiveFileFromPeer(thisPeer, fileName);
                 thisPeer.receiveStatus = "";
             }
             else if (thisPeer.message == "I don`t have this file")
             {
                 thisPeer.receiveStatus = "";
             }
             else if (thisPeer.message == "Yes, send this file to me")
             {
                 thisPeer.sendStatus = "1";
             }
             else if (thisPeer.message == "No, don`t send this file to me")
             {
                 thisPeer.sendStatus = "";
             }
             else if (thisPeer.message.Contains("My address is:"))
             {
                 /*receive message from thisPeer about his ip:port, so I save it.*/
                 String extractedMsg = thisPeer.message.Substring(thisPeer.message.IndexOf(':') + 1);
                 String IP           = extractedMsg.Substring(0, extractedMsg.IndexOf("_"));
                 int    Port         = Convert.ToInt32(extractedMsg.Substring(extractedMsg.IndexOf('_') + 1));
                 for (int i = 0; i < peer.Count(); i++)
                 {
                     /*kya ye peer pehle se exist krta he?*/
                     if (peer[i].Port == Port)
                     {
                         listBox1.Items.Add(thisPeer.Port + " peer already connected.");
                         peer.Remove(thisPeer);
                         return;
                     }
                 }
                 thisPeer.IP   = IP;
                 thisPeer.Port = Port;
                 listBox1.Items.Add("Connected to the peer : " + thisPeer.Port);
                 sendBtn.Enabled     = true;
                 downloadBtn.Enabled = true;
                 destinPeer.Items.Add(thisPeer.Port);
                 destinPeer.SelectedIndex = destinPeer.FindStringExact(thisPeer.Port.ToString());
             }
             else if (thisPeer.message.Contains("Can I send this file to you:"))
             {
                 String fileName = thisPeer.message.Substring(thisPeer.message.IndexOf(':') + 1);
                 thisPeer.receiveStatus = "*";/*I get notification to accept/reject the file that`s name received from peer.*/
                 DialogResult dialogResult = MessageBox.Show("Do you want to save the file '" + fileName + "' ", "New File received from the peer " + thisPeer.Port, MessageBoxButtons.YesNo);
                 if (dialogResult == DialogResult.Yes)
                 {
                     thisPeer.Connector.Send(Encoding.Default.GetBytes("Yes, send this file to me"));/*telling to the peer that OK, ye wali file 'fileName' muje
                                                                                                      * send kar do, aur me is file ko receive kar lu ga.*/
                     thisPeer.receiveStatus = "1";
                     receiveFileFromPeer(thisPeer, fileName);
                 }
                 else
                 {
                     thisPeer.Connector.Send(Encoding.Default.GetBytes("No, don`t send this file to me"));/*(-ve ack) telling to the peer that do not send me this file 'filename'*/
                 }
                 thisPeer.receiveStatus = "";
                 /*is peer se agar ye file completely acctepted/rejected ho gai he, tu phir dubara ham normal messages ko receive karein ge.*/
             }
             else
             {
                 listBox1.Items.Add("Peer " + thisPeer.Port + " : " + thisPeer.message);
             }
         }
         catch (Exception ex)
         {
             listBox1.Items.Add(thisPeer.Port + " peer left");
             peer.Remove(thisPeer);
             return;/*no more this peer alive, so we close accpting things from it.*/
         }
     }
 }