-
Notifications
You must be signed in to change notification settings - Fork 30
SVCD::write [NOT READY] #39
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
cearto
wants to merge
6
commits into
UCB-IoET:master
Choose a base branch
from
AparnaDhinakaran:svcd_write
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
df6d843
SVCD_write pull request cleanup'
cearto 5ee7166
Removed comments to init
cearto d34d735
Cleaned up code and checked for parameters.
Michaelhobo 6adebb5
removed extra resolve_table
Michaelhobo da709b1
svcdwrite
cearto 5951c35
Removed print messages
cearto File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,9 @@ | |
|
||
|
||
#define SVCD_SYMBOLS \ | ||
{ LSTRKEY( "svcd_init"), LFUNCVAL ( svcd_init ) }, | ||
{ LSTRKEY( "svcd_init"), LFUNCVAL ( svcd_init ) },\ | ||
{ LSTRKEY( "svcd_test"), LFUNCVAL ( svcd_test ) },\ | ||
{ LSTRKEY( "svcd_write"), LFUNCVAL ( svcd_write )}, | ||
|
||
|
||
//If this file is defining only specific functions, or if it | ||
|
@@ -29,6 +31,7 @@ static const LUA_REG_TYPE svcd_meta_map[] = | |
// SVCD.init() implementation | ||
// Maintainer: Michael Andersen <[email protected]> | ||
///////////////////////////////////////////////////////////// | ||
static int svcd_write( 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 ) | ||
|
@@ -91,10 +94,14 @@ static int svcd_init( lua_State *L ) | |
//Load the SVCD table that Lua created | ||
//This will be index 3 | ||
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 | ||
|
||
//SVCD.write | ||
lua_pushstring(L, "write"); | ||
lua_pushlightfunction(L, svcd_write); | ||
lua_settable(L, 3); // Store it in the table | ||
|
||
//SVCD.asock | ||
lua_pushstring(L, "asock"); | ||
lua_pushlightfunction(L, libstorm_net_udpsocket); | ||
|
@@ -185,3 +192,158 @@ static int svcd_init( lua_State *L ) | |
|
||
return 0; | ||
} | ||
|
||
////////////////////////////////////////////////////////////////////////////// | ||
// SVCD.write((string targetip, number svcid, number attrid, lightfunction payload, | ||
// number timeout_ms, lightfunction on_done)) | ||
// Authors: Aparna Dhinakaran, Michael Ho, Romi Phadte, Cesar Torres | ||
///////////////////////////////////////////////////////////// | ||
|
||
void resolve_table(lua_State *L, char* key){ | ||
// push the global table, resolve it and then pop it off the stack | ||
lua_getglobal(L, "SVCD"); | ||
lua_pushstring(L, key); | ||
lua_gettable(L, -2); | ||
lua_insert(L, -2); | ||
lua_settop(L, -2); | ||
} | ||
|
||
static int svcd_test(lua_State* L){ | ||
int numargs = lua_gettop(L); | ||
if (numargs == 2) { | ||
int timeout_ms = (int)lua_tonumber(L, 1); | ||
|
||
lua_pushlightfunction(L, libstorm_os_invoke_later); | ||
lua_pushnumber(L, timeout_ms * SECOND_TICKS); | ||
lua_pushvalue(L, 2); | ||
lua_call(L, 2, 0); | ||
} | ||
return 0; | ||
} | ||
|
||
|
||
static int timeout_func(lua_State* L){ | ||
int ivkid = lua_tonumber(L, lua_upvalueindex(1)); | ||
resolve_table(L, "TIMEOUT"); | ||
int timeout = lua_tonumber(L, -1); | ||
|
||
// SVCD.handlers[ivkid] ~= nil | ||
resolve_table(L, "handlers"); | ||
lua_pushnumber(L, ivkid); | ||
lua_gettable(L, -2); | ||
|
||
if(!lua_isnil(L, -1)){ | ||
lua_pushnumber(L, timeout); | ||
lua_call(L, 1, 0); | ||
|
||
resolve_table(L, "handlers"); | ||
lua_pushnumber(L, ivkid); | ||
lua_gettable(L, -2); | ||
lua_pushnil(L); | ||
lua_settable(L, -2); | ||
} | ||
return 1; | ||
} | ||
|
||
static int svcd_write( lua_State *L ) | ||
{ | ||
|
||
int numargs = lua_gettop(L); // 6 | ||
if (numargs == 6) { | ||
/* GET ALL THE ARGS */ | ||
size_t g; | ||
const char* targetip = (char*) lua_tolstring(L, 1, &g); | ||
int svcid = (int)lua_tonumber(L, 2); | ||
int attrid = (int)lua_tonumber(L, 3); | ||
|
||
size_t l; | ||
const char* payload = lua_tolstring(L, 4, &l); | ||
|
||
int timeout_ms = (int)lua_tonumber(L, 5); | ||
|
||
/* SET IVKID */ | ||
resolve_table(L, "ivkid"); //7 | ||
int ivkid = (int) lua_tonumber(L, -1); | ||
|
||
|
||
ivkid = ivkid + 1; | ||
if( ivkid > 65535 ) { | ||
ivkid = 0; | ||
} | ||
|
||
lua_settop(L, numargs); | ||
|
||
// setting ivkid | ||
lua_getglobal(L, "SVCD"); | ||
lua_pushnumber(L, ivkid); | ||
lua_setfield(L, -2, "ivkid"); | ||
lua_settop(L, numargs); | ||
/* END SET IVKID*/ | ||
|
||
/* SET HANDLER */ | ||
resolve_table(L, "handlers"); // 7 | ||
lua_pushnumber(L, ivkid); | ||
lua_pushvalue(L, 6); // the ondone function | ||
lua_settable(L, -3); | ||
/* END SET HANDLER */ | ||
|
||
// /* LUA WRITE INVOKE */ | ||
// resolve_table(L, "write_invoke_later"); // 9 | ||
// //function write invoke later | ||
// lua_pushnumber(L, ivkid); // 10 | ||
// lua_pushnumber(L, timeout_ms); | ||
// lua_call(L, 2, 0); | ||
|
||
lua_pushlightfunction(L, libstorm_os_invoke_later); | ||
lua_pushnumber(L, timeout_ms * MILLISECOND_TICKS); | ||
lua_pushnumber(L, ivkid); | ||
lua_pushcclosure(L, &timeout_func, 1); | ||
|
||
lua_call(L, 2, 0); | ||
|
||
/* END LUA WRITE INVOKE */ | ||
|
||
|
||
/* SEND TO */ | ||
lua_pushlightfunction(L, libstorm_net_sendto); | ||
resolve_table(L, "wcsock"); | ||
/* BEGIN MESSAGE PACK */ | ||
lua_pushlightfunction(L, libmsgpack_mp_pack); | ||
lua_newtable(L); | ||
//svcid | ||
lua_pushnumber(L, 1); | ||
lua_pushnumber(L, svcid); | ||
lua_settable(L, -3); | ||
//attrid | ||
lua_pushnumber(L, 2); | ||
lua_pushnumber(L, attrid); | ||
lua_settable(L, -3); | ||
|
||
// ivkid | ||
lua_pushnumber(L, 3); | ||
lua_pushnumber(L, ivkid); //14 | ||
lua_settable(L, -3); | ||
|
||
//payload | ||
lua_pushnumber(L, 4); | ||
lua_pushlstring(L, payload, l); | ||
lua_settable(L, -3); | ||
|
||
lua_call(L, 1, 1); | ||
|
||
/* END OF MSG PACK FUNCTION */ | ||
|
||
// targetip | ||
lua_pushlstring(L, targetip, g); | ||
//2526 | ||
lua_pushnumber(L, 2526); | ||
|
||
lua_call(L, 4, 0); | ||
/* END SEND_TO */ | ||
} | ||
printf("End\n"); | ||
return 0; //return # of arguments | ||
} | ||
|
||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good job on writing the function. I think it would be better to remove commented code, print statements, etc from the code before we can merge it into master.
Untitled