Add Network draft. Format Vector

This commit is contained in:
Kirigaya Kazuto 2017-10-11 22:20:32 +08:00
parent 81d5a2a80e
commit 1d37ecf526
3 changed files with 133 additions and 56 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
*.exe

59
libnetwork.lua Normal file
View File

@ -0,0 +1,59 @@
function socket() -- Allocate a new socket
end
function connect(sfd,remote_tag,port) -- Connect to a remote device
end
function bind(sfd,port) -- Bind socket at specific port
end
function listen(sfd,sz) -- Set size of queue for waiting connections
end
function accept(sfd) -- Accept Connection
end
function send(sfd,...) -- Standard Network I/O
end
function recv(sfd) -- Standard Network I/O
end
function shutdown(sfd) -- Close Socket
end
function close(sfd) -- Close Socket
end
function do_dhcp_client() -- Connect to DHCP Server and try to get a tag.
end
function do_arp_broadcast() -- ARP: Broadcast tag and uuid information of this device
end
function arp_listener() -- ARP: Listen to arp broadcast and record informations. Notice that this listener also replies to specific arp-request
end
function do_arp_query() -- ARP: Query uuid with tag, will send arp-request
end
function run_arp() -- Start ARP Services in background
end
function run_dhcp_client() -- Start DHCP Client in background
end

View File

@ -1,93 +1,109 @@
require("class") require("class")
Vector=class("Vector") Vector = class("Vector")
function Vector:_reset() function Vector:_reset()
self.sz=0 self.sz = 0
self.bus={} self.bus = {}
end end
function Vector:ctor(t) function Vector:ctor(t)
self:_reset() self:_reset()
if((t ~= nil) and (type(t) == "table")) then if ((t ~= nil) and (type(t) == "table")) then
for k,v in pairs(t) do for k, v in pairs(t) do
local x={} local x = {}
x.first=k x.first = k
x.second=v x.second = v
self:push_back(x) self:push_back(x)
end --for end --for
end --if end --if
end --func end --func
function Vector:empty() function Vector:empty()
return self.sz==0 return self.sz == 0
end end
function Vector:push_back(val) function Vector:push_back(val)
self.bus[self.sz]=val self.bus[self.sz] = val
self.sz=self.sz+1 self.sz = self.sz + 1
end end
function Vector:pop_back() function Vector:pop_back()
if(self:empty()) then return nil if (self:empty()) then
else return nil
local val=self.bus[self.sz-1] else
self.bus[self.sz-1]=nil local val = self.bus[self.sz - 1]
self.sz=self.sz-1 self.bus[self.sz - 1] = nil
return val self.sz = self.sz - 1
end return val
end
end end
function Vector:front() function Vector:front()
if(self:empty()) then return nil if (self:empty()) then
else return self.bus[0] return nil
end else
return self.bus[0]
end
end end
function Vector:back() function Vector:back()
if(self:empty()) then return nil if (self:empty()) then
else return self.bus[self.sz-1] return nil
end else
return self.bus[self.sz - 1]
end
end end
function Vector:clear() function Vector:clear()
self:_reset() self:_reset()
end end
function Vector:at(index) function Vector:at(index)
if(type(index) ~= "number") then return nil if (type(index) ~= "number") then
elseif(self:empty()) then return nil return nil
elseif(index<0) then return nil elseif (self:empty()) then
elseif(index>=self.sz) then return nil return nil
else return self.bus[index] elseif (index < 0) then
end return nil
elseif (index >= self.sz) then
return nil
else
return self.bus[index]
end
end end
function Vector:size() function Vector:size()
return self.sz return self.sz
end end
function Vector:insert_after(index,val) function Vector:insert_after(index, val)
if(type(index) ~= "number") then return nil if (type(index) ~= "number") then
elseif(index>=self.sz) then return nil return nil
elseif(index<-1) then return nil elseif (index >= self.sz) then
else return nil
for i=self.sz-1,index+1,-1 do elseif (index < -1) then
self.bus[i+1]=self.bus[i] return nil
end else
self.bus[index+1]=val for i = self.sz - 1, index + 1, -1 do
self.sz=self.sz+1 self.bus[i + 1] = self.bus[i]
end end
self.bus[index + 1] = val
self.sz = self.sz + 1
end
end end
function Vector:erase(index) function Vector:erase(index)
if(type(index) ~= "number") then return nil if (type(index) ~= "number") then
elseif(index>=self.sz) then return nil return nil
elseif(index<0) then return nil elseif (index >= self.sz) then
else return nil
for i=index+1,self.sz-1,1 do elseif (index < 0) then
self.bus[i-1]=self.bus[i] return nil
end else
self.sz=self.sz-1 for i = index + 1, self.sz - 1, 1 do
end self.bus[i - 1] = self.bus[i]
end
self.sz = self.sz - 1
end
end end