From 1d37ecf52628e2120fa8e84d26441488f952ccff Mon Sep 17 00:00:00 2001 From: kiritow <1362050620@qq.com> Date: Wed, 11 Oct 2017 22:20:32 +0800 Subject: [PATCH] Add Network draft. Format Vector --- .gitignore | 2 + libnetwork.lua | 59 +++++++++++++++++++++++ vector.lua | 128 +++++++++++++++++++++++++++---------------------- 3 files changed, 133 insertions(+), 56 deletions(-) create mode 100644 .gitignore create mode 100644 libnetwork.lua diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..bafe145 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*.exe + diff --git a/libnetwork.lua b/libnetwork.lua new file mode 100644 index 0000000..a9c25b2 --- /dev/null +++ b/libnetwork.lua @@ -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 \ No newline at end of file diff --git a/vector.lua b/vector.lua index 278a876..d36cedc 100644 --- a/vector.lua +++ b/vector.lua @@ -1,93 +1,109 @@ require("class") -Vector=class("Vector") +Vector = class("Vector") function Vector:_reset() -self.sz=0 -self.bus={} + self.sz = 0 + self.bus = {} end function Vector:ctor(t) -self:_reset() -if((t ~= nil) and (type(t) == "table")) then -for k,v in pairs(t) do -local x={} -x.first=k -x.second=v -self:push_back(x) -end --for -end --if + self:_reset() + if ((t ~= nil) and (type(t) == "table")) then + for k, v in pairs(t) do + local x = {} + x.first = k + x.second = v + self:push_back(x) + end --for + end --if end --func function Vector:empty() -return self.sz==0 + return self.sz == 0 end function Vector:push_back(val) -self.bus[self.sz]=val -self.sz=self.sz+1 + self.bus[self.sz] = val + self.sz = self.sz + 1 end function Vector:pop_back() -if(self:empty()) then return nil -else -local val=self.bus[self.sz-1] -self.bus[self.sz-1]=nil -self.sz=self.sz-1 -return val -end + if (self:empty()) then + return nil + else + local val = self.bus[self.sz - 1] + self.bus[self.sz - 1] = nil + self.sz = self.sz - 1 + return val + end end function Vector:front() -if(self:empty()) then return nil -else return self.bus[0] -end + if (self:empty()) then + return nil + else + return self.bus[0] + end end function Vector:back() -if(self:empty()) then return nil -else return self.bus[self.sz-1] -end + if (self:empty()) then + return nil + else + return self.bus[self.sz - 1] + end end function Vector:clear() -self:_reset() + self:_reset() end function Vector:at(index) -if(type(index) ~= "number") then return nil -elseif(self:empty()) then return nil -elseif(index<0) then return nil -elseif(index>=self.sz) then return nil -else return self.bus[index] -end + if (type(index) ~= "number") then + return nil + elseif (self:empty()) then + return nil + elseif (index < 0) then + return nil + elseif (index >= self.sz) then + return nil + else + return self.bus[index] + end end function Vector:size() -return self.sz + return self.sz end -function Vector:insert_after(index,val) -if(type(index) ~= "number") then return nil -elseif(index>=self.sz) then return nil -elseif(index<-1) then return nil -else -for i=self.sz-1,index+1,-1 do -self.bus[i+1]=self.bus[i] -end -self.bus[index+1]=val -self.sz=self.sz+1 -end +function Vector:insert_after(index, val) + if (type(index) ~= "number") then + return nil + elseif (index >= self.sz) then + return nil + elseif (index < -1) then + return nil + else + for i = self.sz - 1, index + 1, -1 do + self.bus[i + 1] = self.bus[i] + end + self.bus[index + 1] = val + self.sz = self.sz + 1 + end end function Vector:erase(index) -if(type(index) ~= "number") then return nil -elseif(index>=self.sz) then return nil -elseif(index<0) then return nil -else -for i=index+1,self.sz-1,1 do -self.bus[i-1]=self.bus[i] -end -self.sz=self.sz-1 -end + if (type(index) ~= "number") then + return nil + elseif (index >= self.sz) then + return nil + elseif (index < 0) then + return nil + else + for i = index + 1, self.sz - 1, 1 do + self.bus[i - 1] = self.bus[i] + end + self.sz = self.sz - 1 + end end