Vlad's blog

In programming veritas

Posts Tagged ‘Boost

Finite-state machines in practice

leave a comment »

Finite-state Machines (FSM) have been known and used widely over the decades. FSM are used when developing lexical and syntax analysers, graph algorithms, communication protocols, UML and many other areas of computer science. However like many other too formalized ideas FSM takes some time to study before you can leverage your experience by using this technic in practice. This post demonstrates using of Boost.Statechart library for developing a fairly straightforward utility that synchronizes files located in two different folders.
Suppose we have a server side application that periodically writes information in some file. A client is interested in getting the most current version of that file. A file synchronization algorithm will be trivial. A client queries the last modification time from the server. If value is greater than time of last update a client initiates a file download. For the sake of simplicity let’s suppose a client communicates with server by using interface IRemoteFileSystem.

struct IRemoteFileSystem
     virtual void GetFileModificationTime(const boost::filesystem::path& remoteFilePath, GetFileModificationTimeCallback callback) = 0;

     virtual void DownloadFile(const boost::filesystem::path& remoteFilePath, DownloadFileCallback callback) = 0;

GetFileModificationTime returns the last file modification time. The function does not wait until the server completes the processing of request and returns immediately. The second argument defines a callback that will be called when server responds. The IRemoteFileSystem implementation establishes a TCP/IP connection with server, keeps a list of pending requests and invokes callbacks when server replies. Error handling logic and timeouts handling are also implemented in this component.
Below is a diagram that illustrates a processing of GetFileModificationTime request.

Read the rest of this entry »


Written by vsukhachev

October 17, 2011 at 6:14 am

Posted in Development

Tagged with ,