-
Notifications
You must be signed in to change notification settings - Fork 108
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 #173 from bobh66/tcp
Add tcp support
- Loading branch information
Showing
22 changed files
with
1,653 additions
and
33 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -58,3 +58,6 @@ bin/ | |
|
||
# PyBuilder | ||
target/ | ||
|
||
# PyCharm | ||
.idea/ |
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 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 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 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 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,56 @@ | ||
"""Small example Asynchronous OSC TCP client | ||
This program listens for incoming messages in one task, and | ||
sends 10 random values between 0.0 and 1.0 to the /filter address, | ||
waiting for 1 seconds between each value in a second task. | ||
""" | ||
|
||
import argparse | ||
import asyncio | ||
import random | ||
import sys | ||
|
||
from pythonosc import tcp_client | ||
|
||
|
||
async def get_messages(client): | ||
async for msg in client.get_messages(60): | ||
print(msg) | ||
|
||
|
||
async def send_messages(client): | ||
for x in range(10): | ||
r = random.random() | ||
print(f"Sending /filter {r}") | ||
await client.send_message("/filter", r) | ||
await asyncio.sleep(1) | ||
|
||
|
||
async def init_main(): | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument("--ip", default="127.0.0.1", help="The ip of the OSC server") | ||
parser.add_argument( | ||
"--port", type=int, default=5005, help="The port the OSC server is listening on" | ||
) | ||
parser.add_argument( | ||
"--mode", | ||
default="1.1", | ||
help="The OSC protocol version of the server (default is 1.1)", | ||
) | ||
args = parser.parse_args() | ||
|
||
async with tcp_client.AsyncSimpleTCPClient( | ||
args.ip, args.port, mode=args.mode | ||
) as client: | ||
async with asyncio.TaskGroup() as tg: | ||
tg.create_task(get_messages(client)) | ||
tg.create_task(send_messages(client)) | ||
|
||
|
||
if sys.version_info >= (3, 7): | ||
asyncio.run(init_main()) | ||
else: | ||
# TODO(python-upgrade): drop this once 3.6 is no longer supported | ||
event_loop = asyncio.get_event_loop() | ||
event_loop.run_until_complete(init_main()) | ||
event_loop.close() |
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,51 @@ | ||
import argparse | ||
import asyncio | ||
import sys | ||
|
||
from pythonosc.dispatcher import Dispatcher | ||
from pythonosc.osc_tcp_server import AsyncOSCTCPServer | ||
|
||
|
||
def filter_handler(address, *args): | ||
print(f"{address}: {args}") | ||
|
||
|
||
dispatcher = Dispatcher() | ||
dispatcher.map("/filter", filter_handler) | ||
|
||
|
||
async def loop(): | ||
"""Example main loop that only runs for 10 iterations before finishing""" | ||
for i in range(10): | ||
print(f"Loop {i}") | ||
await asyncio.sleep(10) | ||
|
||
|
||
async def init_main(): | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument("--ip", default="127.0.0.1", help="The ip of the OSC server") | ||
parser.add_argument( | ||
"--port", type=int, default=5005, help="The port the OSC server is listening on" | ||
) | ||
parser.add_argument( | ||
"--mode", | ||
default="1.1", | ||
help="The OSC protocol version of the server (default is 1.1)", | ||
) | ||
args = parser.parse_args() | ||
|
||
async with AsyncOSCTCPServer( | ||
args.ip, args.port, dispatcher, mode=args.mode | ||
) as server: | ||
async with asyncio.TaskGroup() as tg: | ||
tg.create_task(server.start()) | ||
tg.create_task(loop()) | ||
|
||
|
||
if sys.version_info >= (3, 7): | ||
asyncio.run(init_main()) | ||
else: | ||
# TODO(python-upgrade): drop this once 3.6 is no longer supported | ||
event_loop = asyncio.get_event_loop() | ||
event_loop.run_until_complete(init_main()) | ||
event_loop.close() |
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,27 @@ | ||
"""Small example OSC client | ||
This program sends 10 random values between 0.0 and 1.0 to the /filter address, | ||
waiting for 1 seconds between each value. | ||
""" | ||
|
||
import argparse | ||
import random | ||
import time | ||
|
||
from pythonosc import udp_client | ||
|
||
if __name__ == "__main__": | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument("--ip", default="127.0.0.1", help="The ip of the OSC server") | ||
parser.add_argument( | ||
"--port", type=int, default=5005, help="The port the OSC server is listening on" | ||
) | ||
args = parser.parse_args() | ||
|
||
client = udp_client.SimpleUDPClient(args.ip, args.port) | ||
|
||
for x in range(10): | ||
client.send_message("/filter", random.random()) | ||
reply = next(client.get_messages(2)) | ||
print(str(reply)) | ||
time.sleep(1) |
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,30 @@ | ||
"""Small example OSC server | ||
This program listens to several addresses, and prints some information about | ||
received packets. | ||
""" | ||
|
||
import argparse | ||
import math | ||
|
||
from pythonosc.dispatcher import Dispatcher | ||
from pythonosc import osc_server | ||
|
||
|
||
def echo_handler(client_addr, unused_addr, args): | ||
print(unused_addr, args) | ||
return (unused_addr, args) | ||
|
||
|
||
if __name__ == "__main__": | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument("--ip", default="127.0.0.1", help="The ip to listen on") | ||
parser.add_argument("--port", type=int, default=5005, help="The port to listen on") | ||
args = parser.parse_args() | ||
|
||
dispatcher = Dispatcher() | ||
dispatcher.set_default_handler(echo_handler, True) | ||
|
||
server = osc_server.ThreadingOSCUDPServer((args.ip, args.port), dispatcher) | ||
print("Serving on {}".format(server.server_address)) | ||
server.serve_forever() |
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,35 @@ | ||
"""Small example OSC client | ||
This program sends 10 random values between 0.0 and 1.0 to the /filter address, | ||
and listens for incoming messages for 1 second between each value. | ||
""" | ||
|
||
import argparse | ||
import random | ||
|
||
from pythonosc import tcp_client | ||
|
||
if __name__ == "__main__": | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument("--ip", default="127.0.0.1", help="The ip of the OSC server") | ||
parser.add_argument( | ||
"--port", type=int, default=5005, help="The port the OSC server is listening on" | ||
) | ||
parser.add_argument( | ||
"--mode", | ||
default="1.1", | ||
help="The OSC protocol version of the server (default is 1.1)", | ||
) | ||
args = parser.parse_args() | ||
|
||
with tcp_client.SimpleTCPClient(args.ip, args.port, mode=args.mode) as client: | ||
for x in range(10): | ||
n = random.random() | ||
print(f"Sending /filter {n}") | ||
client.send_message("/filter", n) | ||
resp = client.get_messages(1) | ||
for r in resp: | ||
try: | ||
print(r) | ||
except Exception as e: | ||
print(f"oops {str(e)}: {r}") |
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,46 @@ | ||
"""Small example OSC server | ||
This program listens to the specified address and port, and prints some information about | ||
received packets. | ||
""" | ||
|
||
import argparse | ||
import math | ||
|
||
from pythonosc import osc_tcp_server | ||
from pythonosc.dispatcher import Dispatcher | ||
|
||
|
||
def print_volume_handler(unused_addr, args, volume): | ||
print("[{0}] ~ {1}".format(args[0], volume)) | ||
|
||
|
||
def print_compute_handler(unused_addr, args, volume): | ||
try: | ||
print("[{0}] ~ {1}".format(args[0], args[1](volume))) | ||
except ValueError: | ||
pass | ||
|
||
|
||
if __name__ == "__main__": | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument("--ip", default="127.0.0.1", help="The ip to listen on") | ||
parser.add_argument("--port", type=int, default=5005, help="The port to listen on") | ||
parser.add_argument( | ||
"--mode", | ||
default="1.1", | ||
help="The OSC protocol version of the server (default is 1.1)", | ||
) | ||
|
||
args = parser.parse_args() | ||
|
||
dispatcher = Dispatcher() | ||
dispatcher.map("/filter", print) | ||
dispatcher.map("/volume", print_volume_handler, "Volume") | ||
dispatcher.map("/logvolume", print_compute_handler, "Log volume", math.log) | ||
|
||
server = osc_tcp_server.ThreadingOSCTCPServer( | ||
(args.ip, args.port), dispatcher, mode=args.mode | ||
) | ||
print("Serving on {}".format(server.server_address)) | ||
server.serve_forever() |
Oops, something went wrong.