Exemple #1
0
        public static PyroSerializer GetFor(Config.SerializerType type)
        {
            switch (type)
            {
            case Config.SerializerType.pickle:
                return(pickleSerializer);

            case Config.SerializerType.serpent:
            {
                // Create a serpent serializer if not yet created.
                // This is done dynamically so there is no assembly dependency on the Serpent assembly,
                // and it will become available once you copy that into the correct location.
                lock (typeof(SerpentSerializer))
                {
                    if (serpentSerializer == null)
                    {
                        try {
                            serpentSerializer = new SerpentSerializer();
                            return(serpentSerializer);
                        } catch (TypeInitializationException x) {
                            throw new PyroException("serpent serializer unavailable", x);
                        }
                    }
                    return(serpentSerializer);
                }
            }

            default:
                throw new ArgumentException("unrecognised serializer type: " + type);
            }
        }
Exemple #2
0
		public static PyroSerializer GetFor(Config.SerializerType type)
		{
			switch(type)
			{
				case Config.SerializerType.pickle:
					return pickleSerializer;
				case Config.SerializerType.serpent:
					{
						// Create a serpent serializer if not yet created.
						// This is done dynamically so there is no assembly dependency on the Serpent assembly,
						// and it will become available once you copy that into the correct location.
						lock(typeof(SerpentSerializer))
						{
							if(serpentSerializer==null)
							{
								try {
									serpentSerializer = new SerpentSerializer();
									return serpentSerializer;
								} catch (TypeInitializationException x) {
									throw new PyroException("serpent serializer unavailable", x);
								}
							}
							return serpentSerializer;
						}
					}
				default:
					throw new ArgumentException("unrecognised serializer type: "+type);
			}
		}
        public void PyroClassesSerpent()
        {
            var ser = new SerpentSerializer();
            var uri = new PyroURI("PYRO:something@localhost:4444");
            byte[] s = ser.serializeData(uri);
            object x = ser.deserializeData(s);
            Assert.AreEqual(uri, x);

            var proxy = new PyroProxy(uri);
            proxy.correlation_id = Guid.NewGuid();
            proxy.pyroHandshake = "apples";
            proxy.pyroHmacKey = Encoding.UTF8.GetBytes("secret");
            proxy.pyroAttrs = new HashSet<string>();
            proxy.pyroAttrs.Add("attr1");
            proxy.pyroAttrs.Add("attr2");
            s = ser.serializeData(proxy);
            x = ser.deserializeData(s);
            PyroProxy proxy2 = (PyroProxy) x;
            Assert.AreEqual(uri.host, proxy2.hostname);
            Assert.AreEqual(uri.objectid, proxy2.objectid);
            Assert.AreEqual(uri.port, proxy2.port);
            Assert.IsNull(proxy2.correlation_id, "correlation_id is not serialized on the proxy object");
            Assert.AreEqual(proxy.pyroHandshake, proxy2.pyroHandshake);
            Assert.AreEqual(proxy.pyroHmacKey, proxy2.pyroHmacKey);
            Assert.AreEqual(2, proxy2.pyroAttrs.Count);
            Assert.AreEqual(proxy.pyroAttrs, proxy2.pyroAttrs);

            PyroException ex = new PyroException("error");
            s = ser.serializeData(ex);
            x = ser.deserializeData(s);
            PyroException ex2 = (PyroException) x;
            Assert.AreEqual(ex.Message, ex2.Message);
            Assert.IsNull(ex._pyroTraceback);

            // try another kind of pyro exception
            s = Encoding.UTF8.GetBytes("{'attributes':{'tb': 'traceback', '_pyroTraceback': ['line1', 'line2']},'__exception__':True,'args':('hello',42),'__class__':'CommunicationError'}");
            x = ser.deserializeData(s);
            ex2 = (PyroException) x;
            Assert.AreEqual("hello", ex2.Message);
            Assert.AreEqual("traceback", ex2.Data["tb"]);
            Assert.AreEqual("line1line2", ex2._pyroTraceback);
        }
        public void UnserpentProxy()
        {
            byte[] data = Encoding.UTF8.GetBytes("# serpent utf-8 python3.2\n" +
                                                 "{'state':('PYRO:Pyro.NameServer@localhost:9090',(),('count','lookup','register','ping','list','remove'),(),0.0,'b64:c2VjcmV0','hello',0),'__class__':'Pyro4.core.Proxy'}");

            SerpentSerializer ser = new SerpentSerializer();
            PyroProxy p = (PyroProxy) ser.deserializeData(data);
            Assert.IsNull(p.correlation_id);
            Assert.AreEqual("Pyro.NameServer", p.objectid);
            Assert.AreEqual("localhost", p.hostname);
            Assert.AreEqual(9090, p.port);
            Assert.AreEqual("hello", p.pyroHandshake);
            Assert.AreEqual(Encoding.UTF8.GetBytes("secret"), p.pyroHmacKey);
            Assert.AreEqual(0, p.pyroAttrs.Count);
            Assert.AreEqual(0, p.pyroOneway.Count);
            Assert.AreEqual(6, p.pyroMethods.Count);
            ISet<string> methods = new HashSet<string>();
            methods.Add("ping");
            methods.Add("count");
            methods.Add("lookup");
            methods.Add("list");
            methods.Add("register");
            methods.Add("remove");
            CollectionAssert.AreEquivalent(methods, p.pyroMethods);
        }