1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
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
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
87 nextScreenView = currentScreenView.handleKeyPress(keyPress);
88 } else {
89 if (timerTimestamp != null) {
90 long now = System.currentTimeMillis();
91 if (timerTimestamp <= now) {
92
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
110 if (nextScreenView == null) {
111 return;
112 }
113
114
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 }