Class: Dyndoc::InteractiveServer

Inherits:
Object
  • Object
show all
Defined in:
lib/dyndoc/srv/interactive-server.rb

Instance Method Summary collapse

Constructor Details

#initializeInteractiveServer

Returns a new instance of InteractiveServer.



9
10
11
12
13
14
# File 'lib/dyndoc/srv/interactive-server.rb', line 9

def initialize
  @tmpl_mngr=nil
  @tmpl_filename=nil
  init_dyndoc
  init_server
end

Instance Method Details

#init_dyndocObject



16
17
18
19
20
21
22
23
# File 'lib/dyndoc/srv/interactive-server.rb', line 16

def init_dyndoc
  unless @tmpl_mngr
    Dyndoc.cfg_dyn['dyndoc_session']=:interactive
    @tmpl_mngr = Dyndoc::Ruby::TemplateManager.new({})
    ##is it really well-suited for interactive mode???
  end
  reinit_dyndoc
end

#init_serverObject



50
51
52
# File 'lib/dyndoc/srv/interactive-server.rb', line 50

def init_server
  @server = TCPServer.new('0.0.0.0',7777)
end

#process_dyndoc(content) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/dyndoc/srv/interactive-server.rb', line 35

def process_dyndoc(content)
  ##Dyndoc.warn :process_dyndoc_content,content
  @content=@tmpl_mngr.parse(content)
  ##Dyndoc.warn :content, @content
  @tmpl_mngr.filterGlobal.envir["body.content"]=Dyndoc::Utils.protect_dyn_block_for_atom(@content)
  ##Dyndoc.warn :body_content,@tmpl_mngr.filterGlobal.envir["body.content"]
  if @tmpl_filename
    @tmpl_mngr.filterGlobal.envir["_FILENAME_CURRENT_"]=@tmpl_filename.dup
    @tmpl_mngr.filterGlobal.envir["_FILENAME_"]=@tmpl_filename.dup #register name of template!!!
    @tmpl_mngr.filterGlobal.envir["_FILENAME_ORIG_"]=@tmpl_filename.dup #register name of template!!!
    @tmpl_mngr.filterGlobal.envir["_PWD_"]=File.dirname(@tmpl_filename)
  end
  return Dyndoc::Utils.unprotect_dyn_block_for_atom(@content)
end

#reinit_dyndocObject



25
26
27
28
29
30
31
32
# File 'lib/dyndoc/srv/interactive-server.rb', line 25

def reinit_dyndoc
  if @tmpl_mngr
    @tmpl_mngr.init_doc({:format_output=> "html"})
    @tmpl_mngr.require_dyndoc_libs("DyndocWebTools")
    puts "InteractiveServer (re)initialized!\n"
    @tmpl_mngr.as_default_tmpl_mngr! #=> Dyndoc.tmpl_mngr activated!
  end
end

#runObject



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
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/dyndoc/srv/interactive-server.rb', line 54

def run
	trap("SIGINT") { @server.close;exit! }
loop {
	socket = @server.accept

    ## Rmk: I can't remember the whole story but I started with recv and then need read (but did not notice that the atom-plugin did not work anymore). gets() seems to be the solution.
    ## read and gets are blocking and recv is not
    b=socket.gets("__[[END_TOKEN]]__")
	#b=socket.read
    #b=socket.recv(100000)

	##p [:b,b]
	data=b.to_s.strip
	##p [:data,data]
	if data =~ /^__send_cmd__\[\[([a-z,_]*)\|?([^\]]*)?\]\]__(.*)__\[\[END_TOKEN\]\]__$/m
      cmd,@tmpl_filename,content = $1,$2,$3
			##p [:cmd,cmd,:content,content,:filename,@tmpl_filename]
			#p [:tmpl_mngr,@tmpl_filename]
      unless @tmpl_filename.empty?
        Question.session_dir(File.dirname(@tmpl_filename))
      end
      if content.strip == "__EXIT__"
				socket.close
				@server.close
				break
			end

      if cmd =~ /(.*)_with_layout_reinit$/
          LayoutMngr.reinit
          cmd=$1
      end

      if cmd =~ /(.*)_with_libs_reinit$/
          reinit_dyndoc
          cmd=$1
      end

			if cmd == "dyndoc"
			  res = process_dyndoc(content)
			  ##p [:dyndoc_server,content,res]
			  socket.write "__send_cmd__[[dyndoc]]__"+res+"__[[END_TOKEN]]__"
      end
	end
	socket.close
}
end