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).

!752

!include #1004681 // Concepts

sconcept Sensor {
  long updatedWhen;
  PersistableThrowable error;
  transient volatile bool updating;
  
  void actionImpl() {} // override this in subclasses
  
  void action() {
    updatedWhen = now();
    updating = true;
    Throwable theError = null;
    try {
      actionImpl();
    } catch e {
      theError = e;
    } finally {
      error = persistableThrowable(theError);
      updating = false;
    }
  }
  
  bool ok() { ret error == null; }
  
  void dependOn(Sensor s) {
    updater.depend(this, s);
  }
  
  void update() {
    updater.update(this);
  }
}

sconcept BooleanSensor extends Sensor {
  bool booleanValue;
}

sclass Updater {
  new HashSet<Sensor> updated;
  
  void depend(Sensor a, Sensor b) {
    update(b);
  }
  
  void update(Sensor s) {
    if (!updated.contains(s)) {
      s.action();
      updated.add(s);
    }
  }
  
  void reset() {
    updated.clear();
  }
}

static new Updater updater;

sconcept RawPSCmd extends Sensor {
  S output;
  O notRunBecause;
  
  void actionImpl() {
    if (isLinux())
      output = backtick("ps -aef");
    else
      notRunBecause = "Not on Linux";
  }
}

// boolean value is true when there is at least 1 chrome process
sconcept PSChrome extends BooleanSensor {
  int chromeProcesses;
  
  void actionImpl() {
    RawPSCmd ps = unary(RawPSCmd.class);
    dependOn(ps);
    if (!ps.ok()) supplierFailure();
    L<S> cmds = psGetCommands(ps.output);
    chromeProcesses = l(indexesOfLinesStartingWith(cmds, "/opt/google/chrome/"));
    booleanValue = chromeProcesses > 0;
  }
}

p {
  typeWriterConsole();
  int last = -1;
  while licensed {
    updater.reset();
    pcall {
      PSChrome chrome = unary(PSChrome.class);
      chrome.update();
      if (!chrome.ok())
        print("Chrome Detection Error: " + chrome.error);
      else if (last != chrome.chromeProcesses) {
        last = chrome.chromeProcesses;
        print("Chrome Processes: " + last);
        setConsoleTitle(n(last, "Chrome Process"));
      }
    }
    sleepSeconds(1);
  }
}

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: 429 / 476
Referenced in: [show references]