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