diff options
Diffstat (limited to 'plugins/org.yocto.bc.ui/src/org/yocto/bc/remote/utils/YoctoRunnableWithProgress.java')
-rw-r--r-- | plugins/org.yocto.bc.ui/src/org/yocto/bc/remote/utils/YoctoRunnableWithProgress.java | 220 |
1 files changed, 220 insertions, 0 deletions
diff --git a/plugins/org.yocto.bc.ui/src/org/yocto/bc/remote/utils/YoctoRunnableWithProgress.java b/plugins/org.yocto.bc.ui/src/org/yocto/bc/remote/utils/YoctoRunnableWithProgress.java new file mode 100644 index 0000000..9654cb3 --- /dev/null +++ b/plugins/org.yocto.bc.ui/src/org/yocto/bc/remote/utils/YoctoRunnableWithProgress.java | |||
@@ -0,0 +1,220 @@ | |||
1 | /******************************************************************************* | ||
2 | * Copyright (c) 2013 Intel Corporation. | ||
3 | * All rights reserved. This program and the accompanying materials | ||
4 | * are made available under the terms of the Eclipse Public License v1.0 | ||
5 | * which accompanies this distribution, and is available at | ||
6 | * http://www.eclipse.org/legal/epl-v10.html | ||
7 | * | ||
8 | * Contributors: | ||
9 | * Ioana Grigoropol(Intel) - initial API and implementation | ||
10 | *******************************************************************************/ | ||
11 | package org.yocto.bc.remote.utils; | ||
12 | |||
13 | import java.io.IOException; | ||
14 | import java.lang.reflect.InvocationTargetException; | ||
15 | import java.util.regex.Matcher; | ||
16 | import java.util.regex.Pattern; | ||
17 | |||
18 | import org.eclipse.core.runtime.CoreException; | ||
19 | import org.eclipse.core.runtime.IProgressMonitor; | ||
20 | import org.eclipse.core.runtime.NullProgressMonitor; | ||
21 | import org.eclipse.jface.operation.IRunnableWithProgress; | ||
22 | import org.eclipse.ptp.remote.core.IRemoteConnection; | ||
23 | import org.eclipse.ptp.remote.core.IRemoteServices; | ||
24 | import org.eclipse.ptp.remote.core.exception.RemoteConnectionException; | ||
25 | import org.eclipse.rse.core.model.IHost; | ||
26 | import org.eclipse.rse.services.shells.IHostShell; | ||
27 | import org.eclipse.swt.widgets.Display; | ||
28 | import org.yocto.remote.utils.CommandResponseHandler; | ||
29 | import org.yocto.remote.utils.OutputProcessor; | ||
30 | import org.yocto.remote.utils.RemoteHelper; | ||
31 | import org.yocto.remote.utils.YoctoCommand; | ||
32 | |||
33 | public class YoctoRunnableWithProgress implements IRunnableWithProgress { | ||
34 | |||
35 | private String taskName; | ||
36 | private IRemoteConnection remoteConnection; | ||
37 | private IRemoteServices remoteServices; | ||
38 | private IProgressMonitor monitor; | ||
39 | private final ICalculatePercentage calculator; | ||
40 | private int reportedWorkload; | ||
41 | |||
42 | private final YoctoCommand command; | ||
43 | |||
44 | public YoctoRunnableWithProgress(YoctoCommand command) throws IOException { | ||
45 | this.command = command; | ||
46 | this.calculator = new GitCalculatePercentage(); | ||
47 | } | ||
48 | |||
49 | private interface ICalculatePercentage { | ||
50 | public float calWorkloadDone(String info) throws IllegalArgumentException; | ||
51 | } | ||
52 | |||
53 | private class GitCalculatePercentage implements ICalculatePercentage { | ||
54 | final Pattern pattern = Pattern.compile("^Receiving objects:\\s*(\\d+)%.*"); | ||
55 | @Override | ||
56 | public float calWorkloadDone(String info) throws IllegalArgumentException { | ||
57 | Matcher m = pattern.matcher(info.trim()); | ||
58 | if(m.matches()) { | ||
59 | return new Float(m.group(1)) / 100; | ||
60 | }else { | ||
61 | throw new IllegalArgumentException(); | ||
62 | } | ||
63 | } | ||
64 | } | ||
65 | |||
66 | @Override | ||
67 | public void run(IProgressMonitor monitor) throws InvocationTargetException, | ||
68 | InterruptedException { | ||
69 | try { | ||
70 | this.monitor = monitor; | ||
71 | this.monitor.beginTask(taskName, RemoteHelper.TOTALWORKLOAD); | ||
72 | |||
73 | if (!remoteConnection.isOpen()) { | ||
74 | try { | ||
75 | remoteConnection.open(monitor); | ||
76 | } catch (RemoteConnectionException e1) { | ||
77 | e1.printStackTrace(); | ||
78 | } | ||
79 | } | ||
80 | |||
81 | remoteServices.initialize(new NullProgressMonitor()); | ||
82 | |||
83 | try { | ||
84 | IHost connection = RemoteHelper.getRemoteConnectionByName(remoteConnection.getName()); | ||
85 | YoctoThread th = new YoctoThread(connection, command); | ||
86 | th.run(); | ||
87 | } catch (Exception e) { | ||
88 | e.printStackTrace(); | ||
89 | } finally { | ||
90 | monitor.done(); | ||
91 | } | ||
92 | } catch (Exception e) { | ||
93 | e.printStackTrace(); | ||
94 | } | ||
95 | } | ||
96 | class YoctoRunnableOutputProcessor extends OutputProcessor{ | ||
97 | |||
98 | public YoctoRunnableOutputProcessor(IProgressMonitor monitor, | ||
99 | IHostShell hostShell, CommandResponseHandler cmdHandler, | ||
100 | String task) { | ||
101 | super(monitor, hostShell, cmdHandler, task); | ||
102 | } | ||
103 | @Override | ||
104 | protected boolean isErrChStop(char ch) { | ||
105 | return (ch == '\n' || ch == '\r'); | ||
106 | } | ||
107 | |||
108 | @Override | ||
109 | protected boolean isOutChStop(char ch) { | ||
110 | return (ch == '\n'); | ||
111 | } | ||
112 | |||
113 | @Override | ||
114 | protected void processOutputBufferLine(char ch, String str) { | ||
115 | processBuffer.addOutputLine(str); | ||
116 | } | ||
117 | |||
118 | @Override | ||
119 | protected void processErrorBufferLine(char ch, String str) { | ||
120 | processBuffer.addOutputLine(str); | ||
121 | if (ch == '\r') | ||
122 | reportProgress(str); | ||
123 | } | ||
124 | |||
125 | } | ||
126 | |||
127 | class YoctoThread implements Runnable{ | ||
128 | private final IHost connection; | ||
129 | private final YoctoCommand command; | ||
130 | private final CommandResponseHandler cmdHandler; | ||
131 | private IHostShell hostShell; | ||
132 | |||
133 | YoctoThread(IHost connection, YoctoCommand command){ | ||
134 | this.connection = connection; | ||
135 | this.cmdHandler = RemoteHelper.getCommandHandler(connection); | ||
136 | this.command = command; | ||
137 | } | ||
138 | |||
139 | @Override | ||
140 | public void run() { | ||
141 | try { | ||
142 | hostShell = RemoteHelper.runCommandRemote(this.connection, command, monitor); | ||
143 | command.setProcessBuffer(new YoctoRunnableOutputProcessor(monitor, hostShell, cmdHandler, taskName).processOutput()); | ||
144 | } catch (CoreException e) { | ||
145 | e.printStackTrace(); | ||
146 | } catch (Exception e) { | ||
147 | e.printStackTrace(); | ||
148 | } | ||
149 | } | ||
150 | } | ||
151 | private void updateMonitor(final int work){ | ||
152 | |||
153 | Display.getDefault().asyncExec(new Runnable() { | ||
154 | |||
155 | @Override | ||
156 | public void run() { | ||
157 | if (monitor != null) { | ||
158 | monitor.worked(work); | ||
159 | } | ||
160 | } | ||
161 | |||
162 | }); | ||
163 | } | ||
164 | |||
165 | private void doneMonitor(){ | ||
166 | Display.getDefault().asyncExec(new Runnable() { | ||
167 | @Override | ||
168 | public void run() { | ||
169 | monitor.done(); | ||
170 | } | ||
171 | }); | ||
172 | } | ||
173 | |||
174 | public void reportProgress(String info) { | ||
175 | if(calculator == null) { | ||
176 | updateMonitor(1); | ||
177 | } else { | ||
178 | float percentage; | ||
179 | try { | ||
180 | percentage = calculator.calWorkloadDone(info); | ||
181 | } catch (IllegalArgumentException e) { | ||
182 | System.out.println(info); | ||
183 | //can't get percentage | ||
184 | return; | ||
185 | } | ||
186 | int delta = (int) (RemoteHelper.TOTALWORKLOAD * percentage - reportedWorkload); | ||
187 | if( delta > 0 ) { | ||
188 | updateMonitor(delta); | ||
189 | reportedWorkload += delta; | ||
190 | } | ||
191 | |||
192 | if (reportedWorkload == RemoteHelper.TOTALWORKLOAD) | ||
193 | doneMonitor(); | ||
194 | } | ||
195 | } | ||
196 | |||
197 | public IRemoteConnection getRemoteConnection() { | ||
198 | return remoteConnection; | ||
199 | } | ||
200 | |||
201 | public void setRemoteConnection(IRemoteConnection remoteConnection) { | ||
202 | this.remoteConnection = remoteConnection; | ||
203 | } | ||
204 | |||
205 | public String getTaskName() { | ||
206 | return taskName; | ||
207 | } | ||
208 | |||
209 | public void setTaskName(String taskName) { | ||
210 | this.taskName = taskName; | ||
211 | } | ||
212 | |||
213 | public IRemoteServices getRemoteServices() { | ||
214 | return remoteServices; | ||
215 | } | ||
216 | |||
217 | public void setRemoteServices(IRemoteServices remoteServices) { | ||
218 | this.remoteServices = remoteServices; | ||
219 | } | ||
220 | } | ||