1 /* 2 * Copyright (C) 2009 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 17 package com.android.dumprendertree.forwarder; 18 19 import java.io.BufferedReader; 20 import java.io.File; 21 import java.io.FileReader; 22 import java.io.IOException; 23 24 import android.os.Environment; 25 import android.util.Log; 26 27 public class ForwardService { 28 29 private ForwardServer fs18000, fs8080, fs8443; 30 31 private static ForwardService inst; 32 33 private static final String LOGTAG = "ForwardService"; 34 35 private static final String DEFAULT_TEST_HOST = "android-browser-test.mtv.corp.google.com"; 36 37 private static final String FORWARD_HOST_CONF = 38 Environment.getExternalStorageDirectory() + "/drt_forward_host.txt"; 39 ForwardService()40 private ForwardService() { 41 int addr = getForwardHostAddr(); 42 if (addr != -1) { 43 fs18000 = new ForwardServer(18000, addr, 8000); 44 fs8080 = new ForwardServer(8080, addr, 8080); 45 fs8443 = new ForwardServer(8443, addr, 8443); 46 } 47 } 48 getForwardService()49 public static ForwardService getForwardService() { 50 if (inst == null) { 51 inst = new ForwardService(); 52 } 53 return inst; 54 } 55 startForwardService()56 public void startForwardService() { 57 try { 58 if (fs18000 != null) 59 fs18000.start(); 60 if (fs8080 != null) 61 fs8080.start(); 62 if (fs8443 != null) 63 fs8443.start(); 64 } catch (IOException ioe) { 65 Log.w(LOGTAG, "failed to start forwarder. http tests will fail.", ioe); 66 return; 67 } 68 } 69 stopForwardService()70 public void stopForwardService() { 71 if (fs18000 != null) { 72 fs18000.stop(); 73 fs18000 = null; 74 } 75 if (fs8080 != null) { 76 fs8080.stop(); 77 fs8080 = null; 78 } 79 if (fs8443 != null) { 80 fs8443.stop(); 81 fs8443 = null; 82 } 83 Log.v(LOGTAG, "forwarders stopped."); 84 } 85 getForwardHostAddr()86 private static int getForwardHostAddr() { 87 int addr = -1; 88 String host = null; 89 File forwardHostConf = new File(FORWARD_HOST_CONF); 90 if (forwardHostConf.isFile()) { 91 BufferedReader hostReader = null; 92 try { 93 hostReader = new BufferedReader(new FileReader(forwardHostConf)); 94 host = hostReader.readLine(); 95 Log.v(LOGTAG, "read forward host from file: " + host); 96 } catch (IOException ioe) { 97 Log.v(LOGTAG, "cannot read forward host from file", ioe); 98 } finally { 99 if (hostReader != null) { 100 try { 101 hostReader.close(); 102 } catch (IOException ioe) { 103 // burn!!! 104 } 105 } 106 } 107 } 108 if (host == null || host.length() == 0) 109 host = DEFAULT_TEST_HOST; 110 try { 111 addr = AdbUtils.resolve(host); 112 } catch (IOException ioe) { 113 Log.e(LOGTAG, "failed to resolve server address", ioe); 114 } 115 return addr; 116 } 117 } 118