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.runtime.common.driver.client; 020 021import com.google.protobuf.ByteString; 022import org.apache.reef.annotations.audience.DriverSide; 023import org.apache.reef.proto.ReefServiceProtos; 024import org.apache.reef.runtime.common.driver.parameters.JobIdentifier; 025import org.apache.reef.tang.annotations.Parameter; 026 027import javax.inject.Inject; 028import java.util.logging.Level; 029import java.util.logging.Logger; 030 031/** 032 * Represents the communication channel to the client. 033 */ 034@DriverSide 035public final class ClientConnection { 036 037 private static final Logger LOG = Logger.getLogger(ClientConnection.class.getName()); 038 039 private final JobStatusHandler jobStatusHandler; 040 private final String jobIdentifier; 041 042 @Inject 043 public ClientConnection( 044 @Parameter(JobIdentifier.class) final String jobIdentifier, 045 final JobStatusHandler jobStatusHandler) { 046 047 this.jobIdentifier = jobIdentifier; 048 this.jobStatusHandler = jobStatusHandler; 049 } 050 051 /** 052 * Send the given JobStatus to the client. 053 * 054 * @param status 055 */ 056 public synchronized void send(final ReefServiceProtos.JobStatusProto status) { 057 LOG.log(Level.FINEST, "Sending to client: status={0}", status.getState()); 058 this.jobStatusHandler.onNext(status); 059 } 060 061 /** 062 * Send the given byte[] as a message to the client. 063 * 064 * @param message 065 */ 066 public synchronized void sendMessage(final byte[] message) { 067 this.send(ReefServiceProtos.JobStatusProto.newBuilder() 068 .setIdentifier(this.jobIdentifier) 069 .setState(ReefServiceProtos.State.RUNNING) 070 .setMessage(ByteString.copyFrom(message)) 071 .build()); 072 } 073} 074