Skip to content

SVCD::ndispatch [NOT READY] #40

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
44 changes: 41 additions & 3 deletions natlib/svcd.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@


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

{ LSTRKEY( "svcd_init"), LFUNCVAL ( svcd_init ) \
{ LSTRKEY( "svcd_write"), LFUNCVAL ( svcd_write )},


//If this file is defining only specific functions, or if it
//is defining the whole thing
Expand All @@ -30,6 +31,7 @@ static const LUA_REG_TYPE svcd_meta_map[] =
// Maintainer: Michael Andersen <[email protected]>
/////////////////////////////////////////////////////////////

static int svcd_ndispatch( lua_State *L );
// The anonymous func in init that allows for dynamic binding of advert_received
static int svcd_init_adv_received( lua_State *L )
{
Expand Down Expand Up @@ -93,8 +95,13 @@ static int svcd_init( lua_State *L )
lua_getglobal(L, "SVCD");
printf("Put table at %d\n", lua_gettop(L));
#endif
//Now begins the part that corresponds with the lua init function

//Override symbols in the lua table with C implementation
lua_pushstring(L, "ndispatch");
lua_pushlightfunction(L, svcd_ndispatch);
lua_settable(L, 3);

//Now begins the part that corresponds with the lua init function
//SVCD.asock
lua_pushstring(L, "asock");
lua_pushlightfunction(L, libstorm_net_udpsocket);
Expand Down Expand Up @@ -183,5 +190,36 @@ static int svcd_init( lua_State *L )
lua_call(L, 3, 0);
}


return 0;
}

//////////////////////////////////////////////////////////////////////////////
// SVCD.ndispatch((string pay, number srcip, number srcport))
// Authors: Jennifer Dai, Prashan Dharmasena, Wenqin
/////////////////////////////////////////////////////////////

static int svcd_ndispatch( lua_State *L )
{
// Checks to make sure it is passed 3 parameters
if (lua_gettop(L) != 3) return luaL_error(L, "Expected (pay, srcip, srcport)");

size_t parlen;
const char* pay = lua_tolstring(L, -1, &parlen);
uint16_t ivkid = lua_tonumber(L, -1); // changes pay parameter to number and returns
lua_getglobal(L, "SVCD"); // gets table
lua_pushstring(L, "oursubs"); // use oursubs as the key
lua_gettable(L, 4);
lua_pushnumber(L, ivkid);
lua_pushstring(L, ivkidstr);
lua_gettable(L, 5);
Copy link
Contributor

Choose a reason for hiding this comment

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

Why are you using the string version of the ivkid? I have to check what the other groups are doing when using the ivkid as a key, but the Lua version just straight up used the number, not the string. String conversion is slow and unnecessary.

Copy link
Author

Choose a reason for hiding this comment

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

Okay, thanks! We fixed it.


size_t size;
const char* item = lua_tolstring(L, 6, &size);
if (!lua_isnil(L, 5)) {
lua_pushlstring(L, pay+3, parlen-3);
lua_pushstring(L, newstr); // pay
lua_call(L, 1, 0);
}
return 0;
}