Exemplo n.º 1
0
        public void serialize(OutputArchive archive, string tag)
        {
            archive.startRecord(this, tag);
            foreach (Op op in ops)
            {
                MultiHeader h = new MultiHeader(op.get_Type(), false, -1);
                ((Record)h).serialize(archive, tag);
                ZooDefs.OpCode opCode = EnumUtil <ZooDefs.OpCode> .DefinedCast(op.get_Type());

                switch (opCode)
                {
                case ZooDefs.OpCode.create:
                    op.toRequestRecord().serialize(archive, tag);
                    break;

                case ZooDefs.OpCode.delete:
                    op.toRequestRecord().serialize(archive, tag);
                    break;

                case ZooDefs.OpCode.setData:
                    op.toRequestRecord().serialize(archive, tag);
                    break;

                case ZooDefs.OpCode.check:
                    op.toRequestRecord().serialize(archive, tag);
                    break;

                default:
                    throw new IOException("Invalid type of op");
                }
            }
            ((Record) new MultiHeader(-1, true, -1)).serialize(archive, tag);
            archive.endRecord(this, tag);
        }
Exemplo n.º 2
0
        public void serialize(OutputArchive archive, string tag)
        {
            foreach (OpResult result in results)
            {
                ZooDefs.OpCode opcode = EnumUtil <ZooDefs.OpCode> .DefinedCast(result.get_Type());

                int err = result.get_Type() == (int)ZooDefs.OpCode.error ? ((OpResult.ErrorResult)result).getErr() : 0;

                ((Record) new MultiHeader(result.get_Type(), false, err)).serialize(archive, tag);

                switch (opcode)
                {
                case ZooDefs.OpCode.create:
                    ((Record) new CreateResponse(((OpResult.CreateResult)result).getPath())).serialize(archive, tag);
                    break;

                case ZooDefs.OpCode.delete:
                case ZooDefs.OpCode.check:
                    break;

                case ZooDefs.OpCode.setData:
                    ((Record) new SetDataResponse(((OpResult.SetDataResult)result).getStat())).serialize(archive, tag);
                    break;

                case ZooDefs.OpCode.error:
                    ((Record) new ErrorResponse(((OpResult.ErrorResult)result).getErr())).serialize(archive, tag);
                    break;

                default:
                    throw new IOException("Invalid type " + result.get_Type() + " in MultiResponse");
                }
            }
            ((Record) new MultiHeader(-1, true, -1)).serialize(archive, tag);
        }
Exemplo n.º 3
0
        /**
         * Convert a WatcherEvent sent over the wire into a full-fledged WatcherEvent
         */

        internal WatchedEvent(WatcherEvent eventMessage)
        {
            keeperState = EnumUtil <Watcher.Event.KeeperState> .DefinedCast(eventMessage.getState());

            eventType = EnumUtil <Watcher.Event.EventType> .DefinedCast(eventMessage.get_Type());

            path = eventMessage.getPath();
        }
Exemplo n.º 4
0
        public void testInvalidIntConversion()
        {
            try {
                Watcher.Event.KeeperState ks = EnumUtil <Watcher.Event.KeeperState> .DefinedCast(324142);

                Assert.fail("Was able to create an invalid KeeperState via an integer");
            }
            catch (Exception)
            {
                // we're good.
            }
        }
Exemplo n.º 5
0
        public void deserialize(InputArchive archive, string tag)
        {
            results = new List <OpResult>();

            archive.startRecord(tag);
            MultiHeader h = new MultiHeader();

            ((Record)h).deserialize(archive, tag);
            while (!h.getDone())
            {
                ZooDefs.OpCode opcode = EnumUtil <ZooDefs.OpCode> .DefinedCast(h.get_Type());

                switch (opcode)
                {
                case ZooDefs.OpCode.create:
                    CreateResponse cr = new CreateResponse();
                    ((Record)cr).deserialize(archive, tag);
                    results.Add(new OpResult.CreateResult(cr.getPath()));
                    break;

                case ZooDefs.OpCode.delete:
                    results.Add(new OpResult.DeleteResult());
                    break;

                case ZooDefs.OpCode.setData:
                    SetDataResponse sdr = new SetDataResponse();
                    ((Record)sdr).deserialize(archive, tag);
                    results.Add(new OpResult.SetDataResult(sdr.getStat()));
                    break;

                case ZooDefs.OpCode.check:
                    results.Add(new OpResult.CheckResult());
                    break;

                case ZooDefs.OpCode.error:
                    //FIXME: need way to more cleanly serialize/deserialize exceptions
                    ErrorResponse er = new ErrorResponse();
                    ((Record)er).deserialize(archive, tag);
                    results.Add(new OpResult.ErrorResult(er.getErr()));
                    break;

                default:
                    throw new IOException("Invalid type " + h.get_Type() + " in MultiResponse");
                }
                ((Record)h).deserialize(archive, tag);
            }
            archive.endRecord(tag);
        }
Exemplo n.º 6
0
        public void deserialize(InputArchive archive, string tag)
        {
            archive.startRecord(tag);
            MultiHeader h = new MultiHeader();

            ((Record)h).deserialize(archive, tag);

            while (!h.getDone())
            {
                ZooDefs.OpCode opCode = EnumUtil <ZooDefs.OpCode> .DefinedCast(h.get_Type());

                switch (opCode)
                {
                case ZooDefs.OpCode.create:
                    CreateRequest cr = new CreateRequest();
                    ((Record)cr).deserialize(archive, tag);
                    add(Op.create(cr.getPath(), cr.getData(), cr.getAcl(), cr.getFlags()));
                    break;

                case ZooDefs.OpCode.delete:
                    DeleteRequest dr = new DeleteRequest();
                    ((Record)dr).deserialize(archive, tag);
                    add(Op.delete(dr.getPath(), dr.getVersion()));
                    break;

                case ZooDefs.OpCode.setData:
                    SetDataRequest sdr = new SetDataRequest();
                    ((Record)sdr).deserialize(archive, tag);
                    add(Op.setData(sdr.getPath(), sdr.getData(), sdr.getVersion()));
                    break;

                case ZooDefs.OpCode.check:
                    CheckVersionRequest cvr = new CheckVersionRequest();
                    ((Record)cvr).deserialize(archive, tag);
                    add(Op.check(cvr.getPath(), cvr.getVersion()));
                    break;

                default:
                    throw new IOException("Invalid type of op");
                }
                ((Record)h).deserialize(archive, tag);
            }
            archive.endRecord(tag);
        }
Exemplo n.º 7
0
        public async Task TestGetResults()
        {
            var zk = await createClient();

            /* Delete of a node folowed by an update of the (now) deleted node */
            var ops = Arrays.asList(
                Op.create("/multi", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT),
                Op.delete("/multi", 0),
                Op.setData("/multi", "Y".UTF8getBytes(), 0),
                Op.create("/foo", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT)
                );
            List <OpResult> results = null;

            try
            {
                await zk.multiAsync(ops);

                Assert.fail("/multi should have been deleted so setData should have failed");
            }
            catch (KeeperException e)
            {
                // '/multi' should never have been created as entire op should fail
                Assert.assertNull(await zk.existsAsync("/multi", null));
                results = e.getResults();
            }

            Assert.assertNotNull(results);
            foreach (OpResult r in results)
            {
                LOG.info("RESULT==> " + r);
                if (r is OpResult.ErrorResult)
                {
                    OpResult.ErrorResult er = (OpResult.ErrorResult)r;
                    LOG.info("ERROR RESULT: " + er + " ERR=>" + EnumUtil <KeeperException.Code> .DefinedCast(er.getErr()));
                }
            }
        }
Exemplo n.º 8
0
        public void TestGetResults()
        {
            /* Delete of a node folowed by an update of the (now) deleted node */
            try
            {
                zk.multi(Arrays.asList(Op.create("/multi", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT), Op.delete("/multi", 0), Op.setData("/multi", "Y".getBytes(), 0), Op.create("/foo", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT)));
                Assert.fail("/multi should have been deleted so setData should have failed");
            }
            catch (KeeperException e)
            {
                // '/multi' should never have been created as entire op should fail
                Assert.assertNull(zk.exists("/multi", null));

                foreach (OpResult r in e.getResults())
                {
                    LOG.info("RESULT==> " + r);
                    if (r is OpResult.ErrorResult)
                    {
                        OpResult.ErrorResult er = (OpResult.ErrorResult)r;
                        LOG.info("ERROR RESULT: " + er + " ERR=>" + EnumUtil <KeeperException.Code> .DefinedCast(er.getErr()));
                    }
                }
            }
        }
Exemplo n.º 9
0
         /**
           * All non-specific keeper exceptions should be constructed via
           * this factory method in order to guarantee consistency in error
           * codes and such.  If you know the error code, then you should
           * construct the special purpose exception directly.  That will
           * allow you to have the most specific possible declarations of
           * what exceptions might actually be thrown.
           *
           * @param code The error code of your new exception.  This will
           * also determine the specific type of the exception that is
           * returned.
           * @return The specialized exception, presumably to be thrown by
           * the caller.
           */

        internal static KeeperException create(int code, string path = null)
        
        {
                        switch (EnumUtil <Code> .DefinedCast(code))
                            {
                     //case Code.SYSTEMERROR:
                     //    return new SystemErrorException();
                                        case Code.RUNTIMEINCONSISTENCY:
                                        return(new RuntimeInconsistencyException());

                                    case Code.DATAINCONSISTENCY:
                                        return(new DataInconsistencyException());

                                    case Code.CONNECTIONLOSS:
                                        return(new ConnectionLossException());

                                    case Code.MARSHALLINGERROR:
                                        return(new MarshallingErrorException());

                                    case Code.UNIMPLEMENTED:
                                        return(new UnimplementedException());

                                    case Code.OPERATIONTIMEOUT:
                                        return(new OperationTimeoutException());

                                    case Code.BADARGUMENTS:
                                        return(new BadArgumentsException(path));

                     //case Code.APIERROR:
                     //    return new APIErrorException();
                                        case Code.NONODE:
                                        return(new NoNodeException(path));

                                    case Code.NOAUTH:
                                        return(new NoAuthException());

                                    case Code.BADVERSION:
                                        return(new BadVersionException(path));

                                    case Code.NOCHILDRENFOREPHEMERALS:
                                        return(new NoChildrenForEphemeralsException(path));

                                    case Code.NODEEXISTS:
                                        return(new NodeExistsException(path));

                                    case Code.INVALIDACL:
                                        return(new InvalidACLException(path));

                                    case Code.AUTHFAILED:
                                        return(new AuthFailedException());

                                    case Code.NOTEMPTY:
                                        return(new NotEmptyException(path));

                                    case Code.SESSIONEXPIRED:
                                        return(new SessionExpiredException());

                                    case Code.INVALIDCALLBACK:
                                        return(new InvalidCallbackException());

                                    case Code.SESSIONMOVED:
                                        return(new SessionMovedException());

                                    case Code.NOTREADONLY:
                                        return(new NotReadOnlyException());

                                    case Code.OK:
                                    default:
                                        throw new ArgumentOutOfRangeException("code", "Invalid exception code");
                    
                }
            
        }