Skip to content

Loading…

Spike towards server-controlled immediate printing. #44

Open
wants to merge 1 commit into from

1 participant

@lazyatom

This relates to #8, and explores using a single encoded header to
simplify parsing of more complex parameters from the server.

@lazyatom lazyatom Spike towards server-controlled immediate printing.
This relates to #8, and explores using a single encoded header to
simplify parsing of more complex parameters from the server.
63ce11e
@lazyatom

I haven't been able to test this using my Arduino, because the FTDI cable I have at home is busted.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 8, 2013
  1. @lazyatom

    Spike towards server-controlled immediate printing.

    lazyatom committed
    This relates to #8, and explores using a single encoded header to
    simplify parsing of more complex parameters from the server.
View
2 config.ru
@@ -10,6 +10,8 @@ Sass::Plugin.options[:template_location] = 'public/stylesheets'
use Sass::Plugin::Rack
use Rack::MethodOverride
+use Printer::BackendServer::EncodedStatusHeaderMiddleare
+use Rack::ContentLength
if ENV["RESQUE_SERVER"]
require 'resque/server'
View
3 lib/printer/backend_server.rb
@@ -7,6 +7,9 @@ module Printer::BackendServer
autoload :Settings, "printer/backend_server/settings"
autoload :Archive, "printer/backend_server/archive"
+ autoload :EncodedStatusHeaderMiddleware,
+ "printer/backend_server/encoded_status_header_middleware"
+
App = Rack::Builder.new do
map("/printer") { run Printer::BackendServer::Polling }
map("/preview") { run Printer::BackendServer::Preview }
View
21 lib/printer/backend_server/encoded_status_header_middleware.rb
@@ -0,0 +1,21 @@
+class Printer::BackendServer::EncodedStatusHeaderMiddleware
+ def initialize(app)
+ @app = app
+ end
+
+ def call(env)
+ status, headers, body = @app.call(env)
+ headers.merge!("X-Printer-Encoded-Status" => encoded_status(status, headers))
+ [status, headers, body]
+ end
+
+ private
+
+ def encoded_status(status, headers)
+ [status, headers["Content-length"], encoded_presence(headers["X-Printer-PrintImmediately"])].compact.join("|")
+ end
+
+ def encoded_presence(header)
+ header ? 1 : 0
+ end
+end
View
21 printer.ino
@@ -154,6 +154,7 @@ boolean downloadWaiting = false;
char cacheFilename[] = "TMP";
unsigned long content_length = 0;
boolean statusOk = false;
+boolean printImmediately = false;
void checkForDownload() {
unsigned long length = 0;
@@ -183,19 +184,25 @@ void checkForDownload() {
client.println();
boolean parsingHeader = true;
+ char header[2000];
+
while(client.connected()) {
while(client.available()) {
if (parsingHeader) {
- client.find((char*)"HTTP/1.1 ");
- char statusCode[] = "xxx";
- client.readBytes(statusCode, 3);
- statusOk = (strcmp(statusCode, "200") == 0);
- client.find((char*)"Content-Length: ");
+ client.find((char*)"X-Printer-Encoded-Status: ");
+ char *buffer = "xxxxxxxxxx";
+ client.readBytesUntil('|', buffer, 100);
+ statusOk = (strcmp(buffer, "200") == 0);
+ client.readBytesUntil('|', buffer, 100);
char c;
while (isdigit(c = client.read())) {
content_length = content_length*10 + (c - '0');
}
- debug2("Content length: ", content_length);
+ debug2("Content-length: ", content_length);
+ if (client.readBytesUntil('\n', buffer, 5)) {
+ printImmediately = (buffer[0] == '1');
+ }
+ debug2("Print immediately: ", printImmediately);
client.find((char*)"\n\r\n"); // the first \r may already have been read above
parsingHeader = false;
} else {
@@ -285,7 +292,7 @@ Bounce bouncer = Bounce(buttonPin, 5); // 5 millisecond debounce
void loop() {
if (downloadWaiting) {
bouncer.update();
- if (bouncer.read() == HIGH) {
+ if (bouncer.read() == HIGH || printImmediately) {
printFromDownload();
}
} else {
View
45 test/backend_server/encoded_status_header_middleware_test.rb
@@ -0,0 +1,45 @@
+require "test_helper"
+require "rack/test"
+require "printer/backend_server"
+
+ENV['RACK_ENV'] = 'test'
+
+describe Printer::BackendServer::EncodedStatusHeaderMiddleware do
+ include Rack::Test::Methods
+
+ def app
+ Rack::Builder.new do
+ use Printer::BackendServer::EncodedStatusHeaderMiddleware
+ use Rack::ContentLength
+ run lambda { |env|
+ headers = env["PATH_INFO"] == "/print-now" ? {"X-Printer-PrintImmediately" => true} : {}
+ [200, headers, ["Response body"]]
+ }
+ end
+ end
+
+ describe "making a request" do
+ before do
+ get "anything"
+ end
+
+ it "includes the encoded header" do
+ assert last_response.headers.keys.include?("X-Printer-Encoded-Status")
+ end
+
+ it "encodes the status in the header" do
+ last_response.headers["X-Printer-Encoded-Status"].split("|")[0].must_equal "200"
+ end
+
+ it "encodes the content length in the header" do
+ last_response.headers["X-Printer-Encoded-Status"].split("|")[1].must_equal "Response body".length.to_s
+ end
+ end
+
+ describe "making a request where the X-Printer-PrintImmediately header is present" do
+ it "encodes the print immediately value into the header" do
+ get "/print-now"
+ last_response.headers["X-Printer-Encoded-Status"].split("|")[2].must_equal "1"
+ end
+ end
+end
Something went wrong with that request. Please try again.