Skip to content

Commit 1f38c87

Browse files
committed
fix imports and add udp example
1 parent 865ea8e commit 1f38c87

File tree

6 files changed

+48
-12
lines changed

6 files changed

+48
-12
lines changed

README.md

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -227,16 +227,50 @@ fn main() -> None:
227227

228228
Pure Mojo-based client is available by default. This client is also used internally for testing the server.
229229

230-
## Switching between pure Mojo and Python implementations
231-
232-
By default, Lightbug uses the pure Mojo implementation for networking. To use Python's `socket` library instead, just import the `PythonServer` instead of the `Server` with the following line:
230+
### UDP Support
231+
To get started with UDP, just use the `listen_udp` and `dial_udp` functions, along with `write_to` and `read_from` methods, like below.
233232

233+
On the client:
234234
```mojo
235-
from lightbug_http.python.server import PythonServer
235+
from lightbug_http.connection import dial_udp
236+
from lightbug_http.address import UDPAddr
237+
from utils import StringSlice
238+
239+
alias test_string = "Hello, lightbug!"
240+
241+
fn main() raises:
242+
print("Dialing UDP server...")
243+
alias host = "127.0.0.1"
244+
alias port = 12000
245+
var udp = dial_udp(host, port)
246+
247+
print("Sending " + str(len(test_string)) + " messages to the server...")
248+
for i in range(len(test_string)):
249+
_ = udp.write_to(str(test_string[i]).as_bytes(), host, port)
250+
251+
try:
252+
response, _, _ = udp.read_from(16)
253+
print("Response received:", StringSlice(unsafe_from_utf8=response))
254+
except e:
255+
if str(e) != str("EOF"):
256+
raise e
257+
236258
```
237259

238-
You can then use all the regular server commands in the same way as with the default server.
239-
Note: as of September, 2024, `PythonServer` and `PythonClient` throw a compilation error when starting. There's an open [issue](https://github.com/saviorand/lightbug_http/issues/41) to fix this - contributions welcome!
260+
On the server:
261+
```mojo
262+
fn main() raises:
263+
var listener = listen_udp("127.0.0.1", 12000)
264+
265+
while True:
266+
response, host, port = listener.read_from(16)
267+
var message = StringSlice(unsafe_from_utf8=response)
268+
print("Message received:", message)
269+
270+
# Response with the same message in uppercase
271+
_ = listener.write_to(String.upper(message).as_bytes(), host, port)
272+
273+
```
240274

241275
<!-- ROADMAP -->
242276
## Roadmap

lightbug_http/client.mojo

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
from collections import Dict
22
from utils import StringSlice
33
from memory import UnsafePointer
4-
from lightbug_http.net import default_buffer_size
4+
from lightbug_http.connection import TCPConnection, default_buffer_size, create_connection
55
from lightbug_http.http import HTTPRequest, HTTPResponse, encode
66
from lightbug_http.header import Headers, HeaderKey
7-
from lightbug_http.net import create_connection, TCPConnection
87
from lightbug_http.io.bytes import Bytes, ByteReader
98
from lightbug_http._logger import logger
109
from lightbug_http.pool_manager import PoolManager, PoolKey

lightbug_http/pool_manager.mojo

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ from builtin.value import StringableCollectionElement
44
from memory import UnsafePointer, bitcast, memcpy
55
from collections import Dict, Optional
66
from collections.dict import RepresentableKeyElement
7-
from lightbug_http.net import create_connection, TCPConnection, Connection
7+
from lightbug_http.connection import create_connection, TCPConnection, Connection
88
from lightbug_http._logger import logger
99
from lightbug_http._owning_list import OwningList
1010
from lightbug_http.uri import Scheme

tests/integration/udp/udp_client.mojo

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
from lightbug_http.net import dial_udp, UDPAddr
1+
from lightbug_http.connection import dial_udp
2+
from lightbug_http.address import UDPAddr
23
from utils import StringSlice
34

45
alias test_string = "Hello, lightbug!"

tests/integration/udp/udp_server.mojo

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
from lightbug_http.net import listen_udp, UDPAddr
1+
from lightbug_http.connection import listen_udp
2+
from lightbug_http.address import UDPAddr
23
from utils import StringSlice
34

45

testutils/utils.mojo

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ from lightbug_http.io.bytes import Bytes
33
from lightbug_http.error import ErrorHandler
44
from lightbug_http.uri import URI
55
from lightbug_http.http import HTTPRequest, HTTPResponse
6-
from lightbug_http.net import Listener, Addr, Connection, TCPAddr
6+
from lightbug_http.connection import Listener, Connection
7+
from lightbug_http.address import Addr, TCPAddr
78
from lightbug_http.service import HTTPService, OK
89
from lightbug_http.server import ServerTrait
910
from lightbug_http.client import Client

0 commit comments

Comments
 (0)