Implementing a Simple Server for the SmartInspect Libraries
Update: SmartInspect 3.0 now ships with a fully supported log server (SmartInspect Router).
This article demonstrates and explains a simple server which is capable of receiving logs via TCP/IP from the SmartInspect libraries and storing them in log files for later access. Such a server can be useful for several reasons:
- You can store and access all logs in one single location
- There are some scenarios where log files are not applicable but a server is
The last point is especially important. Imagine you want to use SmartInspect on-site and cannot use log files or the Console on the production servers for whatever reasons. At this point a dedicated SmartInspect server would be your only choice to inspect your applications. In this scenario you would install the SmartInspect server (preferable on a dedicated machine) and configure the SmartInspect libraries to send the logging information via TCP/IP to it.
In this article I explain how to implement such a server and provide a simple example implementation. The example server is called SmartInspect Receiver.
The example implementation described in this article is a multi-threaded TCP server which uses an independent thread for each connected client. Once a new connection comes in, the server creates a worker thread which handles the new client. When the client disconnects, the related thread exits as well. This design is simple to implement and works well, even with hundreds or thousands of clients. A non-blocking, single-threaded server would work as well of course, but is considerably more difficult to implement.
Once a new thread has been created, it begins with the handshake of the SmartInspect TCP protocol as described in the SmartInspect Log Formats and Protocols article. The handshake is very simple. It consists of a server banner and a client banner to exchange the version numbers between both parties. After a successful handshake, the server thread creates a new log file with a unique name for the client. In our example server, the unique filename is built with the ip and port of the client and a timestamp.
After the log file has been opened successfully, the SmartInspect log file header is written to it. The thread then begins to accept log packets from the SmartInspect library and writes them to the opened log file. For implementation details on the exact packet-exchange protocol and the log file header, please refer to the SmartInspect Log Formats and Protocols article (especially the file and TCP protocol sections).
The possible configuration options of a SmartInspect server are virtually open-ended. They can include the listening port of the TCP socket, the directory where to store the resulting log files, the format of the log filenames and so on.
The SmartInspect Receiver example implementation of this article currently offers the options to change the TCP port and the log directory. Options can be set with a configuration file. Like a SmartInspect library configuration file, it consists of simple key-value pairs. The SmartInspect Receiver service expects the configuration file to be placed into the same directory as the service executable with a name of SmartInspectReceiver.exe.config. A typical configuration file for the SmartInspect Receiver looks like:
; Set the listening TCP port Port = 4227 ; Set the directory of the resulting log files LogDir = %appdir%\Logs
For the LogDir option it is possible to specify a %appdir% variable which gets replaced with the application directory of the service executable. For example, if you specify %appdir%\Logs as log directory and the SmartInspect Receiver executable is placed into the c:\Receiver directory, the resulting log files are stored in c:\Receiver\Logs.
Once the configuration is done, the service application needs to be installed and registered as a known service. This can be done with the /install command line switch. To suppress any messages after a successful installation, you can use the /silent switch (useful for automated installations). An example command line can thus look like:
> SmartInspectReceiver.exe /install /silent
Uninstallation is similar. Just use /uninstall instead of /install to uninstall the service and to remove it from the list of known services.
Starting and Stopping
Just like any other service application for Windows, the SmartInspect Receiver service can be started and stopped with the Service Management Console of Windows which can be found in the Control Panel under Administrative Tools | Services. To start the SmartInspect Receiver, select the service in the list and choose Action | Start. To stop it again, choose Action | Stop.
After the service has been started, it is ready to accept new client connections from the SmartInspect libraries on the configured TCP port.
To summarize this article we can say that implementing a SmartInspect server is quite simple. We just need a TCP server which handles the SmartInspect TCP protocol as described in the SmartInspect Log Formats and Protocols article and creates appropriate log files with the received logging information.
The example implementation has less than 300 lines of Delphi source code including logging and tracing statements (the SmartInspect Receiver uses the SmartInspect Delphi library for this, of course). The source code and the compiled executable can be downloaded in our Resources area. Feel free to use and redistribute the service (but please be aware that it is unsupported).
The source should be a good starting point if you plan to implement a SmartInspect server on your own in a different programming language. If you have any questions about the source, the server in general or this article, feel free to contact me at firstname.lastname@example.org.