Skip to content
Snippets Groups Projects
Commit 5e89ced8 authored by Jiří Kalvoda's avatar Jiří Kalvoda
Browse files

Module: Pipe

parent c7153d34
No related branches found
No related tags found
No related merge requests found
......@@ -155,8 +155,100 @@ text = ERR
return Parse(text);
}
}
[ModuleName("pipe")]
class ModulePipe: Module
{
StatusBar bar;
string path;
string text;
protected Color color;
InnerStatusBar ifNoData;
InnerStatusBar ifReadError;
Thread inputThread;
int msgSeparator;
int scheudleIn_ms;
bool pipeError = false;
public void Init(StatusBar _bar, ConfigParser config, ConfigSection section)
{
bar = _bar;
path = section.Mandatory("path").AsPath();
color = section.Optional("color")?.AsColor() ?? System.Drawing.ColorTranslator.FromHtml("white");
ifNoData = new InnerStatusBar(_bar, section.Optional("no_data")?.AsConfig() ??
new ConfigParser(
@"
"));
ifReadError = new InnerStatusBar(_bar, section.Optional("read_error")?.AsConfig() ??
new ConfigParser(
@"
[constant]
color = red
text = ERR
"));
msgSeparator = section.Optional("separator")?.AsInt() ?? 0;
scheudleIn_ms = section.Optional("delay")?.AsMs() ?? 10;
inputThread = new Thread(this.inputThreadFunc);
inputThread.IsBackground = true;
inputThread.Start();
}
void inputThreadFunc()
{
while(true)
{
int r = POSIX.mkfifo(path, (7<<6)+(7<<3)+7);
int err = POSIX.ERRNO;
Console.WriteLine($"{r} {err}");
if(r == 0) break;
if(r != 0 && err == POSIX.EEXIST)
{
File.Delete(path);
continue;
}
pipeError = true;
throw new Exception($"POSIX.mkfifo fail with ERRNO {err}");
}
var sr = new StreamReader(path);
var sw = new StreamWriter(path);
// Only for keeping pipe alive
StringBuilder s = new();
while(true)
{
int c = sr.Read();
if(c == msgSeparator)
{
lock(this)
{
text = s.ToString();
s = new StringBuilder();
}
bar.Schedule(scheudleIn_ms);
}
else
{
s.Append((char)c);
}
}
}
protected virtual IEnumerable<Element> Parse(string text)
{
if(text.Length > 0 && text[^1] == '\n') text = text[..^1];
return text.Split("\n").Select(x => new Element(x, color: color)).ToArray();
}
public IEnumerable<Element> Get()
{
string _text;
lock(this)
{
_text = text;
}
if(pipeError)
return ifReadError.Get();
if(_text == null)
return ifNoData.Get();
return Parse(_text);
}
}
[ModuleName("osdd_last")]
class ModuleOsddLast: ModuleFile
class ModuleOsddLast: ModulePipe
{
public new void Init(StatusBar _bar, ConfigParser config, ConfigSection section)
{
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment