-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from djmitche/udp-network
Use more realistic UDP-based networking
- Loading branch information
Showing
8 changed files
with
326 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import sys | ||
import logging | ||
from network import Node | ||
|
||
|
||
class Client(Node): | ||
|
||
def __init__(self, member_address): | ||
super(Client, self).__init__() | ||
self.member_address = member_address | ||
|
||
def invoke(self, input): | ||
self.output = None | ||
self.send([self.member_address], 'INVOKE', | ||
input=input, caller=self.address) | ||
self.run() | ||
return self.output | ||
|
||
def do_INVOKED(self, output): | ||
self.logger.debug("received output %r" % (output,)) | ||
self.output = output | ||
self.stop() | ||
|
||
if __name__ == "__main__": | ||
logging.basicConfig( | ||
format="%(asctime)s %(name)s %(message)s", level=logging.DEBUG) | ||
client = Client(sys.argv[1]) | ||
print client.invoke(4) | ||
print client.invoke(1) | ||
|
||
# tests | ||
|
||
import unittest | ||
import threading | ||
|
||
|
||
class FakeMember(Node): | ||
|
||
def do_INVOKE(self, caller, input): | ||
self.send([caller], 'INVOKED', output=input * 10) | ||
self.stop() | ||
|
||
|
||
class ClientTests(unittest.TestCase): | ||
|
||
def test_invoke(self): | ||
member = FakeMember() | ||
client = Client(member.address) | ||
memberthd = threading.Thread(target=member.run) | ||
memberthd.start() | ||
self.assertEqual(client.invoke(5), 50) | ||
memberthd.join() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.