This project has retired. For details please refer to its Attic page.
Source code
001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.reef.examples.suspend;
020
021import org.apache.reef.tang.Configuration;
022import org.apache.reef.tang.Injector;
023import org.apache.reef.tang.JavaConfigurationBuilder;
024import org.apache.reef.tang.Tang;
025import org.apache.reef.tang.annotations.Name;
026import org.apache.reef.tang.annotations.NamedParameter;
027import org.apache.reef.tang.annotations.Parameter;
028import org.apache.reef.tang.exceptions.BindException;
029import org.apache.reef.tang.formats.CommandLine;
030import org.apache.reef.wake.EStage;
031import org.apache.reef.wake.EventHandler;
032import org.apache.reef.wake.impl.LoggingEventHandler;
033import org.apache.reef.wake.impl.ThreadPoolStage;
034import org.apache.reef.wake.remote.impl.ObjectSerializableCodec;
035import org.apache.reef.wake.remote.impl.TransportEvent;
036import org.apache.reef.wake.remote.transport.Link;
037import org.apache.reef.wake.remote.transport.Transport;
038import org.apache.reef.wake.remote.transport.netty.NettyMessagingTransport;
039
040import javax.inject.Inject;
041import java.io.IOException;
042import java.net.InetSocketAddress;
043import java.util.logging.Level;
044import java.util.logging.Logger;
045
046public final class Control {
047
048  private static final Logger LOG = Logger.getLogger(Control.class.getName());
049  private final transient String command;
050  private final transient String taskId;
051  private final transient int port;
052
053  @Inject
054  public Control(@Parameter(SuspendClientControl.Port.class) final int port,
055                 @Parameter(TaskId.class) final String taskId,
056                 @Parameter(Command.class) final String command) {
057    this.command = command.trim().toLowerCase();
058    this.taskId = taskId;
059    this.port = port;
060  }
061
062  private static Configuration getConfig(final String[] args) throws IOException, BindException {
063    final JavaConfigurationBuilder cb = Tang.Factory.getTang().newConfigurationBuilder();
064    new CommandLine(cb).processCommandLine(args, SuspendClientControl.Port.class, TaskId.class, Command.class);
065    return cb.build();
066  }
067
068  public static void main(final String[] args) throws Exception {
069    final Configuration config = getConfig(args);
070    final Injector injector = Tang.Factory.getTang().newInjector(config);
071    final Control control = injector.getInstance(Control.class);
072    control.run();
073  }
074
075  public void run() throws Exception {
076
077    LOG.log(Level.INFO, "command: {0} task: {1} port: {2}",
078        new Object[]{this.command, this.taskId, this.port});
079
080    final ObjectSerializableCodec<String> codec = new ObjectSerializableCodec<>();
081
082    final EStage<TransportEvent> stage = new ThreadPoolStage<>("suspend-control-client",
083        new LoggingEventHandler<TransportEvent>(), 1, new EventHandler<Throwable>() {
084      @Override
085      public void onNext(final Throwable throwable) {
086        throw new RuntimeException(throwable);
087      }
088    });
089
090    try (final Transport transport = new NettyMessagingTransport("localhost", 0, stage, stage, 1, 10000)) {
091      final Link link = transport.open(new InetSocketAddress("localhost", this.port), codec, null);
092      link.write(this.command + " " + this.taskId);
093    }
094  }
095
096  @NamedParameter(doc = "Task id", short_name = "task")
097  public static final class TaskId implements Name<String> {
098  }
099
100  @NamedParameter(doc = "Command: 'suspend' or 'resume'", short_name = "cmd")
101  public static final class Command implements Name<String> {
102  }
103}