OS-independent middleware between Python and C

I have very little idea what I'm doing here, I've never done anything like this before, but we are writing competing chess programs with a friend and they should be able to communicate with each other.

He will write mainly in C, the main part of mine will be in Python, and I see several options:

  • Alternatively write a temporary file or consecutive temporary files. Since communication won't be cumbersome in any way, it might work, but for me it seems ugly, programs will have to keep checking for changes / new files, it just seems ugly.
  • Find a way to manipulate the pipes, i.e. my.py | ./ it. It seems like a dead end.
  • Use sockets. But I don’t know what I would do, so can someone give me a pointer to some reading material? I'm not sure if there are OS-independent, language-independent methods. Should there be some kind of supervisor server program to administer?
  • Use some kind of HTML protocol that seems redundant. I do not mind that the programs run on the same machine.

What do people recommend and where can I start reading?

+6
c python networking network-protocols inter-process-communicat
source share
5 answers

If you need and need a truly independent OS-independent interprocess communication, sockets are probably the best option.

This will allow the two programs to also interact between machines (no code changes).

For reading materials, here 's Socket Python Socket Programming .

+7
source share

Two possibilities:

  • Use IP sockets. There are some examples in Python docs. (Actually, it’s not so difficult if you just use the basic material for reading / writing.) On the other hand, sockets in C are usually not so easy to use.

  • Create a third application. It launches both applications using a subprocess and communicates with both applications through channels. Chess applications should only be able to read / write stdin / stdout.

    This has the added benefit that this application can check if the movement is legal. This helps you find bugs and maintain game integrity.

+3
source share

You can use Protobuf as an inter-program protocol and read / write from a file that is rotated each time.

You can read the intermediate file every n seconds.

Once you do this, you can move on to using sockets, where each program will start the server and wait for connections.

The change should be small, because the protocol will be protobuff. So, the only place you need to change is where you are either reading a socket or from a file.

In any case, you will need an exchange protocol.

change

Ooops I read incorrectly, and I thought it was C ++.

Anyway, here is the C support for protobuf, but it still works while working

http://code.google.com/p/protobuf-c/

+2
source share

I would say just write an xml file containing the moves for black and white. Mark in a separate file that will overturn it, and make sure that only the program that will overturn it will write to this file in order to complete its turn.

Here is a link to the proposed xml format for storing your moves that another group came up with http://www.xml.com/pub/a/2004/08/25/tourist.html

0
source share

Sockets with a client / server model ...

Basically, you and your friend create different client implementations.

The local client shows a visual representation of the game and saves the state of the figures (position, killed / not killed) and the rules that the pieces can / cannot do (what steps can be taken, with which pieces the state of the board is checked).

The remote server stores the status of the players (whose move, points are earned, won the game or not) and a list of moves that occurred.

When you make a move, your client checks the move against the rules of the game, and then sends a message to the server, which says I made this move, your move.

Another client sees that a turn has been made, pulls the last move from the server, calculates whether there was a place of movement, checks the move against the rules of the game and repeats the action locally. After that, everything is done, now it allows the user to make the next move (or not, if the game is over).

The most important part of the game communication between the client and the server is to send as little data as possible and save as few states as possible on the server. Thus, you can play it locally or around the world with little or no delay. As long as your client works under the same set of rules as your opponent’s client, everything should work.

If you want to make sure that no one can deceive by hacking your version of the client, you can do all the calculations of the position and rules on the server and just make the clients only simple playback mechanisms.

The reason sockets are the best means of communication:

  • constraints on process communication are almost as complex as node cross-connectivity
  • Widely supported network on all systems.
  • there is little or no entry barrier to using this remotely if you choose
  • Networking is reliable, flexible and proven.

This is part of the reason why many major systems, such as databases, use sockets as a network, as well as a local communications environment.

0
source share

All Articles