Class: FlashSDK::FCSHSocket

Inherits:
Object
  • Object
show all
Defined in:
flashsdk/lib/flashsdk/fcsh_socket.rb

Overview

This is a client to the long-running FCSH process.

The server side of this connection should be started before starting the client.

To do this, just open a new terminal, cd into your project directory, and run:

rake fcsh:start

Constant Summary

DEFAULT_PORT =

The default TCP port that FCSH will use to connect.

12321

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (FCSHSocket) initialize

Create a new FCSHClient



28
29
30
31
# File 'flashsdk/lib/flashsdk/fcsh_socket.rb', line 28

def initialize
  @port = DEFAULT_PORT
  @requests = {}
end

Instance Attribute Details

- (Object) port

Returns the value of attribute port



22
23
24
# File 'flashsdk/lib/flashsdk/fcsh_socket.rb', line 22

def port
  @port
end

- (Object) requests (readonly)

Returns the value of attribute requests



24
25
26
# File 'flashsdk/lib/flashsdk/fcsh_socket.rb', line 24

def requests
  @requests
end

Instance Method Details

- (Object) execute(command, port = nil)



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
# File 'flashsdk/lib/flashsdk/fcsh_socket.rb', line 88

def execute command, port=nil
  start = Time.now
  port = port || @port
  begin
    session = TCPSocket.new 'localhost', port
    session.puts command
    response = session.read
    if response.match /Error/
      raise Sprout::Errors::UsageError.new "[FCSH] #{response}"
    else
      Sprout.stdout.puts "[FCSH] #{response}"
    end
    response
  rescue Errno::ECONNREFUSED => e
    message = "[ERROR] "
    message << e.message
    message << ": Could not connect to an FCSH server on port: #{port} at: #{Dir.pwd}.\n\n"
    message << "This is probably because one has not been started. To start a new FCSH server, open a new "
    message << "terminal and run the following:\n\n"
    message << "cd #{Dir.pwd}\n"
    message << "rake fcsh:start\n"
    raise Sprout::Errors::UsageError.new message
  ensure
    if !session.nil? && !session.closed?
      session.flush
      session.close
    end
  end

  if command.match /^mxmlc|^compc/
    Sprout.stdout.puts "[FCSH] complete in #{(Time.now - start).seconds} seconds."
  end
end

- (Object) listen(pkg_name = nil, pkg_version = nil, port = nil)



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'flashsdk/lib/flashsdk/fcsh_socket.rb', line 33

def listen pkg_name=nil, pkg_version=nil, port=nil
  port = port || @port
  # Instantiate FCSH:
  fcsh             = FlashSDK::FCSH.new
  fcsh.pkg_name    = pkg_name unless pkg_name.nil?
  fcsh.pkg_version = pkg_version unless pkg_version.nil?

  # Notify the outer shell that we're ready:
  Sprout.stdout.puts "FCSH socket open with: #{fcsh.pkg_name} and #{fcsh.pkg_version}, waiting for connections on port #{port}"
  Sprout.stdout.puts ""

  # Start up the FCSH Session:
  fcsh.execute false

  # Create a readable IO pipe:
  output = Sprout::OutputBuffer.new
  # Associate the IO pipe with our 
  # outputs so that FCSH will write
  # to it.
  Sprout.stdout = output
  Sprout.stderr = output

  server = TCPServer.new 'localhost', port

  # Create a thread that will exit
  # when FCSH exits.
  t = Thread.new do
    fcsh.wait
  end

  while t.alive? do
    Sprout.stdout.puts ""
    session  = server.accept
    rendered = render_request session.gets
    parts    = rendered.split(" ")
    method   = parts.shift.strip
    if parts.size > 0
      fcsh.send method, parts.join(" ")
    else
      fcsh.send method
    end

    fcsh.wait_for_prompt

    if method == "clear"
      clear_requests
    end

    response = output.read
    session.puts response.gsub(fcsh.prompt, "\n")
    session.flush
    session.close
  end
end