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

93
LINES

< > BotCompany Repo | #1036215 // FollowTickerFile

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (12253L/71K).

srecord noeq FollowTickerFile(File tickerFile) is AutoCloseable {
  settable int historyEntriesToGrab;
  settable int interval = 250;

  settable ISleeper sleeper = defaultSleeper();
  
  // internal, may change  
  TailFile tailFile;
  
  event historicLine(S line);
  event liveLine(S line);
  
  gettable volatile long liveTimestamp;
  gettable volatile S lastLiveLine;
  
  settable transient bool started;
  event fireStarted;
  
  settable int maxLineLengthForTickerEntry = 200;
  
  settable Q q = startQ();
  
  bool isLive() { ret liveTimestamp != 0; }

  void start {
    q.add(r {
      if (started()) ret;
      started(true);
      fireStarted();
      
      onLiveLine(line -> {
        lastLiveLine = line;
        liveTimestamp = now();
      });
      
      long fileSize = l(tickerFile);
      long start = max(0, fileSize-maxLineLengthForTickerEntry*historyEntriesToGrab);
      if (historyEntriesToGrab > 0) {
        S text = loadTextFilePart(tickerFile, start, fileSize);
        LS lastTickerLines = takeLast(historyEntriesToGrab, lines(text));
  
        print("Have historic lines: " + l(lastTickerLines) + "/" + historyEntriesToGrab);
        for (line : lastTickerLines)
          historicLine(line);
      }
    
      makeTailFile(fileSize);
    });
  }
  
  void makeTailFile(long fileSize) {
    started(true);
    print("Following " + tickerFile + " (" + n2(l(tickerFile)) + " bytes) from position " + n2(fileSize));
    tailFile = tailFileLinewiseFromPosition(tickerFile, interval, fileSize, l1 liveLine, sleeper);
  }
  
  close {
    started(false);
    if (tailFile != null) {
      print("Done " + tailFile);
      dispose tailFile;
    }
  }
  
  void switchToFile(File newFile) {
    if (eq(tickerFile, newFile)) ret;
    q.add(r {
     if (eq(tickerFile, newFile)) ret;
      print("Switching to ticker file: " + newFile);
      if (started()) {
        close();
        tickerFile = newFile;
        started(true);
        makeTailFile(0);
      } else
        tickerFile = newFile;
    });
  }
  
  selfType onLine(IVF1<S> l) {
    onHistoricLine(l);
    ret onLiveLine(l);
  }
  
  toString {
    ret commaCombine(
      "FollowTickerFile",
      stringIf(started(), "started"),
      !isLive() ? null : "Last live line at " + formatLocalDateWithSeconds(liveTimestamp),
      tickerFile
    );
  }
}

download  show line numbers  debug dex  old transpilations   

Travelled to 2 computer(s): elmgxqgtpvxh, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1036215
Snippet name: FollowTickerFile
Eternal ID of this version: #1036215/27
Text MD5: 923ab1f0cae689c5d2e2a6d4143535a4
Transpilation MD5: 810ffa54bd08016276fe0682cbbdb27e
Author: stefan
Category: javax / trading
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2023-01-17 20:11:39
Source code size: 2436 bytes / 93 lines
Pitched / IR pitched: No / No
Views / Downloads: 167 / 319
Version history: 26 change(s)
Referenced in: [show references]