private void CreatePeerBtn_Click(object sender, EventArgs e) { if (xmlFileTextBox.Text.Equals("")) { outputTextBox.Text = "You have to choose path!! Peer not created"; return; } try { //Load config file PeerConfig pc = ConfigFileManager.Load(xmlFileTextBox.Text); //Activate a remoting peer _peer = PeerActivator.NewPeer(pc.Port, pc.PeerUri); //Bind peer from config file to remoting peer ConfigFileManager.BindPeerConfigToPeer(this, pc, _peer); } catch (Exception ex) { outputTextBox.Text = "Error creating peer. Error Message: " + ex.Message; outputTextBox.AppendText("Peer not created"); return; } //change the form options for using after creating a peer xmlFileTextBox.ReadOnly = true; addPeerTextBox.ReadOnly = false; musicToSearchTextBox.ReadOnly = false; albumToSearchTextBox.ReadOnly = false; outputTextBox.Text = "Peer created. Uri: " + _peer.GetPeerURI(); }
public static void BindPeerConfigToPeer(Form1 form, PeerConfig pc, Peer peer) { peer.SetForm(form); peer.SetUri(pc.PeerUri); if (pc.Musics != null) { foreach (string music in pc.Musics) { peer.AddMusic(music); } } if (pc.Albuns != null) { foreach (string album in pc.Albuns) { peer.AddAlBum(album); } } if (pc.AssociatedPeers != null) { foreach (string p in pc.AssociatedPeers) { peer.AddAssociatedPeer(p); } } }
public static void Save(Peer peer, string path) { // Create an instance of the XmlSerializer class; // specify the type of object to serialize. XmlSerializer serializer = new XmlSerializer(typeof(PeerConfig)); TextWriter writer = new StreamWriter(path); // peer.path PeerConfig peerConfig = new PeerConfig(); //TODO os AssociatePeers sao IPeer e tem de ser string //peerConfig.AssociatedPeers = peer.AssociatedPeers; peerConfig.Musics = peer.MyMusics.ToArray(); peerConfig.PeerUri = peer.Uri; peerConfig.Port = peer.Port; peerConfig.Albuns = peer.MyAlbum.ToArray(); peerConfig.ConnectionType = peer.ConnectionType; peerConfig.AssociatedPeers = peer.MyKnownPeers.Select((m) => m.GetPeerURI()).ToArray(); serializer.Serialize(writer, peerConfig); writer.Close(); }