-
Notifications
You must be signed in to change notification settings - Fork 0
Packetizing fixture, attaching to host simulation #37
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,28 @@ | ||
|
|
||
| import asyncio | ||
|
|
||
| from sim_server import SimServer | ||
| from not_tcp.host import StreamProxy | ||
|
|
||
|
|
||
| class HostSimulator(SimServer, StreamProxy): | ||
| # Multiple inheritance is not a *crime*, it's just an abuse of the rules. | ||
| # Tax avoidance is not tax evasion! | ||
| pass | ||
|
|
||
|
|
||
| async def run_server(port): | ||
| import sys | ||
| import ntcp_http | ||
| dut = ntcp_http.NtcpHttpServer() | ||
|
|
||
| with HostSimulator(dut, dut.tx, dut.rx) as srv: | ||
| server = await asyncio.start_server( | ||
| client_connected_cb=srv.client_connected, host="localhost", | ||
| port=port) | ||
| sys.stderr.write(f"listening on port {port}\n") | ||
| await server.serve_forever() | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| asyncio.run(run_server(3278)) |
This file contains hidden or 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 hidden or 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,133 @@ | ||
| import sys | ||
| import pytest | ||
| import asyncio | ||
| from ntcp_http import NtcpHttpServer | ||
|
|
||
| from amaranth import Module | ||
| from amaranth.lib.wiring import Component, In, Out | ||
| from amaranth.lib import stream | ||
|
|
||
| from host_sim import HostSimulator | ||
| from not_tcp.host import Packet, Flag | ||
| from sim_server import SimServer | ||
| from not_tcp.not_tcp import StreamStop | ||
| from http_server import capitalizer | ||
|
|
||
| pytest_plugins = ('pytest_asyncio',) | ||
|
|
||
|
|
||
| class Capitalize(Component): | ||
| """ | ||
| A Not TCP server that capitalizes its input. | ||
| """ | ||
|
|
||
| tx: Out(stream.Signature(8)) | ||
| rx: In(stream.Signature(8)) | ||
|
|
||
| def elaborate(self, platform): | ||
| m = Module() | ||
|
|
||
| stop = m.submodules.stop = StreamStop(1) | ||
| # Serial side: | ||
| m.d.comb += [ | ||
| self.tx.valid.eq(stop.bus.downstream.valid), | ||
| self.tx.payload.eq(stop.bus.downstream.payload), | ||
| stop.bus.downstream.ready.eq(self.tx.ready), | ||
|
|
||
| stop.bus.upstream.valid.eq(self.rx.valid), | ||
| stop.bus.upstream.payload.eq(self.rx.payload), | ||
| self.rx.ready.eq(stop.bus.upstream.ready), | ||
| ] | ||
|
|
||
| cap = m.submodules.capitalizer = capitalizer.Capitalizer() | ||
|
|
||
| m.d.comb += [ | ||
| # Data: | ||
| cap.input.eq(stop.stop.inbound.data.payload), | ||
| stop.stop.outbound.data.payload.eq(cap.output), | ||
| # Stream control: | ||
| stop.stop.outbound.data.valid.eq(stop.stop.inbound.data.valid), | ||
| stop.stop.inbound.data.ready.eq(stop.stop.outbound.data.ready), | ||
| # Session control: | ||
| stop.stop.outbound.active.eq(stop.stop.inbound.active), | ||
| ] | ||
|
|
||
| return m | ||
|
|
||
|
|
||
| def DISABLED_test_capitalize_server(): | ||
| dut = Capitalize() | ||
|
|
||
| with SimServer(dut, dut.tx, dut.rx) as srv: | ||
| p1 = Packet(flags=Flag.START, stream_id=1, body=b"hello world") | ||
| srv.send(p1.to_bytes()) | ||
|
|
||
| received_bytes = bytes() | ||
| received_body = bytes() | ||
| packets = [] | ||
| import sys | ||
| for i in range(100): | ||
| received_bytes += srv.recv() | ||
| (packet, remainder) = Packet.from_bytes(received_bytes) | ||
| if packet is not None: | ||
| sys.stderr.write(f"{packet}\n") | ||
| received_bytes = remainder | ||
| packets += [packet] | ||
| received_body += packet.body | ||
| if packet.end or len(received_body) == len("hello world"): | ||
| break | ||
| assert received_body == b"HELLO WORLD" | ||
|
|
||
| received_body = bytes() | ||
| # TODO: For now, we have to send an explicit "end" packet | ||
| p2 = Packet(stream_id=1, body=b"Goodbye for now") | ||
| p3 = Packet(flags=Flag.END, stream_id=1) | ||
| srv.send(p2.to_bytes()) | ||
| srv.send(p3.to_bytes()) | ||
| for i in range(100): | ||
| received_bytes += srv.recv() | ||
| (packet, remainder) = Packet.from_bytes(received_bytes) | ||
| if packet is not None: | ||
| sys.stderr.write(f"{packet}\n") | ||
| received_bytes = remainder | ||
| packets += [packet] | ||
| received_body += packet.body | ||
| if packet.end: | ||
| break | ||
| assert received_body == b"GOODBYE FOR NOW" | ||
|
|
||
| for i in range(len(packets)): | ||
| packet = packets[i] | ||
| assert packet.to_host | ||
| assert packet.start == ( | ||
| i == 0), f"start {packet.start} for packet {i}" | ||
| assert packet.end == ( | ||
| i == len(packets)-1), f"end {packet.end} for packet {i}" | ||
| assert packet.to_host | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_tcp_proxy(): | ||
| dut = NtcpHttpServer() | ||
|
|
||
| with HostSimulator(dut, dut.tx, dut.rx) as srv: | ||
| server = await asyncio.start_server( | ||
| client_connected_cb=srv.client_connected, host="localhost", | ||
| port=3278) | ||
| async with server: | ||
| reader, writer = await asyncio.open_connection("127.0.0.1", 3278) | ||
| writer.write( | ||
| "\r\n".join([ | ||
| "POST /nothing-here HTTP/1.0", | ||
| "Cache-Control: private", | ||
| "", | ||
| "", | ||
| "lovely day today" | ||
| ]).encode("utf-8") | ||
| ) | ||
| await writer.drain() | ||
|
|
||
| read = await reader.read(-1) | ||
| response = read.decode("utf-8") | ||
| lines = response.split("\r\n") | ||
| assert lines[0] == "HTTP/1.0 404 Not Found" |
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -17,4 +17,5 @@ regex | |
| # Dev dependencies: | ||
| flake8 | ||
| pytest | ||
| pytest-asyncio | ||
| hypothesis | ||
This file contains hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems to be the cause of the issue where we can't send more than one request. If I get rid of this line, I can get as many /coffee (418) and /counts responses I want from curl
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The rewrite/split of "separate state machines" also fixed the "only one request", without getting rid of this -- and I think the "stop" is important to capture, so I'd like to leave this.
Merging this with the bug intact, #39 has the rewrite.