clacks (find it here on github: https://github.com/MaVCArt/clacks) is a modular TCP framework, designed to be as extensible as possible through the user of various factory-pattern and class-composition-inspired techniques.
In this article, we'll go through a step-by-step guide on how to set up your own server instance in a native python interpreter, and how to attach your code to it through the ServerInterface class, which comes built-in with the vanilla clacks environment.
Table of Contents
Step 1: setting up the server
This will install the default library, with all the bits and pieces you will need to get a fully functional server instance running in a Python terminal. Next, let's see if our installation was successful; run the following code in a python interpreter.
Congratulations! You're now on your way to create your first Clacks server! The instance you just created doesn't do much, of course - you haven't attached any of your functionalities to it. However, Clacks comes with several pre-built interfaces out of the box, so in this example we've added the "standard" interface, for convenience.
Step 2: Setting up the Proxy
Next, we have to learn to talk to our server. While some of Clacks' extension libraries make this easy enough that you don't need clacks itself to talk to them, for the sake of this example we will not involve those yet.
In bog-standard clacks, you will need to use the ClientProxyBase class to communicate with a server instance. The rule set is simple; to communicate with a server, find out which handler you're talking to it through. As stated in "the zen of Clacks", each server instance can have an infinite number of handlers, but only one handler/marshaller combination can listen to or speak to one port at the same time.
Thus, to talk to our "simple" handler, which uses a SimpleRequestHandler class as its handler, and the SimplePackageMarshaller class as its marshaller, you will need the same handler/marshaller combination on the proxy side to talk to that handler. Let's illustrate by doing this.
Simple, right? So, that gets us up and running with a simple server/proxy setup. On to the last step, adding your own methods!
Step 3: Create your own interface
Step 4: Attaching the interface to the server
Returning to our earlier example, we can now import our custom interface (which we saved in a file called "interface.py" next to this snippet), and attach it to the server. You can use the "register_interface" method on the server class for this. As you can see, because have not registered this interface class as a library-available Interface class, we have to manually register the interface instance itself. But as you can see, this makes little difference.
Comments
Post a Comment
All questions about blog or article content are welcome. Questions about my employment or employer, past or present, will not be answered.