Friday, February 21, 2014

A tangle with shared memory


Recently I was thinking about inter-process communication and especially how to pass/ share data/ information between two running applications. The thing was, I wanted to check out inter-process communication interfaces and see if there was anything Qt provided that I could use. 

My requirement is not finalized or anything. The rough idea is to have a channel of communication between two apps. Here my thoughts diverge a bit in that, I am yet not sure of the method of communication. There are two approaches: 
1. The message system should be a message board where many processes post messages for each other and periodically access the boards to check out their messages.
2. The message system should be P2P. One process sends a message to another and that message (preferably named) makes other process do something like maybe send an acknowledgement. 

Now after digging through the various approaches, I have found out that for scenario 1, I could use shared memory. For scenario 2 I could use socket communication. 

So just to check out the shared memory stuff I went ahead and dug up Qt's provided example. 
The source has one class that contains that example as class SharedMemoryEx. It works as advised. I came across a limitation though. E.g. If first process runs, posts a message, and quits, then in this case the memory is released (at least in Windows case, where I tested the said code), and the message gets lost. 
Well, I think we can engineer around this. Or maybe just use files for this purpose. 
Although while looking around I found an interesting suggestion by someone about making a single instance application using shared memory. 

That's the second class SingleClass.  

Monday, February 10, 2014

Working on Some Qwt Graphs


Link to previous Article though it doesn't have any code. 
This is Link to Code

I am back with some more code. Recently I found working on some QWT graphs and as I dug deeper into Qwt I found that it's quite easy to work out something quite functional. 

So I developed a small utility to display Qwt's graphs and a small logic that would detect spikes in a data stream. 


Here's a screen shot of the actual thing in action. The left graph shows a curve and the right graph shows the detected spikes in that curve. I am showing both up and down-ward spikes. 


The time is shown as units and there are some tutorials on how to get correct time to shoe on X-Axis. But here it doesn't matter as this is just a Proof of concept. 

I have used a c_sinewave header to get the sinewave and I am introducing spikes using a simple randomizing logic. 

On detection part, I have following comments above the block: 

// tmp is current point; listOfSinePoints is super set;
// we will have a window as a subset of listOfSinePoints
// Spike detection logic here
// 1. find goodPoint window
// 2. See if current Point is in that window;
// if it is then it's a valid point else it's a spike

This is quite simple logic and works well for selected cases. A more complex and esoteric logic like prediction-based spike detection can be introduced if such cases are observed where this simple logic fails.

This serves as a good intro to Qwt's plots and curves.

Tuesday, February 4, 2014

flappy bird - this is a bad game!!!

Simple yet very difficult to play game!

Classes for Logging messages to a file and Truncate that file at routine intervals

Code at: 
https://drive.google.com/file/d/0BxorjNRCBW61VlVhbllEV0Njb1E/edit?usp=sharing

I often used to think that what a simple logging interface would look like. So I went ahead and wrote couple of classes that would do just that. 

The classes are simple. First class LogService has two constructors that set the logFileName. This class also has a function logMessageToFile that writes the provided string to the logFile.

LogService(QString);
void logMessageToFile(QString);

Now the second class LogTruncateService has similar constructors. But this class implements a QThread. So we have run function as well.

LogTruncateService(QString );
void run();

The run function contains an infinite while loop which keeps checking the time stamps stored in logFile and resets the file after certain no of days are past. Here I have set that time to 10 days.

The limitation is that we need to provide log messages in given format. Else this parser fails. But that is feasible when we are only logging critical activities, errors etc. And that's what is most suitable area for use of this service.