Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

106
LINES

< > BotCompany Repo | #1004976 // Sense Chrome Processes ^^

JavaX source code [tags: use-pretranspiled] - run with: x30.jar

Libraryless. Click here for Pure Java version (4167L/28K/92K).

1  
!752
2  
3  
!include #1004681 // Concepts
4  
5  
sconcept Sensor {
6  
  long updatedWhen;
7  
  PersistableThrowable error;
8  
  transient volatile bool updating;
9  
  
10  
  void actionImpl() {} // override this in subclasses
11  
  
12  
  void action() {
13  
    updatedWhen = now();
14  
    updating = true;
15  
    Throwable theError = null;
16  
    try {
17  
      actionImpl();
18  
    } catch e {
19  
      theError = e;
20  
    } finally {
21  
      error = persistableThrowable(theError);
22  
      updating = false;
23  
    }
24  
  }
25  
  
26  
  bool ok() { ret error == null; }
27  
  
28  
  void dependOn(Sensor s) {
29  
    updater.depend(this, s);
30  
  }
31  
  
32  
  void update() {
33  
    updater.update(this);
34  
  }
35  
}
36  
37  
sconcept BooleanSensor extends Sensor {
38  
  bool booleanValue;
39  
}
40  
41  
sclass Updater {
42  
  new HashSet<Sensor> updated;
43  
  
44  
  void depend(Sensor a, Sensor b) {
45  
    update(b);
46  
  }
47  
  
48  
  void update(Sensor s) {
49  
    if (!updated.contains(s)) {
50  
      s.action();
51  
      updated.add(s);
52  
    }
53  
  }
54  
  
55  
  void reset() {
56  
    updated.clear();
57  
  }
58  
}
59  
60  
static new Updater updater;
61  
62  
sconcept RawPSCmd extends Sensor {
63  
  S output;
64  
  O notRunBecause;
65  
  
66  
  void actionImpl() {
67  
    if (isLinux())
68  
      output = backtick("ps -aef");
69  
    else
70  
      notRunBecause = "Not on Linux";
71  
  }
72  
}
73  
74  
// boolean value is true when there is at least 1 chrome process
75  
sconcept PSChrome extends BooleanSensor {
76  
  int chromeProcesses;
77  
  
78  
  void actionImpl() {
79  
    RawPSCmd ps = unary(RawPSCmd.class);
80  
    dependOn(ps);
81  
    if (!ps.ok()) supplierFailure();
82  
    L<S> cmds = psGetCommands(ps.output);
83  
    chromeProcesses = l(indexesOfLinesStartingWith(cmds, "/opt/google/chrome/"));
84  
    booleanValue = chromeProcesses > 0;
85  
  }
86  
}
87  
88  
p {
89  
  typeWriterConsole();
90  
  int last = -1;
91  
  while licensed {
92  
    updater.reset();
93  
    pcall {
94  
      PSChrome chrome = unary(PSChrome.class);
95  
      chrome.update();
96  
      if (!chrome.ok())
97  
        print("Chrome Detection Error: " + chrome.error);
98  
      else if (last != chrome.chromeProcesses) {
99  
        last = chrome.chromeProcesses;
100  
        print("Chrome Processes: " + last);
101  
        setConsoleTitle(n(last, "Chrome Process"));
102  
      }
103  
    }
104  
    sleepSeconds(1);
105  
  }
106  
}

download  show line numbers  debug dex  old transpilations   

Travelled to 14 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, ddnzoavkxhuk, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1004976
Snippet name: Sense Chrome Processes ^^
Eternal ID of this version: #1004976/1
Text MD5: 15d1dc3dd5fb41018bbad9313da83b5e
Transpilation MD5: 6e46b61caee89fac0bd8a3fea98b1e7d
Author: stefan
Category: javax / a.i.
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-09-20 23:04:17
Source code size: 2171 bytes / 106 lines
Pitched / IR pitched: No / No
Views / Downloads: 570 / 665
Referenced in: [show references]