コード例 #1
0
        /// <summary>
        /// Only subscribed keys in the same namespace are saved.
        /// </summary>
        /// <param name="dict"></param>
        /// <returns>XML file</returns>
        public static MemoryStream Save(P2PDictionary dict)
        {
            MemoryStream writeStream = new MemoryStream();

            System.Xml.XmlTextWriter writer = new XmlTextWriter(writeStream,  Encoding.UTF8);
            ICollection<string> keys = dict.Keys;

            writer.WriteStartDocument();
            writer.WriteStartElement("p2pdictionary");
            writer.WriteStartElement("namespace");
            writer.WriteAttributeString("name", dict.Namespace);
            writer.WriteAttributeString("description", dict.Description);

            IFormatter formatter = new BinaryFormatter();

            foreach (string k in keys)
            {
                writer.WriteStartElement("entry");
                writer.WriteAttributeString("key", k);

                using (MemoryStream contents = new MemoryStream())
                {
                    formatter.Serialize(contents, dict[k]);
                    writer.WriteBase64(contents.GetBuffer(), 0, (int)contents.Length);
                }

                writer.WriteEndElement();
            }

            writer.WriteEndElement();
            writer.WriteEndElement();
            writer.Flush();

            return writeStream;
        }
コード例 #2
0
        public MainWindow(int port)
        {
            InitializeComponent();

            dictionary = new P2PDictionary("Peer Dictionary", P2PDictionary.GetFreePort(port), "SimpleChat", P2PDictionaryServerMode.AutoRegister, P2PDictionaryClientMode.AutoConnect);
            dictionary.AddSubscription("*");
            dictionary.Notification += new EventHandler<NotificationEventArgs>(dictionary_Notification);

            this.Title = "Simple Chat (" + (dictionary.LocalEndPoint as System.Net.IPEndPoint).Port + ")";
        }
コード例 #3
0
        public MainWindow(int port)
        {
            InitializeComponent();

            dictionary = new P2PDictionary("Peer Dictionary", port, "shared_photos", P2PDictionaryServerMode.AutoRegister, P2PDictionaryClientMode.AutoConnect);
            dictionary.AddSubscription("*");
            dictionary.Notification += new EventHandler<NotificationEventArgs>(dictionary_Notification);

            dictionary[dictionary.LocalID + "/" + counter] = ConsoleColor.Blue;
        }
コード例 #4
0
        public void RegisterServer(P2PDictionary dict)
        {
            if (service != null)
                throw new NotImplementedException();

            service = new RegisterService();
            service.Name = "com.rhfung.P2PDictionary " + dict.Description;
            service.RegType = ZEROCONF_NAME;
            service.ReplyDomain = "local";
            service.Port = (short)((System.Net.IPEndPoint)dict.LocalEndPoint).Port;
            service.TxtRecord = new TxtRecord();
            service.TxtRecord.Add("uid", dict.LocalID.ToString());
            service.Response += new RegisterServiceEventHandler(service_Response);

            service.Register();
        }
コード例 #5
0
        List<Tuple<string, string, string>> GetDictionary(P2PDictionary dict)
        {
            List<Tuple<string, string, string>> l = new List<Tuple<string, string, string>>();
            foreach (string key in dict.Keys)
            {
                if (dict[key] != null)
                {
                    l.Add(new Tuple<string, string, string>(key, dict[key].ToString(), dict[key].GetType().ToString()));
                }
                else

                {
                    l.Add(new Tuple<string, string, string>(key, "null", "null"));
                }
            }
            return l;
        }
コード例 #6
0
        public MainWindow(string ns, int portHint)
        {
            InitializeComponent();

            mDictionary = new P2PDictionary("Dictionary Inspector", P2PDictionary.GetFreePort(portHint),
                ns, P2PDictionaryServerMode.AutoRegister, P2PDictionaryClientMode.AutoConnect);
            mDictionary.DebugBuffer = System.Console.Out;
            mDictionary.AddSubscription("*");

            mMetadata = new P2PNetworkMetadata(mDictionary);

            mTimer = new DispatcherTimer();
            mTimer.Interval = new TimeSpan(0, 0, 0, 1);
            mTimer.Tick += new EventHandler(mTimer_Tick);
            mTimer.Start();

            this.Title = "Dictionary Inspector (ns: " + ns + ") port " + ((System.Net.IPEndPoint)mDictionary.LocalEndPoint).Port;

            dataGrid.ItemsSource = GetDictionary(mDictionary);
        }
コード例 #7
0
        /// <summary>
        /// Reads a XML and loads a dictionary. Dictionary version
        /// numbers are not preserved. 
        /// </summary>
        /// <remarks>
        /// The dictionary namespace must be the same. Dictionary
        /// comments are not restored.
        /// </remarks>
        /// <param name="dict"></param>
        /// <param name="readStream">Reads an XML file produced by Save</param>
        public static void Load(P2PDictionary dict, Stream readStream)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(readStream);
            XmlElement ns = doc["p2pdictionary"]["namespace"];
            if (ns.Attributes["name"].Value != dict.Namespace)
            {
                throw new NotSupportedException("cannot load the dictionary of another namespace");
            }

            IFormatter formatter = new BinaryFormatter();
            foreach (XmlNode entry in  ns.GetElementsByTagName("entry"))
            {
                string key = ((XmlElement)entry).GetAttribute("key");

                string serializedValue = ((XmlElement)entry).InnerText;
                byte[] bytes = Convert.FromBase64String(serializedValue);
                MemoryStream memory = new MemoryStream(bytes);
                object value = formatter.Deserialize(memory);

                dict[key] = value;
            }
        }
コード例 #8
0
 public P2PNetworkMetadata(P2PDictionary dictionary)
 {
     m_dict = dictionary;
 }
コード例 #9
0
        static void Main(string[] args)
        {
            // FileStream fs = new FileStream("setup-jammanhci-2.3.exe", FileMode.Open);
            //byte[] buffer = new byte[fs.Length];
            //fs.Read(buffer, 0, (int)fs.Length);
            com.rhfung.P2PDictionary.P2PDictionary port80 = new P2PDictionary("port 80", 80, "test", P2PDictionaryServerMode.Hidden, P2PDictionaryClientMode.ManualConnect);
            port80.SetDebugBuffer(new StreamWriter("output-80.txt"), 0, true);

            //port80.AddSubscription("*");
            port80["hidden"] = "Server at 80";
            port80["junk"] = "junk junk junk";

            com.rhfung.P2PDictionary.P2PDictionary port81 = new P2PDictionary("port 81", 81, "test", P2PDictionaryServerMode.Hidden, P2PDictionaryClientMode.ManualConnect);
            //client.StartClient("127.0.0.1", 80);
            port81.DebugBuffer = new StreamWriter("output-81.txt");
            port81["hidden"] = "Server at 81";
            port81["number"] = 1;
            port81["text"] = "Hello world!";

            com.rhfung.P2PDictionary.P2PDictionary port82 = new P2PDictionary("port 82", 82, "test", P2PDictionaryServerMode.Hidden, P2PDictionaryClientMode.ManualConnect);
            port82.DebugBuffer = new StreamWriter("output-82.txt");
            port81["hidden"] = "Server at 82";
            port82["ohayo"] = "o-ha-yo go-za-i-ma-su";
            port82["binary"] = new byte[] { 88, 88, 77, 77, 88, 88 };
            port82["bool"] = true;

            com.rhfung.P2PDictionary.P2PDictionary port83 = new P2PDictionary("port 83", 83, "test", P2PDictionaryServerMode.Hidden, P2PDictionaryClientMode.ManualConnect);
            port83.DebugBuffer = new StreamWriter("output-83.txt");
            port83["hidden"] = "Server at 83";

            //server3.AddSubscription("*");

            P2PDictionary port92 = new P2PDictionary("port 92", 92, "test", P2PDictionaryServerMode.Hidden, P2PDictionaryClientMode.ManualConnect);
            port92.DebugBuffer = new StreamWriter("output-92.txt");
            // stuck.AddSubscription("*");
            port92.AddSubscription("number");

            /*
            FormPeerOverview form1 = new FormPeerOverview();
            form1.Show();
            form1.SetServer(server);

            FormPeerOverview form2 = new FormPeerOverview();
            form2.Show();
            form2.SetServer(client);

            FormPeerOverview form3 = new FormPeerOverview();
            form3.Show();
            form3.SetServer(client2);

            FormPeerOverview form4 = new FormPeerOverview();
            form4.Show();
            form4.SetServer(server3);

            FormPeerOverview form5 = new FormPeerOverview();
            form5.Show();
            form5.SetServer(stuck);

            System.Console.WriteLine("Press space bar to quit");

            System.Windows.Forms.Application.Run(form1);
             */

            port81.OpenClient(System.Net.IPAddress.Loopback, 80);
            port82.OpenClient(System.Net.IPAddress.Loopback, 81);
            port83.OpenClient(System.Net.IPAddress.Loopback, 82);
            port92.OpenClient(System.Net.IPAddress.Loopback, 83);
            port92.OpenClient(System.Net.IPAddress.Loopback, 3333);

            Console.WriteLine("debug peers 80-83, 92 using a web browser and request /data");
            Console.WriteLine("press space bar to quit or any other key for write/delete on port 82");
            ConsoleKeyInfo k;
            do
            {
                 k = System.Console.ReadKey();

                 port82["junk"] = "new junk";
                 port82.Remove("junk");
            }
            while (k.KeyChar != ' ');

            var stuff = port80.GetEnumerator();

            // break connection and modify keys
            Console.WriteLine("wrote hidden key in peer 80, press any key to quit");
            port83.Close();
            port80["hidden"] = "won't make it to 92";
            System.Console.ReadKey();

            System.Console.WriteLine("Stopping...");
            port82.Close();
            port83.Close();
            port81.Close();
            port80.Close();
            port92.Close();

            port80.DebugBuffer.Flush();
            port81.DebugBuffer.Flush();
            port82.DebugBuffer.Flush();
            port83.DebugBuffer.Flush();
            port92.DebugBuffer.Flush();
        }
コード例 #10
0
        static void RunOptionsAndExit(Options opts)
        {
            var discoveryModules = new Dictionary <string, Func <IPeerInterface> >();

            discoveryModules.Add("none", () => new NoDiscovery());
#if !NETCOREAPP2_0
            discoveryModules.Add("bonjour", () => new ZeroconfDiscovery());
            discoveryModules.Add("win-bonjour", () => new ZeroconfDiscovery());
#endif

            if (!discoveryModules.ContainsKey(opts.Discovery))
            {
                Console.Error.WriteLine($"Discovery module not supported: {opts.Discovery}");
                return;
            }

            P2PDictionary dict = new P2PDictionary(
                opts.Description,
                opts.Port,
                opts.Namespace,
                searchForClients: opts.Timespan,
                peerDiscovery: discoveryModules[opts.Discovery]());

            if (opts.FullDebug)
            {
                dict.SetDebugBuffer(Console.Out, 0, true);
            }
            else if (opts.Debug)
            {
                dict.SetDebugBuffer(Console.Out, 1, true);
            }

            if (!opts.NoPattern)
            {
                bool hasAddedPattern = false;
                foreach (var pattern in opts.Pattern)
                {
                    hasAddedPattern = true;
                    dict.AddSubscription(pattern);
                }

                if (!hasAddedPattern)
                {
                    dict.AddSubscription("*");
                }
            }

            foreach (var node in opts.Node)
            {
                var splitStr = node.Split(':');
                var address  = IPAddress.Parse(splitStr[0]);
                dict.OpenClient(address, Int32.Parse(splitStr[1]));
            }

            bool cancelled = false;

            Console.Out.WriteLine("Server started");
            Console.CancelKeyPress += (object sender, ConsoleCancelEventArgs e) => {
                Console.Out.WriteLine("Cancel pressed");
                cancelled = true;
                e.Cancel  = true;
            };

            while (!cancelled)
            {
                try
                {
                    Thread.Sleep(1000);
                }
                catch (ThreadInterruptedException)
                {
                    break;
                }
            }

            dict.Close();

            Console.Out.WriteLine("Server finished");
        }
コード例 #11
0
 public WeakDataServer(P2PDictionary target)
     : base(target)
 {
 }