Question

I am writing a server in twisted that needs to propagate information to other servers. To implement this, I have written a simple client protocol which writes to a given server. I use deferreds to call transport.write(), and I have confirmed through print statements that my callback is called, but I never get any output from my server.

My code is here: https://gist.github.com/sakekasi/9460002

maybe I'm preventing the reactor from running for some reason? I'm pretty sure I haven't, as I don't use any long running loops.

Thanks in advance.

EDIT: quick walkthrough of my code

I'm instantiating 5 servers according to the dictionary ports. For each server, I create a new factory and bind it to its respective port.

invalid commands: given an invalid command, a server must reply with ? command

IAMAT: 'IAMAT name lat lon time' must be responded to with AT servername name lat lon time. In addition, the server that recieves this command proceeds to propagate it to all servers under its name in the talks dictionary. To do so, I instantiate clients from a client factory, and have them send a message to the right port based on the ports dictionary.

for the code details, look at respond.iamat

Était-ce utile?

La solution

The final line in parse_message(line) reads:

{... }.get(toks[0], inv)()

I think it should be

{... }.get(toks[0])()

EDIT

I can successfully send an IAMAT message and have it parsed by the server:

nc -c localhost 12604
IAMAT b 1 2 3
AT Young 1394489423.5 b 1.0 2.0 3.0

But I'm a little confused as to what should happen next. The server seems to attempt to establish new connections back to itself using TCP4ClientEndpoint (rather than using the existing connections to the clients) in order to send AT messages. There seems to be a problem here where the messages aren't sent, but if this succeeded, wouldn't it result in a continuous loop of messages going back and forth within the server?

EDIT 2

Thanks for the clarification. By default, Twisted's LineReceiver expects lines to be terminated by a carriage return and new line character (\r\n). Your messages are being sent, but then subsequently ignored as your flatten_cmd function only appends \n. Changing it to the following fixes this problem:

def flatten_cmd(schema, cmd):
    ret = cmd['type']
    for x in schema:
        k = x[0]
        ret = ret + ' ' + str(cmd[k])
    return ret+'\r\n'

And now the server receives the updates as expected:

got IAMAT blah 1 2 3
sending AT Farmar 1394676270.11 blah 1.0 2.0 3.0
sending AT Farmar 1394676270.11 blah 1.0 2.0 3.0
got AT Farmar 1394676270.11 blah 1.0 2.0 3.0
got AT Farmar 1394676270.11 blah 1.0 2.0 3.0
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top