Class: FlashSDK::FCSHSocket

Inherits:
Object
  • Object
show all
Defined in:
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 collapse

DEFAULT_PORT =

The default TCP port that FCSH will use to connect.

12321

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeFCSHSocket

Create a new FCSHClient



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

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

Instance Attribute Details

#portObject

Returns the value of attribute port.



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

def port
  @port
end

#requestsObject (readonly)

Returns the value of attribute requests.



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

def requests
  @requests
end

Instance Method Details

#execute(command, port = nil) ⇒ Object



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

def execute command, port=nil
  duration = Benchmark.measure do
    port = port || @port
    begin
      #Sprout.stdout.puts "[FCSH] #{command}"
      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
  end

  if command.match /^mxmlc|^compc/
    Sprout.stdout.puts "[FCSH] complete in #{duration} seconds."
  end
end

#listen(pkg_name = nil, pkg_version = nil, port = nil) ⇒ Object



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

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 = "#{rendered}\n"
    response << output.read.gsub(rendered, '')
    session.puts response.gsub(fcsh.prompt, "\n")
    session.flush
    session.close
  end
end