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

SVCD::subdispatch [NOT READY] #34

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
16 changes: 16 additions & 0 deletions app/svcdTest.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
require "cord"
sh = require "stormsh"

-- start a coroutine that provides a REPL
sh.start()

require "svcd"
require "pprint"

storm.n.svcd_init("kavan", function() print("ready to go") end)
-- SVCD.add_service(12)
-- SVCD.add_attribute(12,1,function() print("someone wrote to attribute") end)

-- enter the main event loop. This puts the processor to sleep
-- in between events
cord.enter_loop()
21 changes: 21 additions & 0 deletions lib/svcd/svcd.lua
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,27 @@ SVCD.add_attribute = function(svc_id, attr_id, write_fn)
SVCD.manifest_map[svc_id][attr_id] = write_fn
end

SVCD.test_subdispatch = function()
SVCD.subscribers = {}
local msg = storm.array.create(7,storm.array.UINT8)
msg:set(1, 1)
msg:set_as(storm.array.UINT16, 1, svcid)
msg:set_as(storm.array.UINT16, 3, attrid)
msg:set_as(storm.array.UINT16, 5, ivkid)
SVCD.subdispatch(msg,"ff01::1",2343)
outputTable = SVCD.subscribers

SVCD.subscribers = {}
storm.n.subdispatch(msg,"ff01::1",2343)
nativeOutputTable = SVCD.subscribers

require "pprint"
print("\n lua:\n")
ppNestedTable(outputTable)
print("\n native:\n")
ppNestedTable(nativeOutputTable)
end

SVCD.subdispatch = function(pay, srcip, srcport)
local parr = storm.array.fromstr(pay)
local cmd = parr:get(1)
Expand Down
1 change: 0 additions & 1 deletion native.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
#include "natlib/util.c"
#include "natlib/svcd.c"


////////////////// BEGIN FUNCTIONS /////////////////////////////

int contrib_fourth_root_m1000(lua_State *L) //mandatory signature
Expand Down
92 changes: 87 additions & 5 deletions natlib/svcd.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
//This file is included into native.c
#include "libstormarray.h"
Copy link

Choose a reason for hiding this comment

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

This will be included in native.c by #30

men in #000000

#include "lrodefs.h"

static int svcd_subdispatch(lua_State *L);

#define SVCD_SYMBOLS \
{ LSTRKEY( "svcd_init"), LFUNCVAL ( svcd_init ) },
//This file is included into native.c

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

//If this file is defining only specific functions, or if it
//is defining the whole thing
Expand Down Expand Up @@ -143,8 +147,7 @@ static int svcd_init( lua_State *L )
lua_pushstring(L, "subsock");
lua_pushlightfunction(L, libstorm_net_udpsocket);
lua_pushnumber(L, 2530);
lua_pushstring(L, "subdispatch");
lua_gettable(L, 3);
lua_pushlightfunction(L, svcd_subdispatch);
lua_call(L, 2, 1);
lua_settable(L, 3); //Store

Expand Down Expand Up @@ -185,3 +188,82 @@ static int svcd_init( lua_State *L )

return 0;
}


//////////////////////////////////////////////////////////////////////////////
// SVCD.subdispatch() implementation
// Maintainer: Running with scissors
//////////////////////////////////////////////////////////////////////////////

static int svcd_subdispatch(lua_State *L) {
//local parr = storm.array.fromstr(pay)
size_t len;
uint8_t *parr __attribute__((aligned(2))) = lua_tolstring(L, 1, &len);
uint8_t *srcip = lua_tostring(L, 2);
uint32_t strcport = lua_tointeger(L, 3);

//local cmd = parr:get(1);
uint8_t cmd = parr[0];
//local svc_id = parr:get_as(storm.array.UINT16,1);
int16_t svc_id = *(int16_t*)(parr+1);
Copy link
Contributor

Choose a reason for hiding this comment

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

You cannot do an unaligned access like this on ARM. See my other ranty comments for people doing unaligned writes.

//local attr_id = parr:get_as(storm.array.UINT16,3);
int16_t attr_id = *(int16_t*)(parr+3);
//local ivkid = parr:get_as(storm.array.UINT16, 5);
int16_t ivkid = *(int16_t*)(parr+5);

lua_getglobal(L, "SVCD");
lua_pushstring(L, "subscribers");
lua_gettable(L, -2);
lua_pushinteger(L, svc_id);
lua_gettable(L, -2);
//stack: SVCD, SVCD[subscribers], SVCD[subscribers][svc_id]
if (cmd == 1){ //subscribe command
if (lua_isnil(L,-1)){ // if (SVCD.subscribers[svc_id] == nil){
lua_pop(L, 1); //stack: SVCD, SVCD[subscribers]
//SVCD.subscribers[svc_id] = {};
lua_newtable(L);
lua_pushinteger(L, svc_id);
lua_pushvalue(L, -2);
//stack: SVCD, SVCD[subscribers], <newTable>, svc_id, <newTable>
lua_settable(L, -4);
}
//stack: SVCD, SVCD[subscribers], SVCD[subscribers][svc_id],
//if (SVCD.subscribers[svc_id][attr_id] == nil){
lua_pushstring(L,"attr_id");
lua_gettable(L, -2);
if (lua_isnil(L, -1)){
//SVCD.subscribers[svc_id][attr_id] = {}
lua_pop(L, 1);
lua_newtable(L);
lua_pushinteger(L, attr_id);
lua_pushvalue(L, -2);
lua_settable(L, -4);
}
//stack: SVCD[subscribers], SVCD[subscribers][svc_id], SVCD[subscribers][svc_id][attr_id]

//SVCD.subscribers[svc_id][attr_id][srcip] = ivkid;
lua_pushstring(L, srcip);
lua_pushnumber(L, ivkid);
lua_settable(L, -3);

}else if (cmd == 0){ //unsubscribe command

Choose a reason for hiding this comment

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

in svcd.lua, 2 is the unsubscribe command, not 0.
Fantastic Four

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes, in subdispatch it is. But in SVCD.unsubscribe it is 0 so we changed it here so everything would work

Copy link
Contributor

Choose a reason for hiding this comment

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

Was this a bug in unsubscribe?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think it was a bug in the program as a whole, I'm not sure which they meant it to be

Copy link
Contributor

Choose a reason for hiding this comment

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

I meant it to be 2, but I will fix it when I merge, as this is my bad.

//stack: SVCD, SVCD[subscribers], SVCD[subscribers][svc_id]

//if (SVCD.subscribers[svc_id] == nil){
if (lua_isnil(L, -1)){
return 0;
}
//if (SVCD.subscribers[svc_id][attr_id] == nil){
lua_pushnumber(L, attr_id);
lua_gettable(L, -2);
if (lua_isnil(L, -1)){
return 0;
}
//stack: SVCD, SVCD[subscribers], SVCD[subscribers][svc_id], SVCD[subscribers][svc_id][attr_id]
//SVCD.subscribers[svc_id][attr_id][srcip] = nil;
lua_pushstring(L, srcip);
lua_pushnil(L);
lua_settable(L, -3);
}
return 0;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Looks good. Good job on doing the comments. Line 224-228 is fantastic! Took me a while to understand the reasoning.

Untitled