Skip to content
Wouter Timmermans edited this page Jan 18, 2015 · 2 revisions

Introduction

Summary

After reading this introduction you should have a general idea how this protocol implementation works and can be used in your application.

Structure

C4 Protocol is a Remote Procedure Call(RPC) wrapper around the INF3 Connect 4 String protocol. Instead of manually generating and parsing Strings a client simply executes a method and the corresponding method gets called on the server and vice versa. The wrapper takes care of converting the Java types to Strings and parsing the Strings to Java types. The function groups are splitted in Client, which are the commands a server can send to a client, and Server, which are the commands a client can send to the server. E.g. for the core functionality you have. CoreClient (server -> client) and CoreServer client -> server

Client

That sounds all nice and dandy but wat if you just want to send a command to a server? For this you need a C4Client, every function group has a nested classs Client that extends the class C4Client. The C4Client needs a BufferedOutputReader and can write commands to it. Here follows an example if you want to send the join command to a server over the network:

Socket sock = new Socket(InetAddress.getLocalHost(), 1337);
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(sock.getOutputStream()));
CoreServer.Client serverClient = new CoreServer.Client(out);
serverClient.join("Pietje", 11, new HashSet<Extension>());

Exception handling and imports are not included for brevity. But it doesn't get much harder than this to send a command. All methods for sending commands throw exceptions if the given paramaters don't comply with the protocol specification. If you don't want to include an optional parameter you can pass null.

Server

If you also want to use the protocol wrapper on the other side of the connection you will need a Processor. This is the fun part. A Processor transforms incoming command strings to method calls on your own code. Every function group has an interace Iface. The constructor of the processor gets passed an object that implements this interface. When constructed the Processor.process method can be used to execute commands. Again an example:

ClientHandler handler = new ClientHandler();
CoreServer.Processor processor = new CoreServer.Processor<>(handler);
BufferedReader in = new BufferedReader(new InputStreamReader(sock.getInputStream()));
processor.process(in.readLine());
Clone this wiki locally