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

1  
srecord noeq FollowTickerFile(File tickerFile) is AutoCloseable {
2  
  settable int historyEntriesToGrab;
3  
  settable int interval = 250;
4  
5  
  settable ISleeper sleeper = defaultSleeper();
6  
  
7  
  // internal, may change  
8  
  TailFile tailFile;
9  
  
10  
  event historicLine(S line);
11  
  event liveLine(S line);
12  
  
13  
  gettable volatile long liveTimestamp;
14  
  gettable volatile S lastLiveLine;
15  
  
16  
  settable transient bool started;
17  
  event fireStarted;
18  
  
19  
  settable int maxLineLengthForTickerEntry = 200;
20  
  
21  
  settable Q q = startQ();
22  
  
23  
  bool isLive() { ret liveTimestamp != 0; }
24  
25  
  void start {
26  
    q.add(r {
27  
      if (started()) ret;
28  
      started(true);
29  
      fireStarted();
30  
      
31  
      onLiveLine(line -> {
32  
        lastLiveLine = line;
33  
        liveTimestamp = now();
34  
      });
35  
      
36  
      long fileSize = l(tickerFile);
37  
      long start = max(0, fileSize-maxLineLengthForTickerEntry*historyEntriesToGrab);
38  
      if (historyEntriesToGrab > 0) {
39  
        S text = loadTextFilePart(tickerFile, start, fileSize);
40  
        LS lastTickerLines = takeLast(historyEntriesToGrab, lines(text));
41  
  
42  
        print("Have historic lines: " + l(lastTickerLines) + "/" + historyEntriesToGrab);
43  
        for (line : lastTickerLines)
44  
          historicLine(line);
45  
      }
46  
    
47  
      makeTailFile(fileSize);
48  
    });
49  
  }
50  
  
51  
  void makeTailFile(long fileSize) {
52  
    started(true);
53  
    print("Following " + tickerFile + " (" + n2(l(tickerFile)) + " bytes) from position " + n2(fileSize));
54  
    tailFile = tailFileLinewiseFromPosition(tickerFile, interval, fileSize, l1 liveLine, sleeper);
55  
  }
56  
  
57  
  close {
58  
    started(false);
59  
    if (tailFile != null) {
60  
      print("Done " + tailFile);
61  
      dispose tailFile;
62  
    }
63  
  }
64  
  
65  
  void switchToFile(File newFile) {
66  
    if (eq(tickerFile, newFile)) ret;
67  
    q.add(r {
68  
     if (eq(tickerFile, newFile)) ret;
69  
      print("Switching to ticker file: " + newFile);
70  
      if (started()) {
71  
        close();
72  
        tickerFile = newFile;
73  
        started(true);
74  
        makeTailFile(0);
75  
      } else
76  
        tickerFile = newFile;
77  
    });
78  
  }
79  
  
80  
  selfType onLine(IVF1<S> l) {
81  
    onHistoricLine(l);
82  
    ret onLiveLine(l);
83  
  }
84  
  
85  
  toString {
86  
    ret commaCombine(
87  
      "FollowTickerFile",
88  
      stringIf(started(), "started"),
89  
      !isLive() ? null : "Last live line at " + formatLocalDateWithSeconds(liveTimestamp),
90  
      tickerFile
91  
    );
92  
  }
93  
}

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: 173 / 329
Version history: 26 change(s)
Referenced in: [show references]