Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Variables
- local networkDeviceSide = "left" --Change this to where network device is
- deviceType = "default"
- ipAddress = {-1,-1,-1} --We aren't working with subnets yet
- routerID = -1 --The computer ID of the router, allowing for responses to be returned
- message = {}
- packet = {}
- localFunctions = {}
- itemList = {}
- importExportList = {}
- storageList = {}
- inventoryManagerList = {}
- inventoryManagerExportDirection = "up"
- inventoryManagerImportDirection = "front"
- --[[
- All data packets have a set structure made of four sections
- First Section - IP Address of target system
- Second Section - IP Address of sending system
- Third Section - Type of packet being sent (what protocol does packet follow)
- Fourth Section - The main data the packet is transporting
- For testing, the IP address will be the same as the device ID.
- In the future this will be changed to allow for proper routing and
- security measures.
- The protocol the packet follows is a string in all caps
- (CDP,ERP,DRP,UPP,etc) to allow for universal understanding (and style)
- An IP address of 0 for the target system means that the packet is for
- the router to work with. Trying to assign a different device this IP will fail
- as the router will intercept all packets with that IP.
- --]]
- local function sendPacket (
- targetIP, --Array
- protocol, --String
- data --Any
- )
- --[[
- This function sends a packet to an IP address in the network.
- Each packet needs to follow the rules of packet construction.
- If the target IP is 0 then the packet will be sent to the default router.
- If the sending IP is -1 then that means the IP hasn't been set yet
- and will need to be to recieve packets from devices other than the router.
- --]]
- --Checking if there is a default router to send packet to, otherwise skip all code
- if routerID == -1 then
- print("Default router not set")
- goto skip
- end
- --Checking if packet components have the right data types, otherwise skip all code
- if not (type(targetIP) == "table") or not (type(protocol) == "string") then
- print("Improperly formatted packet")
- goto skip
- end
- --Packet construction
- local packet = {
- targetIP,
- ipAddress,
- protocol,
- data
- }
- --Sending packet
- print(targetIP[1].."."..targetIP[2].."."..targetIP[3].. " " .. ipAddress[1].."."..ipAddress[2].."."..ipAddress[3] .. " " .. protocol .. " " .. data)
- rednet.send(routerID,packet)
- ::skip::
- end
- local function responseRDP ()
- --[[
- Router Discovery Protocol
- Used by a router to discover other routers and build up a internetwork routing table.
- After recieving a routing table, start adding it to router routing table
- Routing table info:
- 1 - IP of router
- 2 - ID of next hop
- 3 - IP of next hop? (for future tracert)
- 4 - Number of hops to IP (To find shortest route)
- Routers will not have themselves on their own routing tables
- --]]
- end
- local function responseCDP ()
- --[[
- Computer Discovery Protocol
- Used by a router to discover devices on the network then assign them IP addresses
- and set itself as the device's default router. Devices already with an IP and
- default router do not respond to CDP packets to avoid confusion on the side of
- the router.
- --]]
- --If the device already has an IP and assigned Router then skip all code
- if not (ipAddress == {-1,-1,-1}) and not (routerID == -1) then goto skip end
- --Sets the device's default router to the device ID that sent the packet
- routerID = message[1]
- --Send CDP packet to router (IP 0) with the ID of the device
- sendPacket({0,0,0},"CDP",os.getComputerID())
- --An IP packet should be sent in the near future to this device to assign IP
- ::skip::
- end
- local function responseERP ()
- --[[
- Echo Request Protocol is used to see if an IP address is responding.
- Sending an ERP packet will cause the device to send a return packet back to the
- source device.
- --]]
- if packet[4] == "Ping" then
- sendPacket(packet[2],"ERP","pong")
- end
- end
- local function responseDRP ()
- --Device Request Protocol
- end
- local function responseUPP ()
- --Unknown Packet Protocol
- end
- local function responseIP ()
- --[[
- Internet Protocol
- Used by router to give a device an IP address. Will only be accepted if the
- router is the default router the device knows. If the default router is
- unassigned then the IP packet is dropped. The data on the packet should
- be a number.
- --]]
- --Checking if routerID is assigned
- print(routerID)
- if routerID == -1 then goto skip end
- --Checking if routerID is the same as the message sender
- if not (routerID == message[1]) then goto skip end
- --Setting the IP of the device to the one sent by the router
- ipAddress = packet[4]
- --Sending IP packet with the device type to the router
- sendPacket({0,0,0},"IP",deviceType)
- ::skip::
- end
- local function responseDP ()
- --Data Packet
- end
- local function responseAP ()
- --Acknowledgement Packet
- end
- local function takeInventory ()
- --[[
- Step 1: Save list of all connected inventories that are on the network
- Step 2: Classify inventories depending on name of item in last slot
- Import/Export: IE:nameOfChest
- Else: saved as standard chest
- Step 3: Save list of items in all inventories, keep a reference of all chests and
- slots where item is found (do not count item in last slot)
- itemName{
- itemCount,
- chestName{
- inventoryType,
- slotID,
- slotID,
- ...
- },
- chestName{
- inventoryType,
- slotID,
- slotID,
- ...
- },
- ...
- }
- --]]
- itemList = {}
- importExportList = {}
- storageList = {}
- local connectedInventories = peripheral.find("inventory")
- local targetInventory = peripheral.wrap(connectedInventories[1])
- local inventoryName = ""
- for index = 1, #connectedInventories, 1 do
- targetInventory = peripheral.wrap(connectedInventories[index])
- --inventoryName = targetInventory.getItemDetail(inventoryName.size()).displayName
- inventoryName = peripheral.getName(connectedInventories[index])
- for itemIndex = 1, inventoryName.size()-1, 1 do
- if itemList[targetInventory.getItemDetail(itemIndex).displayName] == nil do
- itemList[targetInventory.getItemDetail(itemIndex).displayName] = {
- targetInventory.getItemDetail(itemIndex).count
- }
- itemList[targetInventory.getItemDetail(itemIndex).displayName][inventoryName] = {
- "Inventory Type: ",
- itemIndex
- }
- end
- end
- end
- end
- local function sortInventory ()
- --
- end
- local function exportItem (itemName,count,inventoryName)
- --[[
- Move item 'itemName' in amount 'count' from general storage to
- the target inventory. Remove taken amount from item manifest. If count
- exceeds target inventory storage then throw error. If count exceeds amount of
- items in storage then send all items to targer inventory.
- --]]
- end
- local function importItem (inventoryName)
- --[[
- Move items from target inventory to general storage. Save where items are placed in
- item manifest and update total count.
- --]]
- end
- local function exportItemToPlayer (itemName, count, playerName)
- --[[
- Check if playerName exists in stored inventoryManagers.
- Run exportItem with inventoryName being equal to the playerName.
- Use inventoryManager that is assigned to that playerName to send item to player.
- Items will be sent to any open stanard slots
- --]]
- end
- local function importItemFromPlayer (slotID,playerName)
- --[[
- Pull item from player in slot ID and put it into an
- import storage with same name as player.
- Run importItem with inventoryName the same as playerName
- --]]
- end
- function mainLoop ()
- --This function runs continuously and allows the device to respond to requests and packets
- while true do
- print("Ready to recieve message")
- --Wait until rednet message recieved
- message = {rednet.receive()}
- --Pulling out the packet inside of the message
- packet = (message[2])
- --Checking if packet is properly formatted, if it is not then drop packet and continue loop
- if #packet < 4 then goto continue end
- if packet[3] == "RDP" then goto continue end
- if not (packet[3] == "CDP") then
- print("Packet Type: " .. packet[3])
- end
- if localFunctions["response"..packet[3]] then
- --If protocol exists
- if not (packet[3] == "CDP") then
- print("Running response "..packet[3])
- end
- localFunctions["response"..packet[3]]()
- --table.insert(dataQueue,packet)
- else
- --Send UPP packet to source device if protocol is unknown
- print("Unknown")
- sendPacket(packet[1],"UPP","Unknown Packet Type: "..packet[3])
- end
- --]]
- ::continue::
- --End of cycle upkeep
- --Resetting last received message and packet back to their default states
- message = {}
- packet = {}
- end
- end
- --Finding all of the local functions and storing them
- index = 1
- while true do
- key, data = debug.getlocal(1,index)
- if not key then break end
- --print(key)
- if type(data) == "function" then
- if debug.getinfo(data, "S").what == "Lua" then
- localFunctions[key] = data
- end
- end
- index = index + 1
- end
- --Turning on the network capabilities of the device
- peripheralNames = peripheral.getNames()
- for i = 1, #peripheralNames, 1 do
- if peripheral.hasType(peripheralNames[i],"modem") then
- rednet.open(peripheralNames[i])
- end
- end
- --Calling the mainLoop to start program
- print("Starting mainLoop")
- mainLoop()
Add Comment
Please, Sign In to add comment