public virtual void test_createUsingByte()
 {
     Assert.False(ProtocolSwitch.create(0).isOn(0));
     Assert.True(ProtocolSwitch.create(1).isOn(0));
     Assert.True(ProtocolSwitch.create(2).isOn(1));
     Assert.True(ProtocolSwitch.create(4).isOn(2));
     Assert.True(ProtocolSwitch.create(8).isOn(3));
     Assert.True(ProtocolSwitch.create(3).isOn(0));
     Assert.True(ProtocolSwitch.create(3).isOn(1));
     Assert.False(ProtocolSwitch.create(3).isOn(2));
     Assert.True(ProtocolSwitch.create(64).isOn(6));
     Assert.False(ProtocolSwitch.create(64).isOn(0));
     Assert.True(ProtocolSwitch.create(127).isOn(0));
     Assert.True(ProtocolSwitch.create(127).isOn(1));
     Assert.True(ProtocolSwitch.create(127).isOn(6));
     Assert.False(ProtocolSwitch.create(127).isOn(7));
     Assert.True(ProtocolSwitch.create(255).isOn(0));
     Assert.True(ProtocolSwitch.create(255).isOn(1));
     Assert.True(ProtocolSwitch.create(255).isOn(7));
     try
     {
         ProtocolSwitch.create(511);
         Assert.Null("Should not reach here!");
     }
     catch (System.ArgumentOutOfRangeException)
     {
     }
 }
        public virtual void test_byteToBitSet()
        {
            var bs = ProtocolSwitch.toBitSet(1);

            Assert.True(bs.Get(0));

            bs = ProtocolSwitch.toBitSet(2);
            Assert.True(bs.Get(1));

            bs = ProtocolSwitch.toBitSet(4);
            Assert.True(bs.Get(2));

            bs = ProtocolSwitch.toBitSet(8);
            Assert.True(bs.Get(3));

            bs = ProtocolSwitch.toBitSet(3);
            Assert.True(bs.Get(0));
            Assert.True(bs.Get(1));

            bs = ProtocolSwitch.toBitSet(12);
            Assert.True(bs.Get(2));
            Assert.True(bs.Get(3));

            bs = ProtocolSwitch.toBitSet(64);
            Assert.True(bs.Get(6));

            bs = ProtocolSwitch.toBitSet(127);
            for (int i = 0; i <= 6; ++i)
            {
                Assert.True(bs.Get(i));
            }
        }
 public virtual void test_isSwitchOn()
 {
     for (int i = 0; i < 7; ++i)
     {
         Assert.True(ProtocolSwitch.isOn(i, 1 << i));
     }
     Assert.True(ProtocolSwitch.isOn(0, 1));
     Assert.True(ProtocolSwitch.isOn(1, 2));
     Assert.True(ProtocolSwitch.isOn(2, 4));
     Assert.True(ProtocolSwitch.isOn(3, 8));
     Assert.True(ProtocolSwitch.isOn(0, 3));
     Assert.True(ProtocolSwitch.isOn(1, 3));
     Assert.False(ProtocolSwitch.isOn(2, 3));
     Assert.True(ProtocolSwitch.isOn(6, 64));
     Assert.False(ProtocolSwitch.isOn(0, 64));
     Assert.True(ProtocolSwitch.isOn(0, 127));
     Assert.True(ProtocolSwitch.isOn(1, 127));
     Assert.False(ProtocolSwitch.isOn(7, 127));
     Assert.True(ProtocolSwitch.isOn(0, 255));
     Assert.True(ProtocolSwitch.isOn(1, 255));
     Assert.True(ProtocolSwitch.isOn(7, 255));
     try
     {
         ProtocolSwitch.isOn(7, 511);
         Assert.Null("Should not reach here!");
     }
     catch (System.ArgumentOutOfRangeException)
     {
     }
 }
        public virtual void test_toByte()
        {
            ProtocolSwitch protocolSwitch = new ProtocolSwitch();

            protocolSwitch.turnOn(0);
            Assert.Equal(1, protocolSwitch.toByte());

            protocolSwitch = new ProtocolSwitch();
            protocolSwitch.turnOn(1);
            Assert.Equal(2, protocolSwitch.toByte());

            protocolSwitch = new ProtocolSwitch();
            protocolSwitch.turnOn(2);
            Assert.Equal(4, protocolSwitch.toByte());

            protocolSwitch = new ProtocolSwitch();
            protocolSwitch.turnOn(3);
            Assert.Equal(8, protocolSwitch.toByte());

            protocolSwitch = new ProtocolSwitch();
            protocolSwitch.turnOn(0);
            protocolSwitch.turnOn(1);
            Assert.Equal(3, protocolSwitch.toByte());

            protocolSwitch = new ProtocolSwitch();
            protocolSwitch.turnOn(6);
            Assert.Equal(64, protocolSwitch.toByte());

            protocolSwitch = new ProtocolSwitch();
            for (int i = 0; i < 7; ++i)
            {
                protocolSwitch.turnOn(i);
            }
            Assert.Equal(127, protocolSwitch.toByte());

            protocolSwitch = new ProtocolSwitch();
            try
            {
                for (int i = 0; i < 9; ++i)
                {
                    protocolSwitch.turnOn(i);
                }

                protocolSwitch.toByte();
                Assert.Null("Should not reach here!");
            }
            catch (System.ArgumentOutOfRangeException)
            {
            }
        }
Esempio n. 5
0
        /// <summary>
        /// Convert application request object to remoting request command.
        /// </summary>
        /// <param name="request"> </param>
        /// <param name="conn"> </param>
        /// <param name="timeoutMillis">
        /// @return </param>
        /// <exception cref="CodecException"> </exception>
        //JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
        //ORIGINAL LINE: protected RemotingCommand toRemotingCommand(Object request, Connection conn, InvokeContext invokeContext, int timeoutMillis) throws exception.SerializationException
        protected internal virtual RemotingCommand toRemotingCommand(object request, Connection conn, InvokeContext invokeContext, int timeoutMillis)
        {
            RpcRequestCommand command = (RpcRequestCommand)CommandFactory.createRequestCommand(request);

            if (null != invokeContext)
            {
                // set client custom serializer for request command if not null
                object clientCustomSerializer = invokeContext.get(InvokeContext.BOLT_CUSTOM_SERIALIZER);
                if (null != clientCustomSerializer)
                {
                    try
                    {
                        command.Serializer = ((byte?)clientCustomSerializer).Value;
                    }
                    catch (System.InvalidCastException)
                    {
                        //JAVA TO C# CONVERTER WARNING: The .NET Type.FullName property will not always yield results identical to the Java Class.getName method:
                        throw new System.ArgumentException("Illegal custom serializer [" + clientCustomSerializer + "], the type of value should be [byte], but now is [" + clientCustomSerializer.GetType().FullName + "].");
                    }
                }

                // enable crc by default, user can disable by set invoke context `false` for key `InvokeContext.BOLT_CRC_SWITCH`
                bool?crcSwitch = (bool)invokeContext.get(InvokeContext.BOLT_CRC_SWITCH, ProtocolSwitch.CRC_SWITCH_DEFAULT_VALUE);
                if (null != crcSwitch && crcSwitch.HasValue && crcSwitch.Value)
                {
                    command.ProtocolSwitch = ProtocolSwitch.create(new int[] { ProtocolSwitch.CRC_SWITCH_INDEX });
                }
            }
            else
            {
                // enable crc by default, if there is no invoke context.
                command.ProtocolSwitch = ProtocolSwitch.create(new int[] { ProtocolSwitch.CRC_SWITCH_INDEX });
            }
            command.Timeout = timeoutMillis;
            //JAVA TO C# CONVERTER WARNING: The .NET Type.FullName property will not always yield results identical to the Java Class.getName method:
            command.RequestClass  = request.GetType();
            command.InvokeContext = invokeContext;
            command.serialize();
            logDebugInfo(command);
            return(command);
        }
        public virtual void test_createUsingIndex()
        {
            for (int i = 0; i < 7; ++i)
            {
                Assert.True(ProtocolSwitch.create(new int[] { i }).isOn(i));
            }

            int size = 7;

            int[] a = new int[size];
            for (int i = 0; i < size; ++i)
            {
                a[i] = i;
            }
            ProtocolSwitch status = ProtocolSwitch.create(a);

            for (int i = 0; i < size; ++i)
            {
                Assert.True(status.isOn(i));
            }
        }
Esempio n. 7
0
        static void Main(string[] args)
        {
            const ushort controlPortStarting = 5061;

            var settingsManager = new CommandLineClientSettingsReader();

            if (!settingsManager.ParseArgs(args))
            {
                return;
            }

            var settings = settingsManager.ClientSettings;

            var webClient = new RopuWebClient("https://192.168.1.9:5001/", settingsManager);

            var keysClient       = new KeysClient(webClient, false, encryptionKey => new CachedEncryptionKey(encryptionKey, key => new AesGcmWrapper(key)));
            var packetEncryption = new PacketEncryption(keysClient);

            var protocolSwitch    = new ProtocolSwitch(controlPortStarting, new PortFinder(), packetEncryption, keysClient, settings);
            var servingNodeClient = new ServingNodeClient(protocolSwitch);

            IAudioSource audioSource =
                settings.FileMediaSource != null ?
                (IAudioSource) new FileAudioSource(settings.FileMediaSource) :
                (IAudioSource) new PulseAudioSimple(StreamDirection.Record, "RopuInput");

            var audioPlayer            = new PulseAudioSimple(StreamDirection.Playback, "RopuOutput");
            var audioCodec             = new OpusCodec();
            var jitterBuffer           = new AdaptiveJitterBuffer(2, 50);
            var mediaClient            = new MediaClient(protocolSwitch, audioSource, audioPlayer, audioCodec, jitterBuffer, settings);
            var callManagementProtocol = new LoadBalancerProtocol(new PortFinder(), 5079, packetEncryption, keysClient);

            var beepPlayer = new BeepPlayer(new PulseAudioSimple(StreamDirection.Playback, "RopuBeeps"));


            var ropuClient = new RopuClient(protocolSwitch, servingNodeClient, mediaClient, callManagementProtocol, settings, beepPlayer, webClient, keysClient);

            var application = new RopuApplication(ropuClient);

            var imageService = new ImageService();

            //TODO: get web address from config
            var imageClient  = new ImageClient(webClient);
            var groupsClient = new GroupsClient(webClient, imageClient);
            var usersClient  = new UsersClient(webClient);
            //settings.UserId = usersClient.GetCurrentUser().Result.Id;
            var pttPage = new PttPage(imageService);

            var navigator = new Navigator();

            var colorService = new ColorService();

            navigator.Register <LoginViewModel, LoginView>(() => new LoginView(new LoginViewModel(navigator, webClient, settingsManager), imageService));

            navigator.Register <SignupViewModel, SignupPage>(() => new SignupPage(new SignupViewModel(navigator, usersClient), imageService));

            Action <Func <Task> > invoke = toDo => Application.Instance.Invoke(toDo);

            var permissionServices = new PermissionServices();

            var pttView = new PttView(new PttViewModel <Color>(ropuClient, settingsManager, groupsClient, usersClient, imageClient, colorService, invoke, permissionServices, webClient, navigator), pttPage);

            navigator.Register <PttViewModel <Color>, PttView>(() => pttView);
            navigator.RegisterView("HomeRightPanel", "PttView", () => pttView);


            var homeView = new HomeView(new HomeViewModel(navigator), navigator, colorService);

            navigator.Register <HomeViewModel, HomeView>(() => homeView);

            var browseGroupsView = new BrowseGroupsView(new BrowseGroupsViewModel(groupsClient, navigator));

            navigator.Register <BrowseGroupsViewModel, BrowseGroupsView>(() => browseGroupsView);

            Func <Group, BrowseGroupView> browseGroupViewBuilder = group => new BrowseGroupView(new BrowseGroupViewModel(group, groupsClient, settings, navigator), imageService, navigator, colorService);

            navigator.Register <BrowseGroupViewModel, BrowseGroupView, Group>(group => browseGroupViewBuilder(group));

            var selectIdleGroupView = new SelectIdleGroupView(new SelectGroupViewModel(groupsClient, navigator, ropuClient));

            navigator.RegisterView("HomeRightPanel", "SelectIdleGroupView", () => selectIdleGroupView);

            var mainForm = new MainView(navigator, new MainViewModel(settings, navigator));

            mainForm.Icon = imageService.Ropu;

            var ignore = navigator.ShowModal <HomeViewModel>();

            ignore = navigator.ShowPttView();
            application.Run(mainForm);
        }
        /// <seealso cref= CommandDecoder#decode(IChannelHandlerContext, IByteBuffer, List<object>) </seealso>
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET:
//ORIGINAL LINE: @Override public void decode(io.netty.channel.IChannelHandlerContext ctx, io.netty.buffer.IByteBuffer in, java.util.List<object><Object> out) throws Exception
        public virtual void decode(IChannelHandlerContext ctx, IByteBuffer @in, List <object> @out)
        {
            // the less length between response header and request header
            if (@in.ReadableBytes >= lessLen)
            {
                @in.MarkReaderIndex();
                byte protocol = @in.ReadByte();
                @in.ResetReaderIndex();
                if (protocol == RpcProtocolV2.PROTOCOL_CODE)
                {
                    /*
                     * ver: version for protocol
                     * type: request/response/request oneway
                     * cmdcode: code for remoting command
                     * ver2:version for remoting command
                     * requestId: id of request
                     * codec: code for codec
                     * switch: function switch
                     * (req)timeout: request timeout
                     * (resp)respStatus: response status
                     * classLen: length of request or response class name
                     * headerLen: length of header
                     * contentLen: length of content
                     * className
                     * header
                     * content
                     */
                    if (@in.ReadableBytes > 2 + 1)
                    {
                        int startIndex = @in.ReaderIndex;
                        @in.MarkReaderIndex();
                        @in.ReadByte();                         //protocol code
                        byte version = @in.ReadByte();          //protocol version
                        byte type    = @in.ReadByte();          //type
                        if (type == RpcCommandType.REQUEST || type == RpcCommandType.REQUEST_ONEWAY)
                        {
                            //decode request
                            if (@in.ReadableBytes >= RpcProtocolV2.RequestHeaderLength - 3)
                            {
                                short  cmdCode             = @in.ReadShort();
                                byte   ver2                = @in.ReadByte();
                                int    requestId           = @in.ReadInt();
                                byte   serializer          = @in.ReadByte();
                                byte   protocolSwitchValue = @in.ReadByte();
                                int    timeout             = @in.ReadInt();
                                short  classLen            = @in.ReadShort();
                                short  headerLen           = @in.ReadShort();
                                int    contentLen          = @in.ReadInt();
                                byte[] clazz               = null;
                                byte[] header              = null;
                                byte[] content             = null;

                                // decide the at-least bytes length for each version
                                int  lengthAtLeastForV1 = classLen + headerLen + contentLen;
                                bool crcSwitchOn        = ProtocolSwitch.isOn(ProtocolSwitch.CRC_SWITCH_INDEX, protocolSwitchValue);
                                int  lengthAtLeastForV2 = classLen + headerLen + contentLen;
                                if (crcSwitchOn)
                                {
                                    lengthAtLeastForV2 += 4;                                     // crc int
                                }

                                // continue read
                                if ((version == RpcProtocolV2.PROTOCOL_VERSION_1 && @in.ReadableBytes >= lengthAtLeastForV1) || (version == RpcProtocolV2.PROTOCOL_VERSION_2 && @in.ReadableBytes >= lengthAtLeastForV2))
                                {
                                    if (classLen > 0)
                                    {
                                        clazz = new byte[classLen];
                                        @in.ReadBytes(clazz);
                                    }
                                    if (headerLen > 0)
                                    {
                                        header = new byte[headerLen];
                                        @in.ReadBytes(header);
                                    }
                                    if (contentLen > 0)
                                    {
                                        content = new byte[contentLen];
                                        @in.ReadBytes(content);
                                    }
                                    if (version == RpcProtocolV2.PROTOCOL_VERSION_2 && crcSwitchOn)
                                    {
                                        checkCRC(@in, startIndex);
                                    }
                                }
                                else
                                {                                 // not enough data
                                    @in.ResetReaderIndex();
                                    return;
                                }
                                RequestCommand command;
                                if (cmdCode == CommandCode_Fields.HEARTBEAT_VALUE)
                                {
                                    command = new HeartbeatCommand();
                                }
                                else
                                {
                                    command = createRequestCommand(cmdCode);
                                }
                                command.Type           = type;
                                command.Version        = ver2;
                                command.Id             = requestId;
                                command.Serializer     = serializer;
                                command.ProtocolSwitch = ProtocolSwitch.create(protocolSwitchValue);
                                command.Timeout        = timeout;
                                command.Clazz          = clazz;
                                command.Header         = header;
                                command.Content        = content;

                                @out.Add(command);
                            }
                            else
                            {
                                @in.ResetReaderIndex();
                            }
                        }
                        else if (type == RpcCommandType.RESPONSE)
                        {
                            //decode response
                            if (@in.ReadableBytes >= RpcProtocolV2.ResponseHeaderLength - 3)
                            {
                                short  cmdCode             = @in.ReadShort();
                                byte   ver2                = @in.ReadByte();
                                int    requestId           = @in.ReadInt();
                                byte   serializer          = @in.ReadByte();
                                byte   protocolSwitchValue = @in.ReadByte();
                                short  status              = @in.ReadShort();
                                short  classLen            = @in.ReadShort();
                                short  headerLen           = @in.ReadShort();
                                int    contentLen          = @in.ReadInt();
                                byte[] clazz               = null;
                                byte[] header              = null;
                                byte[] content             = null;

                                // decide the at-least bytes length for each version
                                int  lengthAtLeastForV1 = classLen + headerLen + contentLen;
                                bool crcSwitchOn        = ProtocolSwitch.isOn(ProtocolSwitch.CRC_SWITCH_INDEX, protocolSwitchValue);
                                int  lengthAtLeastForV2 = classLen + headerLen + contentLen;
                                if (crcSwitchOn)
                                {
                                    lengthAtLeastForV2 += 4;                                     // crc int
                                }

                                // continue read
                                if ((version == RpcProtocolV2.PROTOCOL_VERSION_1 && @in.ReadableBytes >= lengthAtLeastForV1) || (version == RpcProtocolV2.PROTOCOL_VERSION_2 && @in.ReadableBytes >= lengthAtLeastForV2))
                                {
                                    if (classLen > 0)
                                    {
                                        clazz = new byte[classLen];
                                        @in.ReadBytes(clazz);
                                    }
                                    if (headerLen > 0)
                                    {
                                        header = new byte[headerLen];
                                        @in.ReadBytes(header);
                                    }
                                    if (contentLen > 0)
                                    {
                                        content = new byte[contentLen];
                                        @in.ReadBytes(content);
                                    }
                                    if (version == RpcProtocolV2.PROTOCOL_VERSION_2 && crcSwitchOn)
                                    {
                                        checkCRC(@in, startIndex);
                                    }
                                }
                                else
                                {                                 // not enough data
                                    @in.ResetReaderIndex();
                                    return;
                                }
                                ResponseCommand command;
                                if (cmdCode == CommandCode_Fields.HEARTBEAT_VALUE)
                                {
                                    command = new HeartbeatAckCommand();
                                }
                                else
                                {
                                    command = createResponseCommand(cmdCode);
                                }
                                command.Type               = type;
                                command.Version            = ver2;
                                command.Id                 = requestId;
                                command.Serializer         = serializer;
                                command.ProtocolSwitch     = ProtocolSwitch.create(protocolSwitchValue);
                                command.ResponseStatus     = (ResponseStatus)status;
                                command.Clazz              = clazz;
                                command.Header             = header;
                                command.Content            = content;
                                command.ResponseTimeMillis = new DateTimeOffset(DateTime.UtcNow).ToUnixTimeMilliseconds();
                                command.ResponseHost       = (IPEndPoint)ctx.Channel.RemoteAddress;

                                @out.Add(command);
                            }
                            else
                            {
                                @in.ResetReaderIndex();
                            }
                        }
                        else
                        {
                            string emsg = "Unknown command type: " + type;
                            logger.LogError(emsg);
                            throw new Exception(emsg);
                        }
                    }
                }
                else
                {
                    string emsg = "Unknown protocol: " + protocol;
                    logger.LogError(emsg);
                    throw new Exception(emsg);
                }
            }
        }
        public virtual void test_bitSetToByte()
        {
            var bs = new BitArray(8);

            bs.Set(0, true);
            Assert.Equal(1, ProtocolSwitch.toByte(bs));

            bs.SetAll(false);
            bs.Set(1, true);
            Assert.Equal(2, ProtocolSwitch.toByte(bs));

            bs.SetAll(false);
            bs.Set(2, true);
            Assert.Equal(4, ProtocolSwitch.toByte(bs));

            bs.SetAll(false);
            bs.Set(3, true);
            Assert.Equal(8, ProtocolSwitch.toByte(bs));

            bs.SetAll(false);
            bs.Set(0, true);
            bs.Set(1, true);
            Assert.Equal(3, ProtocolSwitch.toByte(bs));

            bs.SetAll(false);
            bs.Set(2, true);
            bs.Set(3, true);
            Assert.Equal(12, ProtocolSwitch.toByte(bs));

            bs.SetAll(false);
            bs.Set(6, true);
            Assert.Equal(64, ProtocolSwitch.toByte(bs));

            bs.SetAll(false);
            for (int i = 0; i <= 6; ++i)
            {
                bs.Set(i, true);
            }
            Assert.Equal(127, ProtocolSwitch.toByte(bs));

            bs.SetAll(false);
            for (int i = 0; i <= 7; ++i)
            {
                bs.Set(i, true);
            }
            Assert.Equal(255, ProtocolSwitch.toByte(bs));


            bs.SetAll(false);
            try
            {
                for (int i = 0; i <= 8; ++i)
                {
                    bs.Set(i, true);
                }
                ProtocolSwitch.toByte(bs);
                Assert.Null("Should not reach here!");
            }
            catch (System.ArgumentOutOfRangeException)
            {
            }
        }