2

I am trying to find way to create a semaphore like functionality between a JS script run on a browser and a C++ program. My application is a real-time log file viewer. I have an application that logs periodically to a file. I am going to extract some information from that file and display on a browser using JS. But the problem is this file can get pretty big and having to go through the entire file periodically in JS is very slow and ineffective.

So I my thinking was to have a separate application read the file periodically and write to another file 'only' the new content. Then the JS can go through that smaller file and update its values. This can be done effectively with seek and tellg functions in C++.

But I can't have the JS and the C++ program running asynchronously. I need some way for the JS and the C++ program to sync with some kind of locking mechanism. For example C++ will take the lock and then read the log file and update the new contents. Then when the lock is available JS will lock it and display the contents and so on. Of course I need some kind of counter to make sure the C++ program will not overwrite before the JS reads the data.

Anyway you get the idea what I want to achieve. I came across 'Native Client' by Google. This was perfect as I can easily exchange messages back and forth between the JS and the C++ program. But unfortunately when I use Native Client it doesnt allow me to read/write to local files. So my C++ program has no access to read the log file.

Other way was to use Web Storage, but there is no consistent way for the C++ program to read the data written by the Web storage. So that doesn't work either.

If you have any idea please do share. Thanks.

4

1 回答 1

1

我可以建议的第一个选项是使用用c++ 实现的websocket 服务器。

第二种选择是(又快又脏);

  1. 编写一个简短的程序:acquire.cpp,它获取锁并输出一个 html 标头和消息“OK”或“error”。
  2. 编写另一个程序:release.cpp,它释放锁并输出一个 html 标题和消息“OK”或“error”。
  3. 在 JS 中编写函数以在编译后向那些(使用 CGI )发送 POST/GET 请求
  4. 像往常一样继续在你的 main.cpp 中使用这些锁。

那些将解决问题。

于 2013-07-22T18:24:55.357 回答