public IncomingAggreGateCommand sendCommandAndCheckReplyCode(OutgoingAggreGateCommand cmd)
        {
            var ans = sendCommand(cmd);

            if (ans.getReplyCode().Equals(AggreGateCommand.REPLY_CODE_DENIED))
            {
                throw new ContextSecurityException(String.Format(Cres.get().getString("devAccessDeniedReply"),
                                                                 ans.getReplyCode(), cmd));
            }

            if (ans.getReplyCode().Equals(AggreGateCommand.REPLY_CODE_ERROR))
            {
                var message = ans.getNumberOfParameters() > AggreGateCommand.INDEX_REPLY_MESSAGE
                                  ? ": " +
                              DataTableUtils.transferDecode(ans.getParameter(AggreGateCommand.INDEX_REPLY_MESSAGE))
                                  : "";
                var details = ans.getNumberOfParameters() > AggreGateCommand.INDEX_REPLY_DETAILS
                                  ? DataTableUtils.transferDecode(ans.getParameter(AggreGateCommand.INDEX_REPLY_DETAILS))
                                  : null;
                throw new RemoteDeviceErrorException(Cres.get().getString("devServerReturnedError") + message, details);
            }

            if (!ans.getReplyCode().Equals(AggreGateCommand.REPLY_CODE_OK))
            {
                throw new RemoteDeviceErrorException(Cres.get().getString("devServerReturnedError") + ": " +
                                                     ans + " (error code: '" + ans.getReplyCode() + "')");
            }

            return(ans);
        }
        protected void processMessage(IncomingAggreGateCommand cmd, OutgoingAggreGateCommand ans)
        {
            var messageCode = cmd.getMessageCode();

            if (messageCode.Length > 1)
            {
                throw new SyntaxErrorException(Cres.get().getString("clInvalidMsgCode") + messageCode);
            }

            var code = messageCode[0];

            if ((code != AggreGateCommand.MESSAGE_CODE_START) && (!startMessageReceived))
            {
                Logger.getLogger(Log.CLIENTS).debug("Can't process message: start message was not received yet");
                ans.constructReply(cmd.getId(), AggreGateCommand.REPLY_CODE_DENIED);
                return;
            }

            switch (code)
            {
            case AggreGateCommand.MESSAGE_CODE_START:
                processMessageStart(cmd, ans);
                break;

            case AggreGateCommand.MESSAGE_CODE_OPERATION:
                processMessageOperation(cmd, ans);
                break;

            default:
                throw new SyntaxErrorException(Cres.get().getString("clUnknownMsgCode") + messageCode[0]);
            }
        }
            public override void handle(Event anEvent)
            {
                if (owner.shutDown)
                {
                    throw new ObsoleteListenerException("Client constoller was stopped");
                }

                try
                {
                    OutgoingAggreGateCommand cmd = this.owner.constructEventCommand(anEvent, this.getListenerCode());

                    owner.sendCommand(cmd);
                }
                catch (SocketException ex)
                {
                    owner.shutdown();
                    const string msg = "Socket exception while forwarding event to client: ";
                    Logger.getLogger(Log.CLIENTS).info(msg + ex);
                    Logger.getLogger(Log.CLIENTS).debug(msg + ex.Message, ex);
                }
                catch (Exception ex)
                {
                    throw new EventHandlingException(
                              String.Format(Cres.get().getString("clErrorHandlingEvent"), anEvent.getName()) + ex.Message, ex);
                }
            }
        public void processOperationRemoveEventListener(String id, String context, String name, Int32?listener,
                                                        OutgoingAggreGateCommand ans)
        {
            Logger.getLogger(Log.CLIENTS).debug("Removing listener for event '" + name + "' of context '" + context +
                                                "'");
            var cel = createListener(listener);

            removeMaskListener(context, name, cel);
            ans.constructReply(id, AggreGateCommand.REPLY_CODE_OK);
        }
        public OutgoingAggreGateCommand constructEventCommand(Event anEvent, int?listenerCode)
        {
            var cmd = new OutgoingAggreGateCommand();

            string data = anEvent.getData().encode(this.createClassicEncodingSettings(true));

            cmd.constructEvent(anEvent.getContext(), anEvent.getName(), anEvent.getLevel(), data, anEvent.getId(), anEvent.getCreationtime(), listenerCode);

            cmd.setAsync(true);

            return(cmd);
        }
        public void processMessageStart(IncomingAggreGateCommand cmd, OutgoingAggreGateCommand ans)
        {
            var version = Int32.Parse(cmd.getParameter(AggreGateCommand.INDEX_PROTOCOL_VERSION));

            Logger.getLogger(Log.CLIENTS).debug("Processing start command, client protocol version: " + version);

            if (version == ClientCommandUtils.CLIENT_PROTOCOL_VERSION)
            {
                ans.constructReply(cmd.getId(), AggreGateCommand.REPLY_CODE_OK);
                startMessageReceived = true;
            }
            else
            {
                ans.constructReply(cmd.getId(), AggreGateCommand.REPLY_CODE_DENIED);
            }
        }
        public void processOperationGetVar(String id, Context con, String name, OutgoingAggreGateCommand ans)
        {
            var vd = con.getVariableDefinition(name);

            if (vd == null)
            {
                ans.constructReply(id, AggreGateCommand.REPLY_CODE_DENIED, Cres.get().getString("conVarNotAvail") + name);
                return;
            }

            var result = con.getVariable(name, callerController);

            ans.constructReply(id, AggreGateCommand.REPLY_CODE_OK);


            ans.addParam(result.encode(createClassicEncodingSettings(vd.getFormat() != null)));
        }
        public void processOperationSetVar(String id, Context con, String name, String encodedValue,
                                           OutgoingAggreGateCommand ans)
        {
            var vd = con.getVariableDefinition(name);

            if (vd == null)
            {
                ans.constructReply(id, AggreGateCommand.REPLY_CODE_DENIED, Cres.get().getString("conVarNotAvail") + name);
                return;
            }

            var settings = new ClassicEncodingSettings(false, vd.getFormat());

            var value = new DataTable(encodedValue, settings, true);

            con.setVariable(name, callerController, value);

            ans.constructReply(id, AggreGateCommand.REPLY_CODE_OK);
        }
        public void processOperationCallFunction(String id, Context con, String name, String encodedParameters,
                                                 OutgoingAggreGateCommand ans)
        {
            Logger.getLogger(Log.CLIENTS).debug("Calling function '" + name + "' of context '" + con.getPath() + "'");

            var fd = con.getFunctionDefinition(name);

            if (fd == null)
            {
                ans.constructReply(id, AggreGateCommand.REPLY_CODE_DENIED, Cres.get().getString("conFuncNotAvail") + name);
                return;
            }

            var settings = new ClassicEncodingSettings(false, fd.getInputFormat());

            var parameters = new DataTable(encodedParameters, settings, true);

            var result = con.callFunction(name, callerController, parameters);

            ans.constructReply(id, AggreGateCommand.REPLY_CODE_OK);
            ans.addParam(result.encode(createClassicEncodingSettings(fd.getOutputFormat() != null)));
        }
        public OutgoingAggreGateCommand processCommand(IncomingAggreGateCommand cmd)
        {
            var ans = new OutgoingAggreGateCommand();

            try
            {
                var commandCode = cmd.getParameter(AggreGateCommand.INDEX_COMMAND_CODE);

                if (commandCode.Length > 1)
                {
                    throw new AggreGateException(Cres.get().getString("clInvalidCmdCode") + commandCode);
                }

                switch (commandCode[0])
                {
                case AggreGateCommand.COMMAND_CODE_MESSAGE:
                    processMessage(cmd, ans);
                    break;

                default:
                    throw new AggreGateException(Cres.get().getString("clUnknownCmdCode") + commandCode[0]);
                }
            }
            catch (ContextSecurityException ex)
            {
                Logger.getLogger(Log.CLIENTS).info("Access denied while processing command '" + cmd + "': ", ex);
                ans.constructReply(cmd.getId(), AggreGateCommand.REPLY_CODE_DENIED);
            }
            catch (Exception ex)
            {
                Logger.getLogger(Log.CLIENTS).info("Error processing command '" + cmd + "': ", ex);
                ans.constructReply(cmd.getId(), AggreGateCommand.REPLY_CODE_ERROR, ex.Message ?? ex.ToString(),
                                   getErrorDetails(ex));
            }
            return(ans);
        }
 public IncomingAggreGateCommand sendCommand(OutgoingAggreGateCommand cmd)
 {
     connect();
     return(processor.sendSyncCommand(cmd));
 }
 protected abstract void send(OutgoingAggreGateCommand cmd);
 public void sendCommand(OutgoingAggreGateCommand cmd)
 {
     cmd.send(streamWrapper);
 }
        protected virtual void processMessageOperation(IncomingAggreGateCommand cmd, OutgoingAggreGateCommand ans)
        {
            var operation = cmd.getParameter(AggreGateCommand.INDEX_OPERATION_CODE);
            var context   = cmd.getParameter(AggreGateCommand.INDEX_OPERATION_CONTEXT);
            var target    = cmd.getParameter(AggreGateCommand.INDEX_OPERATION_TARGET);

            if (operation.Length > 1)
            {
                throw new SyntaxErrorException(Cres.get().getString("clInvalidOpcode") + operation);
            }

            Logger.getLogger(Log.CLIENTS).debug("Processing message, context '" + context + "', target '" + target +
                                                "', operation '" + operation + "'");

            switch (operation[0])
            {
            case AggreGateCommand.COMMAND_OPERATION_ADD_EVENT_LISTENER:
                var listenerStr = cmd.getParameter(AggreGateCommand.INDEX_OPERATION_LISTENER);
                var listener    = listenerStr.Length > 0 ? (Int32?)Int32.Parse(listenerStr) : null;
                processOperationAddEventListener(cmd.getId(), context, target, listener, ans);
                return;

            case AggreGateCommand.COMMAND_OPERATION_REMOVE_EVENT_LISTENER:
                listenerStr = cmd.getParameter(AggreGateCommand.INDEX_OPERATION_LISTENER);
                listener    = listenerStr.Length > 0 ? (Int32?)Int32.Parse(listenerStr) : null;
                processOperationRemoveEventListener(cmd.getId(), context, target, listener, ans);
                return;
            }

            var con = contextManager.get(context, callerController);

            if (con == null)
            {
                throw new ContextException(Cres.get().getString("conNotAvail") + context);
            }

            if (addNormalListener(con.getPath(), AbstractContext.E_DESTROYED, defaultEventListener))
            {
                addNormalListener(con.getPath(), AbstractContext.E_CHILD_ADDED, defaultEventListener);
                addNormalListener(con.getPath(), AbstractContext.E_CHILD_REMOVED, defaultEventListener);
                addNormalListener(con.getPath(), AbstractContext.E_VARIABLE_ADDED, defaultEventListener);
                addNormalListener(con.getPath(), AbstractContext.E_VARIABLE_REMOVED, defaultEventListener);
                addNormalListener(con.getPath(), AbstractContext.E_FUNCTION_ADDED, defaultEventListener);
                addNormalListener(con.getPath(), AbstractContext.E_FUNCTION_REMOVED, defaultEventListener);
                addNormalListener(con.getPath(), AbstractContext.E_EVENT_ADDED, defaultEventListener);
                addNormalListener(con.getPath(), AbstractContext.E_EVENT_REMOVED, defaultEventListener);
                addNormalListener(con.getPath(), AbstractContext.E_INFO_CHANGED, defaultEventListener);

                addCustomListeners(con);
            }

            switch (operation[0])
            {
            case AggreGateCommand.COMMAND_OPERATION_GET_VAR:
                processOperationGetVar(cmd.getId(), con, target, ans);
                break;

            case AggreGateCommand.COMMAND_OPERATION_SET_VAR:
                processOperationSetVar(cmd.getId(), con, target, cmd.getEncodedDataTableFromOperationMessage(), ans);
                break;

            case AggreGateCommand.COMMAND_OPERATION_CALL_FUNCTION:
                processOperationCallFunction(cmd.getId(), con, target, cmd.getEncodedDataTableFromOperationMessage(),
                                             ans);
                break;

            default:
                throw new SyntaxErrorException(Cres.get().getString("clUnknownOpcode") + operation[0]);
            }
        }
Пример #15
0
 protected override void send(OutgoingAggreGateCommand cmd)
 {
     cmd.send(streamWrapper);
 }
Пример #16
0
        protected override void processMessageOperation(IncomingAggreGateCommand cmd, OutgoingAggreGateCommand ans)
        {
            base.processMessageOperation(cmd, ans);

            var context = cmd.getParameter(AggreGateCommand.INDEX_OPERATION_CONTEXT);

            var conManager = getContextManager();
            var con        = conManager.get(context, getCallerController());

            if (con != null)
            {
                addNormalListener(con.getPath(), AbstractContext.E_UPDATED, getDefaultEventListener());
            }
        }