View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.hbase.hbtop.screen;
19  
20  import java.io.Closeable;
21  import java.io.IOException;
22  import java.util.concurrent.TimeUnit;
23  
24  import org.apache.commons.logging.Log;
25  import org.apache.commons.logging.LogFactory;
26  import org.apache.hadoop.conf.Configuration;
27  import org.apache.hadoop.hbase.classification.InterfaceAudience;
28  import org.apache.hadoop.hbase.client.Admin;
29  import org.apache.hadoop.hbase.client.Connection;
30  import org.apache.hadoop.hbase.client.ConnectionFactory;
31  import org.apache.hadoop.hbase.hbtop.mode.Mode;
32  import org.apache.hadoop.hbase.hbtop.screen.top.TopScreenView;
33  import org.apache.hadoop.hbase.hbtop.terminal.KeyPress;
34  import org.apache.hadoop.hbase.hbtop.terminal.Terminal;
35  import org.apache.hadoop.hbase.hbtop.terminal.impl.TerminalImpl;
36  
37  /**
38   * This dispatches key presses and timers to the current {@link ScreenView}.
39   */
40  @InterfaceAudience.Private
41  public class Screen implements Closeable {
42  
43    private static final Log LOG = LogFactory.getLog(Screen.class);
44  
45    private static final long SLEEP_TIMEOUT_MILLISECONDS = 100;
46  
47    private final Connection connection;
48    private final Admin admin;
49    private final Terminal terminal;
50  
51    private ScreenView currentScreenView;
52    private Long timerTimestamp;
53  
54    public Screen(Configuration conf, long initialRefreshDelay, Mode initialMode)
55      throws IOException {
56      connection = ConnectionFactory.createConnection(conf);
57      admin = connection.getAdmin();
58  
59      // The first screen is the top screen
60      this.terminal = new TerminalImpl("hbtop");
61      currentScreenView = new TopScreenView(this, terminal, initialRefreshDelay, admin,
62        initialMode);
63    }
64  
65    @Override
66    public void close() throws IOException {
67      try {
68        admin.close();
69      } finally {
70        try {
71          connection.close();
72        } finally {
73          terminal.close();
74        }
75      }
76    }
77  
78    public void run() {
79      currentScreenView.init();
80      while (true) {
81        try {
82          KeyPress keyPress = terminal.pollKeyPress();
83  
84          ScreenView nextScreenView;
85          if (keyPress != null) {
86            // Dispatch the key press to the current screen
87            nextScreenView = currentScreenView.handleKeyPress(keyPress);
88          } else {
89            if (timerTimestamp != null) {
90              long now = System.currentTimeMillis();
91              if (timerTimestamp <= now) {
92                // Dispatch the timer to the current screen
93                timerTimestamp = null;
94                nextScreenView = currentScreenView.handleTimer();
95              } else {
96                if (timerTimestamp - now < SLEEP_TIMEOUT_MILLISECONDS) {
97                  TimeUnit.MILLISECONDS.sleep(timerTimestamp - now);
98                } else {
99                  TimeUnit.MILLISECONDS.sleep(SLEEP_TIMEOUT_MILLISECONDS);
100               }
101               continue;
102             }
103           } else {
104             TimeUnit.MILLISECONDS.sleep(SLEEP_TIMEOUT_MILLISECONDS);
105             continue;
106           }
107         }
108 
109         // If the next screen is null, then exit
110         if (nextScreenView == null) {
111           return;
112         }
113 
114         // If the next screen is not the previous, then go to the next screen
115         if (nextScreenView != currentScreenView) {
116           currentScreenView = nextScreenView;
117           currentScreenView.init();
118         }
119       } catch (Exception e) {
120         LOG.error("Caught an exception", e);
121       }
122     }
123   }
124 
125   public void setTimer(long delay) {
126     timerTimestamp = System.currentTimeMillis() + delay;
127   }
128 
129   public void cancelTimer() {
130     timerTimestamp = null;
131   }
132 }