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.ClientRemoteIdentifier; 025import org.apache.reef.runtime.common.driver.parameters.JobIdentifier; 026import org.apache.reef.runtime.common.utils.RemoteManager; 027import org.apache.reef.tang.annotations.Parameter; 028import org.apache.reef.wake.EventHandler; 029 030import javax.inject.Inject; 031import java.util.logging.Level; 032import java.util.logging.Logger; 033 034/** 035 * Represents the communication channel to the client. 036 */ 037@DriverSide 038public final class ClientConnection { 039 040 private static final Logger LOG = Logger.getLogger(ClientConnection.class.getName()); 041 042 private final EventHandler<ReefServiceProtos.JobStatusProto> jobStatusHandler; 043 private final String jobIdentifier; 044 045 @Inject 046 public ClientConnection( 047 final RemoteManager remoteManager, 048 @Parameter(ClientRemoteIdentifier.class) final String clientRID, 049 @Parameter(JobIdentifier.class) final String jobIdentifier) { 050 this.jobIdentifier = jobIdentifier; 051 if (clientRID.equals(ClientRemoteIdentifier.NONE)) { 052 LOG.log(Level.FINE, "Instantiated 'ClientConnection' without an actual connection to the client."); 053 this.jobStatusHandler = new LoggingJobStatusHandler(); 054 } else { 055 this.jobStatusHandler = remoteManager.getHandler(clientRID, ReefServiceProtos.JobStatusProto.class); 056 LOG.log(Level.FINE, "Instantiated 'ClientConnection'"); 057 } 058 } 059 060 /** 061 * Send the given JobStatus to the client. 062 * 063 * @param status 064 */ 065 public synchronized void send(final ReefServiceProtos.JobStatusProto status) { 066 LOG.log(Level.FINEST, "Sending:\n" + status); 067 this.jobStatusHandler.onNext(status); 068 } 069 070 /** 071 * Send the given byte[] as a message to the client. 072 * 073 * @param message 074 */ 075 public synchronized void sendMessage(final byte[] message) { 076 this.send(ReefServiceProtos.JobStatusProto.newBuilder() 077 .setIdentifier(this.jobIdentifier) 078 .setState(ReefServiceProtos.State.RUNNING) 079 .setMessage(ByteString.copyFrom(message)) 080 .build()); 081 } 082} 083