Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
SampleVmServiceListener.java
Go to the documentation of this file.
1/*
2 * Copyright (c) 2015, the Dart project authors.
3 *
4 * Licensed under the Eclipse Public License v1.0 (the "License"); you may not use this file except
5 * in compliance with the License. You may obtain a copy of the License at
6 *
7 * http://www.eclipse.org/legal/epl-v10.html
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the License
10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11 * or implied. See the License for the specific language governing permissions and limitations under
12 * the License.
13 */
14package org.dartlang.vm.service;
15
16import org.dartlang.vm.service.element.Event;
17import org.dartlang.vm.service.element.EventKind;
18
19import java.util.Set;
20
21/**
22 * Sample VmListener for responding to state changes in the running application
23 */
25 private final Object lock = new Object();
26 private String lastStreamId;
27 private Event lastEvent;
28 private final Set<EventKind> ignoreAll;
29
30 SampleVmServiceListener(Set<EventKind> ignoreAll) {
31 this.ignoreAll = ignoreAll;
32 }
33
34 @Override
35 public void connectionOpened() {
36
37 }
38
39 @Override
40 public void received(String streamId, Event event) {
41 synchronized (lock) {
42 if (ignoreAll.contains(event.getKind())) {
43 return;
44 }
45 if (lastStreamId != null) {
46 unexpectedEvent(lastStreamId, lastEvent);
47 }
48 lastStreamId = streamId;
49 lastEvent = event;
50 lock.notifyAll();
51 }
52 }
53
54 @Override
55 public void connectionClosed() {
56
57 }
58
59 public Event waitFor(String expectedStreamId, EventKind expectedEventKind) {
60 long end = System.currentTimeMillis() + 5000;
61 synchronized (lock) {
62 while (true) {
63 if (expectedStreamId.equals(lastStreamId) && expectedEventKind.equals(lastEvent.getKind())) {
64 Event event = lastEvent;
65 lastStreamId = null;
66 lastEvent = null;
67 return event;
68 }
69 long timeout = end - System.currentTimeMillis();
70 if (timeout <= 0) {
71 break;
72 }
73 try {
74 lock.wait(timeout);
75 } catch (InterruptedException e) {
76 // ignored
77 }
78 }
79 }
80 throw new RuntimeException("Expected event: " + expectedStreamId + ", " + expectedEventKind);
81 }
82
83 private void unexpectedEvent(String streamId, Event event) {
84 System.out.println("****** Unexpected Event: " + streamId + ", " + event.getKind());
85 }
86}
Event waitFor(String expectedStreamId, EventKind expectedEventKind)
glong glong end
FlKeyEvent * event