/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.hadoop.mapred;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.mapreduce.test.system.TTTaskInfo;
/**
 * Abstract class which passes the Task view of the TaskTracker to the client.
 * See {@link TTInfoImpl} for further details.
 *
 */
abstract class TTTaskInfoImpl implements TTTaskInfo {

  private boolean slotTaken;
  private boolean wasKilled;
  TaskStatus status;
  Configuration conf;
  String user;
  boolean isTaskCleanupTask;
  private String pid;

  public TTTaskInfoImpl() {
  }

  public TTTaskInfoImpl(boolean slotTaken, boolean wasKilled,
      TaskStatus status, Configuration conf, String user,
      boolean isTaskCleanupTask, String pid) {
    super();
    this.slotTaken = slotTaken;
    this.wasKilled = wasKilled;
    this.status = status;
    this.conf = conf;
    this.user = user;
    this.isTaskCleanupTask = isTaskCleanupTask;
    this.pid = pid;
  }

  @Override
  public boolean slotTaken() {
    return slotTaken;
  }

  @Override
  public boolean wasKilled() {
    return wasKilled;
  }

  @Override
  public abstract TaskStatus getTaskStatus();

  @Override
  public Configuration getConf() {
    return conf;
  }
  
  @Override
  public String getUser() {
    return user;
  }
  
  @Override
  public boolean isTaskCleanupTask() {
    return isTaskCleanupTask;
  }
  
  @Override
  public String getPid() {
    return pid;
  }
  
  @Override
  public void readFields(DataInput in) throws IOException {
    slotTaken = in.readBoolean();
    wasKilled = in.readBoolean();
    conf = new Configuration();
    conf.readFields(in);
    user = in.readUTF();
    isTaskCleanupTask = in.readBoolean();
    pid = in.readUTF();
  }

  @Override
  public void write(DataOutput out) throws IOException {
    out.writeBoolean(slotTaken);
    out.writeBoolean(wasKilled);
    conf.write(out);
    out.writeUTF(user);
    out.writeBoolean(isTaskCleanupTask);
    if (pid != null) {
      out.writeUTF(pid);
    } else {
      out.writeUTF("");
    }
    status.write(out);
  }

  static class MapTTTaskInfo extends TTTaskInfoImpl {

    public MapTTTaskInfo() {
      super();
    }

    public MapTTTaskInfo(boolean slotTaken, boolean wasKilled,
        MapTaskStatus status, Configuration conf, String user,
        boolean isTaskCleanup,String pid) {
      super(slotTaken, wasKilled, status, conf, user, isTaskCleanup, pid);
    }

    @Override
    public TaskStatus getTaskStatus() {
      return status;
    }
    
    public void readFields(DataInput in) throws IOException {
      super.readFields(in);
      status = new MapTaskStatus();
      status.readFields(in);
    }
  }

  static class ReduceTTTaskInfo extends TTTaskInfoImpl {

    public ReduceTTTaskInfo() {
      super();
    }

    public ReduceTTTaskInfo(boolean slotTaken, boolean wasKilled,
        ReduceTaskStatus status, Configuration conf, String user,
        boolean isTaskCleanup, String pid) {
      super(slotTaken, wasKilled, status, conf, user, isTaskCleanup, pid);
    }

    @Override
    public TaskStatus getTaskStatus() {
      return status;
    }
    
    public void readFields(DataInput in) throws IOException {
      super.readFields(in);
      status = new ReduceTaskStatus();
      status.readFields(in);
    }
  }

}
