Skip to content

SVCD::add_{service,attribute} [OK] #29

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 84 additions & 1 deletion natlib/svcd.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@


#define SVCD_SYMBOLS \
{ LSTRKEY( "svcd_init"), LFUNCVAL ( svcd_init ) },
{ LSTRKEY( "svcd_init"), LFUNCVAL ( svcd_init ) }, \
{ LSTRKEY( "svcd_add_service"), LFUNCVAL ( svcd_add_service )}, \
{ LSTRKEY( "svcd_add_attribute"), LFUNCVAL ( svcd_add_attribute )},


//If this file is defining only specific functions, or if it
Expand Down Expand Up @@ -185,3 +187,84 @@ static int svcd_init( lua_State *L )

return 0;
}

// Lua: storm.n.svcd_add_service ( svcd_id )
// Add a new service to the service daemon
// this must be called before SVCD.advertise()
Copy link

Choose a reason for hiding this comment

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

I know this comment was pulled from the lua version, but the current implementation advertises automatically so this is no longer needed (in either lua or c).

static int svcd_add_service( lua_State *L ) {

//TODO: Garbage collection?

lua_getglobal(L, "SVCD");

lua_pushstring(L, "blsmap");
lua_gettable(L, 2);
lua_pushvalue(L, 1); //svc_id @ index 4
lua_pushlightfunction(L, libstorm_bl_addservice);
lua_pushvalue(L, 1); //svc_id @ index 6
Copy link

Choose a reason for hiding this comment

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

I'm not sure what you're trying to do with the pushvalue() function. I see the comment says that you want to get svc_id that's at index 4 but you push the value that is at index 1. According to my understanding this should be pushing SVCD.blsmap onto the stack again since it is at index 1 of the stack.
EDIT: My understanding of the stack was wrong. This works correctly!
PB4J

lua_call(L, 1, 1);
lua_settable(L, 3);

lua_pushstring(L, "blamap");
lua_gettable(L, 2);
lua_pushvalue(L, 1); //svc_id @ index 5
lua_newtable(L);
lua_settable(L, 4);

lua_pushstring(L, "manifest");
lua_gettable(L, 2);
lua_pushvalue(L, 1); //svc_id @ index 6
lua_newtable(L);
lua_settable(L, 5);

lua_pushstring(L, "manifest_map");
lua_gettable(L, 2);
lua_pushvalue(L, 1); //svc_id @ index 7
lua_newtable(L);
lua_settable(L, 6);

return 0;
}

// Lua: storm.n.svcd_add_attribute ( svc_id, attr_id, write_fn )
// Add a new attribute to a service in the service daemon
static int svcd_add_attribute( lua_State *L ) {

lua_getglobal(L, "SVCD"); //Index 4

lua_pushstring(L, "blsmap"); //Index 5
lua_gettable(L, 4);

Copy link

Choose a reason for hiding this comment

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

It might be clearer to use -2 reference SVCD instead of 4
Holy Cow

Copy link

Choose a reason for hiding this comment

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

I think that you've got the numbering of elements on the stack wrong. You haven't pushed 4 elements onto the stack yet and you're trying to do a gettable of the 4th element. You probably shouldn't assume that there are elements already on the stack when you enter the function.
PB4J

Copy link

Choose a reason for hiding this comment

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

My bad. I didn't see that the function took in three attributes and that they'd be on the stack. I yet feel that the stack is addressed opposite to what you have done though.
EDIT: neglect these comments. My understanding of the stack was wrong
PB4J

lua_pushstring(L, "blamap"); //Index 6
lua_gettable(L, 4);
lua_pushvalue(L, 1); //svc_id @ Index 7
lua_gettable(L, 6);
lua_pushvalue(L, 2); //attr_id @ Index 8 - key

lua_pushlightfunction(L, libstorm_bl_addcharacteristic); //Index 9
lua_pushvalue(L, 1); //svc_id @ Index 10
lua_gettable(L, 5);
lua_pushvalue(L, 2); //attr_id @ Index 11
lua_pushvalue(L, 3); //wrtie_fn @ Index 12
lua_call(L, 3, 1); //Returns at Index 9 - value

lua_settable(L, 7); //Remove index 8, 9

lua_pushstring(L, "manifest"); //Index 8
lua_gettable(L, 4);
lua_pushvalue(L, 1); //svc_id @ Index 9
lua_gettable(L, 8);
int n = luaL_getn(L, 9);
lua_pushvalue(L, 2); //attr_id @ Index 10
lua_rawseti(L, 9, n+1); //Index back to 9

lua_pushstring(L, "manifest_map"); // Index 10
lua_gettable(L, 4);
lua_pushvalue(L, 1); //svc_id @ Index 11
lua_gettable(L, 10);
lua_pushvalue(L, 2); //attr_id @ Index 12
lua_pushvalue(L, 3); // write_fn @ Index 13
lua_settable(L, 11); //Index back to 11

return 0;
}