Jump to content
  • Sky
  • Blueberry
  • Slate
  • Blackcurrant
  • Watermelon
  • Strawberry
  • Orange
  • Banana
  • Apple
  • Emerald
  • Chocolate
  • Charcoal
Dustpuppy

Print server and client for openprinter

Recommended Posts

While i was testing how to make the print server management for my gui, i wrote print server and a client, that will not need the gui.

All you need is gamax92s vcomponent lib for the client. Install it over oppm, or get it here https://github.com/OpenPrograms/gamax92-Programs/tree/master/vcomponent

Start the server first, then the client(s).

Server has no queue. It will use the openprinter when data comming in. Timing is by your self.

 

The code for the server. Start it with parameter "silent" to get it to background.

local component = require("component")
local event = require("event")

local args = { ... }

local silent = false
if #args > 0 then
  if args[1] == "silent" then
    silent = true
  end
end

if not component.isAvailable("openprinter") then
  io.stderr:write("This program requires openprinter to run.")
  return
end
local printer = component.openprinter

if not component.isAvailable("modem") then
  io.stderr:write("This program requires a modem to run.")
  return
end
local modem = component.modem
local printServerPort = 9100
modem.open(printServerPort)


local function modemCallback(_, _, from, port, distance, ...)
  local message = {...}
  if silent == false then
    if message[2] then
      print(from .. " " .. tostring(port) .. " " .. message[1] .. " " .. message[2])
    else
      print(from .. " " .. tostring(port) .. " " .. message[1])
    end
  end
  if port == printServerPort then
    if message[1] == "GET PRINTER" then
      addr = printer.address
      modem.send(from, printServerPort, addr)
    elseif message[1] == "WRITELN" then
      addr = message[2]
      if addr == printer.address then
	str = message[3]
	col = message[4]
	al = message[5]
	printer.writeln(str, col, al)
      end
    elseif message[1] == "SETTITLE" then
      addr = message[2]
      if addr == printer.address then
	str = message[3]
	printer.setTitle(str)
      end
    elseif message[1] == "GETPAPERLEVEL" then
      addr = message[2]
      if addr == printer.address then
	modem.send(from, printServerPort, printer.getPaperLevel())
      end
    elseif message[1] == "GETCOLORINKLEVEL" then
      addr = message[2]
      if addr == printer.address then
	modem.send(from, printServerPort, printer.getColorInkLevel())
      end
    elseif message[1] == "GETBLACKINKLEVEL" then
      addr = message[2]
      if addr == printer.address then
	modem.send(from, printServerPort, printer.getBlackInkLevel())
      end
    elseif message[1] == "CLEAR" then
      addr = message[2]
      if addr == printer.address then
	printer.clear()
      end
    elseif message[1] == "PRINT" then
      addr = message[2]
      if addr == printer.address then
	printer.print()
      end
    elseif message[1] == "PRINTTAG" then
      addr = message[2]
      if addr == printer.address then
	str = message[3]
	printer.printTag(str)
      end
    end
  end
end


event.listen("modem_message", modemCallback)

if silent == false then
  while true do
    os.sleep(0)
  end
end

 

and the code for the client.

local component = require("component")
local ser = require("serialization")
local vcomp = require("vcomponent")
local modem = component.modem
local event = require("event")

local printServerPort = 9100

modem.open(printServerPort)
local netprinter = {}

local printerList = {}

function getPrinterAddress(addr)
  for i = 1, #printerList do
    if printerList[i].server == addr then
--      print(printerList[i].server .. "->" .. printerList[i].printerAddress)
      return printerList[i].printerAddress
    end
  end
end

local netprinterproxy = {}
local netprinterdocs = {}


function netprinter.register(addr)
  netprinterproxy.printerAddress = function() return getPrinterAddress(addr) end
  netprinterproxy.writeln = function(str, col, al) modem.send(addr, printServerPort, "WRITELN", getPrinterAddress(addr), str, col, al) end
  netprinterproxy.setTitle = function(str) modem.send(addr, printServerPort, "SETTITLE", getPrinterAddress(addr), str) end
  netprinterproxy.print = function(str) modem.send(addr, printServerPort, "PRINT", getPrinterAddress(addr)) end
  netprinterproxy.clear = function(str) modem.send(addr, printServerPort, "CLEAR", getPrinterAddress(addr)) end
  netprinterproxy.getPaperLevel = function()
    modem.send(addr, printServerPort, "GETPAPERLEVEL", getPrinterAddress(addr))
    local ev, _, from, port, distance, message = event.pull(0.5,"modem_message")
    if ev then
      if message then
	return message
      end
    end
  end
  netprinterproxy.getColorInkLevel = function()
    modem.send(addr, printServerPort, "GETCOLORINKLEVEL", getPrinterAddress(addr))
    local ev, _, from, port, distance, message = event.pull(0.5,"modem_message")
    if ev then
      if message then
	return message
      end
    end
  end
  netprinterproxy.getBlackInkLevel = function()
    modem.send(addr, printServerPort, "GETBLACKINKLEVEL", getPrinterAddress(addr))
    local ev, _, from, port, distance, message = event.pull(0.5,"modem_message")
    if ev then
      if message then
	return message
      end
    end
  end
  netprinterproxy.printTag = function(str) modem.send(addr, printServerPort, "PRINTTAG", getPrinterAddress(addr)) end
  vcomp.register(addr, "nprinter", netprinterproxy, netprinterdocs)
end


function netprinter.getPrinterList()
  printerList = {}
  modem.broadcast(printServerPort, "GET PRINTER")
  for i = 1, 20 do
    local ev, _, from, port, distance, message = event.pull(0,"modem_message")
    if ev then
      local tmpTable = {}
      tmpTable.server = from
      if message then
	tmpTable.printerAddress = message
	table.insert(printerList, tmpTable)
	netprinter.register(from)
      end
    end
  end
  return printerList
end

netprinter.getPrinterList()

return netprinter

 

a little test program

local component = require("component")
local netprinter = require("netprinter")
local ser = require("serialization")


p = component.nprinter

p.clear()
p.setTitle("test page")
p.writeln("hallo")
p.print()

print("Paper : " .. p.getPaperLevel())
print("Black : " .. p.getBlackInkLevel())
print("Color : " .. p.getColorInkLevel())

 

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use and Privacy Policy.