Class: Baykit::BayServer::Docker::Terminal::TerminalTrain

Inherits:
Train::Train
  • Object
show all
Includes:
Agent::Transporter, Tours, Tours::ReqContentHandler, Train, Util
Defined in:
lib/baykit/bayserver/docker/terminal/terminal_train.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(terminal_docker, tur, app, env) ⇒ TerminalTrain

Returns a new instance of TerminalTrain.



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/baykit/bayserver/docker/terminal/terminal_train.rb', line 35

def initialize(terminal_docker, tur, app, env)
  BayLog.debug "%s New Rack Train", tur
  @terminal_docker = terminal_docker
  @tour = tur
  @tour_id = tur.tour_id
  @app = app
  @env = env
  @available = false
  @tmpfile = nil
  @req_cont = nil

  @lock = Mutex.new
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



27
28
29
# File 'lib/baykit/bayserver/docker/terminal/terminal_train.rb', line 27

def app
  @app
end

#envObject (readonly)

Returns the value of attribute env.



28
29
30
# File 'lib/baykit/bayserver/docker/terminal/terminal_train.rb', line 28

def env
  @env
end

#lockObject (readonly)

Returns the value of attribute lock.



31
32
33
# File 'lib/baykit/bayserver/docker/terminal/terminal_train.rb', line 31

def lock
  @lock
end

#req_availableObject (readonly)

Returns the value of attribute req_available.



30
31
32
# File 'lib/baykit/bayserver/docker/terminal/terminal_train.rb', line 30

def req_available
  @req_available
end

#req_contObject (readonly)

Returns the value of attribute req_cont.



33
34
35
# File 'lib/baykit/bayserver/docker/terminal/terminal_train.rb', line 33

def req_cont
  @req_cont
end

#terminal_dockerObject (readonly)

Returns the value of attribute terminal_docker.



24
25
26
# File 'lib/baykit/bayserver/docker/terminal/terminal_train.rb', line 24

def terminal_docker
  @terminal_docker
end

#tmpfileObject (readonly)

Returns the value of attribute tmpfile.



32
33
34
# File 'lib/baykit/bayserver/docker/terminal/terminal_train.rb', line 32

def tmpfile
  @tmpfile
end

#tourObject (readonly)

Returns the value of attribute tour.



25
26
27
# File 'lib/baykit/bayserver/docker/terminal/terminal_train.rb', line 25

def tour
  @tour
end

#tour_idObject (readonly)

Returns the value of attribute tour_id.



26
27
28
# File 'lib/baykit/bayserver/docker/terminal/terminal_train.rb', line 26

def tour_id
  @tour_id
end

Instance Method Details

#departObject



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/baykit/bayserver/docker/terminal/terminal_train.rb', line 70

def depart

  begin
    if @tour.req.method.casecmp?("post")
      BayLog.debug("%s Terminal: posted: content-length: %s", @tour, @env["CONTENT_LENGTH"])
    end


    if BayServer.harbor.trace_header?
      @env.keys.each do |key|
        value = @env[key]
        BayLog.info("%s Terminal: env:%s=%s", @tour, key, value)
      end
    end

    status, headers, body = @app.call(@env)

    # Hijack check
    pip = @env[TerminalDocker::RACK_TERMINAL_PIPE]

    if pip != nil
      # Fully hijacked (Do nothing)
      BayLog.debug("%s Tour is fully hijacked", @tour)

    else
      @tour.res.headers.status = status

      hijack = nil
      headers.each do | key, value |
        if key == Rack::RACK_HIJACK
          hijack = value
        else
          @tour.res.headers.add key, value
        end
      end

      # Send headers
      @tour.res.send_headers @tour_id

      if hijack != nil
        # Partially hijacked
        BayLog.debug("%s Tour is partially hijacked", @tour)

        pip = IO.pipe
        yat = HijackersYacht.new()
        bufsize = @tour.ship.protocol_handler.max_res_packet_data_size()
        tp = PlainTransporter.new(false, bufsize)

        yat.init(@tour, pip[0], tp)
        tp.init(@tour.ship.agent.non_blocking_handler, pip[0], yat)
        @tour.ship.resume(@tour.ship_id)

        hijack.call pip[1]

      else
        # Not hijacked

        # Setup consume listener
        @tour.res.set_consume_listener() do |len, resume|
          if(resume)
            @available = true
          end
        end

        # Send contents
        body.each do | str |
          bstr = StringUtil.to_bytes(str)
          BayLog.trace("%s TerminalTask: read body: len=%d", @tour, bstr.length)
          @available = @tour.res.send_content(@tour_id, bstr, 0, bstr.length)
          while !@available
            sleep 0.1
          end
        end

        @tour.res.end_content(@tour_id)

      end
    end
  rescue HttpException => e
    raise e
  rescue => e
    BayLog.error_e e
    raise HttpException.new HttpStatus::INTERNAL_SERVER_ERROR, "Terminal error"
  ensure
    if @tmpfile
      @tmpfile.close()
    end
  end
end

#inspectObject



202
203
204
# File 'lib/baykit/bayserver/docker/terminal/terminal_train.rb', line 202

def inspect
  "TerminalTask #{@tour}"
end

#on_abort(tur) ⇒ Object



193
194
195
196
197
198
199
200
# File 'lib/baykit/bayserver/docker/terminal/terminal_train.rb', line 193

def on_abort(tur)
  BayLog.trace("%s TerminalTask:abort", @tour)
  if @tmpfile
    @tmpfile.close()
    @tmpfile = nil
  end
  return false
end

#on_end_content(tur) ⇒ Object



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/baykit/bayserver/docker/terminal/terminal_train.rb', line 175

def on_end_content(tur)
  BayLog.trace("%s TerminalTask:endContent", @tour)

  if @req_cont != nil
    # Cache content in memory
    rack_input = StringIO.new(@req_cont)
  else
    # Content save on disk
    @tmpfile.rewind()
    rack_input = @tmpfile
  end
  env[Rack::RACK_INPUT] = rack_input

  if !TrainRunner.post(self)
    raise HttpException.new(HttpStatus::SERVICE_UNAVAILABLE, "TrainRunner is busy")
  end
end

#on_read_content(tur, buf, start, len) ⇒ Object



160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/baykit/bayserver/docker/terminal/terminal_train.rb', line 160

def on_read_content(tur, buf, start, len)
  BayLog.trace("%s TerminalTask:onReadContent: len=%d", @tour, len)

  if @req_cont != nil
    # Cache content in memory
    @req_cont << buf[start, len]
  else
    # Content save on disk
    @tmpfile.write(buf[start, len])
  end

  tur.req.consumed(Tour::TOUR_ID_NOCHECK, len)
  true
end

#start_tourObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/baykit/bayserver/docker/terminal/terminal_train.rb', line 49

def start_tour()
  @tour.req.set_content_handler(self)
  @tmpfile = nil

  if @env["CONTENT_LENGTH"]
    req_cont_len = @env["CONTENT_LENGTH"].to_i
  else
    req_cont_len = 0
  end

  if req_cont_len <= @terminal_docker.post_cache_threshold
    # Cache content in memory
    @req_cont = StringUtil.alloc(0)
  else
    # Content save on disk
    @tmpfile = Tempfile.new("terminal_upload")
    @tmpfile.binmode
  end

end