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

Inherits:
Train::Train
  • Object
show all
Includes:
Agent, Agent::Multiplexer, Rudders, 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.



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/baykit/bayserver/docker/terminal/terminal_train.rb', line 40

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.



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

def app
  @app
end

#envObject (readonly)

Returns the value of attribute env.



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

def env
  @env
end

#lockObject (readonly)

Returns the value of attribute lock.



36
37
38
# File 'lib/baykit/bayserver/docker/terminal/terminal_train.rb', line 36

def lock
  @lock
end

#req_availableObject (readonly)

Returns the value of attribute req_available.



35
36
37
# File 'lib/baykit/bayserver/docker/terminal/terminal_train.rb', line 35

def req_available
  @req_available
end

#req_contObject (readonly)

Returns the value of attribute req_cont.



38
39
40
# File 'lib/baykit/bayserver/docker/terminal/terminal_train.rb', line 38

def req_cont
  @req_cont
end

#terminal_dockerObject (readonly)

Returns the value of attribute terminal_docker.



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

def terminal_docker
  @terminal_docker
end

#tmpfileObject (readonly)

Returns the value of attribute tmpfile.



37
38
39
# File 'lib/baykit/bayserver/docker/terminal/terminal_train.rb', line 37

def tmpfile
  @tmpfile
end

#tourObject (readonly)

Returns the value of attribute tour.



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

def tour
  @tour
end

#tour_idObject (readonly)

Returns the value of attribute tour_id.



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

def tour_id
  @tour_id
end

Instance Method Details

#departObject



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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/baykit/bayserver/docker/terminal/terminal_train.rb', line 75

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)
        agt = GrandAgent.get(@tour.ship.agent_id)
        mpx = agt.net_multiplexer
        pip = IO.pipe
        rd_read = IORudder.new(pip[0])
        sip = HijackersShip.new()
        bufsize = @tour.ship.protocol_handler.max_res_packet_data_size()
        tp = PlainTransporter.new(mpx, sip, false, bufsize, false)

        sip.init(@tour, rd_read, tp)
        sid = sip.ship_id

        @tour.res.set_consume_listener do |len, resume|
          if resume
            sip.resume_read(sid)
          end
        end

        mpx.add_rudder_state(rd_read, RudderState.new(rd_read, tp))
        mpx.req_read(rd_read)

        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_res_content(@tour_id, bstr, 0, bstr.length)
          while !@available
            sleep 0.1
          end
        end

        @tour.res.end_res_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



221
222
223
# File 'lib/baykit/bayserver/docker/terminal/terminal_train.rb', line 221

def inspect
  "TerminalTask #{@tour}"
end

#on_abort_req(tur) ⇒ Object



208
209
210
211
212
213
214
215
# File 'lib/baykit/bayserver/docker/terminal/terminal_train.rb', line 208

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

#on_end_req_content(tur) ⇒ Object



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/baykit/bayserver/docker/terminal/terminal_train.rb', line 190

def on_end_req_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(tur.ship.agent_id, self)
    raise HttpException.new(HttpStatus::SERVICE_UNAVAILABLE, "TrainRunner is busy")
  end
end

#on_read_req_content(tur, buf, start, len, &lis) ⇒ Object



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

def on_read_req_content(tur, buf, start, len, &lis)
  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, &lis)
  true
end

#on_timerObject



217
218
219
# File 'lib/baykit/bayserver/docker/terminal/terminal_train.rb', line 217

def on_timer
  BayLog.debug("%s TerminalTask:timer", @tour)
end

#start_tourObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/baykit/bayserver/docker/terminal/terminal_train.rb', line 54

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