Skip to content
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

Add Toit version of the external messaging #2291

Merged

Conversation

floitsch
Copy link
Member

@floitsch floitsch commented May 8, 2024

No description provided.

@floitsch floitsch requested a review from kasperl May 8, 2024 13:40
@cla-bot cla-bot bot added the cla-signed The contributors have signed the CLA label May 8, 2024
import io
import rpc

class ExternalMessageHandler_ implements SystemMessageHandler_:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd move this below the public bits.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

set-system-message-handler_ TYPE this

close -> none:
clear-system-message-handler_ TYPE
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we clear the handler here? It would have to be nullable, which may be annoying.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

obsolete.


externals_ := {:}

class External:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this will naturally be called external.External? It's a bit weird.

Client, Target, Reference ...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed to 'Client'.

pid := pid-for-external-id_ id
if pid == -1: return null
result = External.private_ pid id
externals_[id] = result
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure I understand the design here. You're caching instances of External, but they come with a close. That feels shaky. Can you ever reopen them?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As discussed in person. That was all wrong.
should be better now.

if not external-message-handler_: return
external-message-handler_.close
external-message-handler_ = null

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Drop empty line.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.


close -> none:
if is-closed_: return
clear-notification-handler
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So if you close any External, you drop the shared external message handler?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

was all wrong.
done.


set-notification-handler handler/Lambda -> none:
if is-closed_: throw "CLOSED"
if not external-message-handler_:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You only want one of these open as long as you have any External instances, no?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

was all wrong.
should be better now.

is-closed -> bool:
return is-closed_

byte-message_ message/io.Data --copy/bool -> ByteArray:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

static? Or a 'this is a helper' comment.

Maybe call it encode-byte-message_?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Went with encode-message_.

Copy link
Member

@kasperl kasperl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me.

test c-lib #[99, 99]

c-lib.close

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Drop one newline.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

obsolete.


constructor.private_ .pid .id:

static open id/string -> Client?:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we make it possible to set the handler when you open so you don't drop notifications?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done


constructor.private_ .pid .id .notification-callback_:

static open id/string --notification-callback/Lambda?=null -> Client?:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should use on-notify or on-message rather than notification-callback?

bytes := encode-message_ message --copy=copy
return rpc.invoke pid function bytes

set-notification-callback callback/Lambda? -> none:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
set-notification-callback callback/Lambda? -> none:
set-on-notify callback/Lambda? -> none:

or

Suggested change
set-notification-callback callback/Lambda? -> none:
set-on-message callback/Lambda? -> none:

if client.notification-callback_:
client.notification-callback_.call argument

static start-if-necessary -> none:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe just merge the start/stop functions?

clients.do: | client/external.Client |
e := catch:
client.request 0 #[99, 99]
expect-equals "EXTERNAL-ERROR" e
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be "EXTERNAL_ERROR" for consistency with other errors?


static void __attribute__((constructor)) init2() {
printf("registering external handler 1\n");
test_service_t* test_service = (test_service_t*)malloc(sizeof(test_service_t));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having a toit_malloc that does GC if necessary would make this a bit more robust. It actually doesn't matter here, because this only runs on startup, but it is something to consider for the other places where we need to allocate.

@floitsch floitsch merged commit 4d21470 into floitsch/external-messaging May 13, 2024
24 of 25 checks passed
@floitsch floitsch deleted the floitsch/external-messaging.type.toit branch May 13, 2024 08:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla-signed The contributors have signed the CLA
Development

Successfully merging this pull request may close these issues.

None yet

2 participants