Skip to content

Commit 1cf5f20

Browse files
committed
Basic session envelope docs
1 parent 63d4623 commit 1cf5f20

File tree

3 files changed

+95
-12
lines changed

3 files changed

+95
-12
lines changed

README.md

Lines changed: 87 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ not := msg.FailedNotification(&lime.Reason{Code: 1, Description: "Destination no
358358

359359
### Command
360360

361-
The command envelope is used to **manipulate resources of a server**.
361+
The command envelope is used to **manipulate resources of a remote node**.
362362
It provides a REST capable interface, with a URI and methods (verbs), much like the HTTP protocol.
363363
It also supports multiplexing, so the connection is not blocked when a request is sent.
364364

@@ -446,14 +446,96 @@ In case of `failure` response status, the command should have the `reason` prope
446446
For creating a request command in Go, you can use the `lime.RequestCommand` type:
447447

448448
```go
449-
cmd := &lime.RequestCommand{}
450-
cmd.SetURIString("/contacts").
449+
reqCmd := &lime.RequestCommand{}
450+
reqCmd.SetURIString("/contacts").
451451
SetMethod(lime.CommandMethodGet).
452452
SetID(lime.NewEnvelopeID())
453453
```
454454

455455
Note that for the `id` value, we are using the value returned by the `lime.NewEnvelopeID()` function, which will return
456456
a UUID v4 string (something like `3cdd2654-911d-497e-834a-3b7865510155`).
457457

458-
If you are building a server, you can add handlers for specific commands using the `RequestCommandHandler*` methods from
459-
the `lime.Client` and `lime.Server` types.
458+
For sending a command request, you should use the `ProcessCommand` method from the `lime.Client` and `lime.Server`
459+
types, instead of the `SendCommand` method. The `ProcessCommand` method take care of await for the correspondent
460+
response command.
461+
462+
```go
463+
respCmd, err := client.ProcessCommand(context.Background(), reqCmd)
464+
if err == nil {
465+
// TODO: Handle the response
466+
}
467+
```
468+
469+
In the server side, you can add handlers for specific commands using the `RequestCommandHandler*` methods from
470+
the `lime.Server` type.
471+
472+
```go
473+
server := lime.NewServerBuilder().
474+
RequestCommandHandlerFunc(
475+
// Set a predicate for filtering only the get contacts commands
476+
func(cmd *lime.RequestCommand) bool {
477+
return cmd.Method == lime.CommandMethodGet && cmd.URI.Path() == "/contacts"
478+
},
479+
// The handler implementation
480+
func(ctx context.Context, cmd *lime.RequestCommand, s lime.Sender) error {
481+
// Create a document collection of contacts
482+
contacts := &lime.DocumentCollection{
483+
Total: 2,
484+
ItemType: chat.MediaTypeContact(),
485+
Items: []lime.Document{
486+
&chat.Contact{Name: "John Doe"},
487+
&chat.Contact{Name: "Mary"},
488+
},
489+
}
490+
// Send the response to the sender
491+
respCmd := cmd.SuccessResponseWithResource(contacts)
492+
return s.SendResponseCommand(ctx, respCmd)
493+
}).
494+
// TODO: Setup other server options
495+
Build()
496+
```
497+
498+
This is also valid for the `lime.Client` type.
499+
In Lime, the **client can receive and process commands requests** from other nodes, like the server.
500+
501+
### Session
502+
503+
The session envelope is used for the negotiation, authentication and establishment of the communication channel between
504+
the client and a server.
505+
It helps select the transport options, like compression and encryption (TLS), authentication credentials, and session
506+
metadata, like its `id` and local/remote node addresses.
507+
508+
For instance, the first envelope sent in every like session is the **new session** envelope, which the client sends to
509+
the server after the transport connection is established:
510+
511+
```json
512+
{
513+
"state": "new"
514+
}
515+
```
516+
The server should reply this with another session envelope, according to the session state that it wants to enforce.
517+
518+
For instance, the server may want to present the client the transport options for negotiation.
519+
This is useful for applying the TLS encryption to an unencrypted connection (the TCP connection starts without
520+
encryption by default).
521+
522+
```json
523+
{
524+
"id": "0676a702-a7d6-43e6-947f-bde3c3e25eb5",
525+
"from": "server@localhost/s1",
526+
"state": "negotiating",
527+
"compressionOptions": ["none"],
528+
"encryptionOptions": ["none", "tls"]
529+
}
530+
```
531+
532+
Note that this envelope haves a `id` defined, which is the **session id**.
533+
The next session envelopes sent by the client should use this same id, until the end of the session.
534+
535+
The session state progression can occur in the following order:
536+
1. new (client started)
537+
2. negotiating (optional)
538+
3. authenticating (optional)
539+
4. established
540+
5. finishing (optional, client started)
541+
6. finished OR failed

examples/server/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func main() {
3333
}).
3434
RequestCommandHandlerFunc(
3535
func(cmd *lime.RequestCommand) bool {
36-
return cmd.URI.Path() == "/presence"
36+
return cmd.Method == lime.CommandMethodSet && cmd.URI.Path() == "/presence"
3737
},
3838
func(ctx context.Context, cmd *lime.RequestCommand, s lime.Sender) error {
3939
return s.SendResponseCommand(

session.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,17 @@ import (
77
"fmt"
88
)
99

10-
// Session Allows the configuration and establishment of the communication channel between nodes.
10+
// The Session envelope is used for the negotiation, authentication and establishment of the communication channel
11+
// between the client and a server.
1112
type Session struct {
1213
Envelope
13-
// State Informs or changes the state of a session.
14+
// State informs or changes the state of a session.
1415
// Only the server can change the session state, but the client can request
1516
// the state transition.
1617
State SessionState
17-
// EncryptionOptions Options provided by the server during the session negotiation.
18+
// EncryptionOptions represent the options provided by the server during the session negotiation.
1819
EncryptionOptions []SessionEncryption
19-
// Encryption The encryption option selected for the session.
20+
// Encryption represents the encryption option selected for the session.
2021
// This property is provided by the client in the negotiation and by the
2122
// server in the confirmation after that.
2223
Encryption SessionEncryption
@@ -32,8 +33,8 @@ type Session struct {
3233
// The authentication scheme option selected for the session.
3334
// This property must be present if the property authentication is defined.
3435
Scheme AuthenticationScheme
35-
// RawAuthentication data, related To the selected schema.
36-
// Information like password sent by the client or roundtrip data sent by the server.
36+
// Authentication data, related To the selected schema.
37+
// Information like password sent by the client or round-trip data sent by the server.
3738
Authentication Authentication
3839
// In cases where the client receives a session with failed state,
3940
// this property should provide more details about the problem.

0 commit comments

Comments
 (0)