1 /* 2 * Copyright (C) 2018 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package android.car.cluster; 17 import android.os.Handler; 18 import android.os.Message; 19 import android.util.Log; 20 21 /** 22 * This class serves as a template for sending to specific clients of the broadcaster. 23 */ 24 public abstract class SenderThread extends Thread { 25 private static final String TAG = "Cluster.SenderThread"; 26 27 private Handler mHandler; 28 SenderThread(Handler handler)29 SenderThread(Handler handler) { 30 mHandler = handler; 31 } 32 send(byte[] buf, int len)33 abstract void send(byte[] buf, int len); close()34 abstract void close(); 35 36 /** 37 * Tells the broadcasting thread to stop and close everything in progress, and start over again. 38 * It will kill the current instance of this thread, and produce a new one. 39 */ restart()40 synchronized void restart() { 41 if (mHandler.hasMessages(NetworkedVirtualDisplay.MSG_START)) return; 42 Log.i(TAG, "Sending STOP and START msgs to NetworkedVirtualDisplay"); 43 44 mHandler.sendMessage(Message.obtain(mHandler, NetworkedVirtualDisplay.MSG_STOP)); 45 mHandler.sendMessage(Message.obtain(mHandler, NetworkedVirtualDisplay.MSG_START)); 46 } 47 } 48