@Override public synchronized void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception { channel = ctx.getChannel(); List<Command<K, V, ?>> tmp = new ArrayList<Command<K, V, ?>>(queue.size() + 2); if (password != null) { CommandArgs<K, V> args = new CommandArgs<K, V>(codec).add(password); tmp.add(new Command<K, V, String>(AUTH, new StatusOutput<K, V>(codec), args, false)); } if (db != 0) { CommandArgs<K, V> args = new CommandArgs<K, V>(codec).add(db); tmp.add(new Command<K, V, String>(SELECT, new StatusOutput<K, V>(codec), args, false)); } tmp.addAll(queue); queue.clear(); for (Command<K, V, ?> cmd : tmp) { if (!cmd.isCancelled()) { queue.add(cmd); channel.write(cmd); } } tmp.clear(); }
public <T> T await(Command<K, V, T> cmd, long timeout, TimeUnit unit) { if (!cmd.await(timeout, unit)) { cmd.cancel(true); throw new RedisException("Command timed out"); } CommandOutput<K, V, T> output = cmd.getOutput(); if (output.hasError()) throw new RedisException(output.getError()); return output.get(); }
@Override public synchronized void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception { if (closed) { for (Command<K, V, ?> cmd : queue) { cmd.getOutput().setError("Connection closed"); cmd.complete(); } queue.clear(); queue = null; channel = null; } }
public void executeCommand(String commandText) { String[] parts = commandText.split(" "); String commandName = parts[0]; String[] args = new String[parts.length - 1]; for (int i = 1; i < parts.length; i++) args[i - 1] = parts[i]; Command targetCommand = null; synchronized (commands) { for (Command command : commands) if (commandName.equalsIgnoreCase(command.getName())) targetCommand = command; } if (targetCommand == null) log("[BOT] Unknown command."); else if (!targetCommand.execute(this, args)) log("[BOT] Invalid command usage."); }
private <TRequest> ClientCommandExecutor createExecutor( Membership.Member member, Command<TRequest> command) { CommandExecutor commandHandler = null; for (CommandExecutor ch : commandHandlers) { if (ch.canHandle(command.getType())) commandHandler = ch; } return new UdpClientCommandExecutor<>(member, command, commandHandler, logger); }
public void run() { try { for (; ; ) { final Pair<Handler, Message> entry = eventQueue.take(); final Handler handler = entry.left; final Message message = entry.right; try { // A message is either a command or an event. // A command returns a value that must be read by // the caller. if (message instanceof Command<?>) { Command<?> command = (Command<?>) message; try { Locus.push(command.getLocus()); Object result = command.call(); responseQueue.put(command, Pair.of(result, (Throwable) null)); } catch (PleaseShutdownException e) { responseQueue.put(command, Pair.of(null, (Throwable) null)); return; // exit event loop } catch (Throwable e) { responseQueue.put(command, Pair.of(null, e)); } finally { Locus.pop(command.getLocus()); } } else { Event event = (Event) message; event.acceptWithoutResponse(handler); // Broadcast the event to anyone who is interested. RolapUtil.MONITOR_LOGGER.debug(message); } } catch (Throwable e) { // REVIEW: Somewhere better to send it? e.printStackTrace(); } } } catch (InterruptedException e) { // REVIEW: Somewhere better to send it? e.printStackTrace(); } catch (Throwable e) { e.printStackTrace(); } }