Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

if filename is "-", read config from stdin (TSUN-193)

  • Loading branch information...
commit f7f7d08d17a0f70fa0a783699444898da6c6bf4b 1 parent 0dfefc2
Nicolas Niclausse authored September 23, 2011
50  src/tsung_controller/ts_config.erl
@@ -57,24 +57,26 @@
57 57
 %%% @doc:  read and parse the xml config file
58 58
 %%% @end
59 59
 %%%----------------------------------------------------------------------
  60
+read(Filename=standard_io, LogDir) ->
  61
+    ?LOG("Reading config file from stdin~n", ?NOTICE),
  62
+    XML = read_stdio(),
  63
+    handle_read(catch xmerl_scan:string(XML,
  64
+                                      [{fetch_path,["/usr/share/tsung/","./"]},
  65
+                                       {validation,true}]),Filename,LogDir);
60 66
 read(Filename, LogDir) ->
61  
-    case catch xmerl_scan:file(Filename,
62  
-                               [{fetch_path,["/usr/share/tsung/","./"]},
63  
-                                {validation,true}]) of
64  
-        {ok, Root = #xmlElement{}} ->  % xmerl-0.15
65  
-            ?LOGF("Reading config file: ~s~n", [Filename], ?NOTICE),
66  
-            Table = ets:new(sessiontable, [ordered_set, protected]),
67  
-            {ok, parse(Root, #config{session_tab = Table})};
68  
-        {Root = #xmlElement{}, _Tail} ->  % xmerl-0.19 and up
69  
-            ?LOGF("Reading config file: ~s~n", [Filename], ?NOTICE),
70  
-            Table = ets:new(sessiontable, [ordered_set, protected]),
71  
-            backup_config(LogDir, Filename, Root),
72  
-            {ok, parse(Root, #config{session_tab = Table, proto_opts=#proto_opts{}})};
73  
-        {error,Reason} ->
74  
-            {error, Reason};
75  
-        {'EXIT',Reason} ->
76  
-            {error, Reason}
77  
-    end.
  67
+    ?LOGF("Reading config file: ~s~n", [Filename], ?NOTICE),
  68
+    handle_read(catch xmerl_scan:file(Filename,
  69
+                                      [{fetch_path,["/usr/share/tsung/","./"]},
  70
+                                       {validation,true}]),Filename,LogDir).
  71
+
  72
+handle_read( {Root = #xmlElement{}, _Tail}, Filename, LogDir) ->
  73
+    Table = ets:new(sessiontable, [ordered_set, protected]),
  74
+    backup_config(LogDir, Filename, Root),
  75
+    {ok, parse(Root, #config{session_tab = Table, proto_opts=#proto_opts{}})};
  76
+handle_read({error,Reason},_,_) ->
  77
+    {error, Reason};
  78
+handle_read({'EXIT',Reason},_,_) ->
  79
+    {error, Reason}.
78 80
 
79 81
 %%%----------------------------------------------------------------------
80 82
 %%% Function: parse/2
@@ -948,6 +950,8 @@ shortnames(Hostname)->
948 950
 %%   Use parsed config file to expand all ENTITY
949 951
 %% @end
950 952
 %%----------------------------------------------------------------------
  953
+backup_config(Dir,standard_io, Config) ->
  954
+    backup_config(Dir, "tsung_stdin.xml", Config);
951 955
 backup_config(Dir, Name, Config) ->
952 956
     BaseName = filename:basename(Name),
953 957
     {ok,IOF}=file:open(filename:join(Dir,BaseName),[write]),
@@ -959,3 +963,15 @@ backup_config(Dir, Name, Config) ->
959 963
             ok
960 964
     end,
961 965
     file:close(IOF).
  966
+
  967
+%% @spec read_stdio()-> string()
  968
+%% @doc Read config from standard input
  969
+%% @end
  970
+read_stdio()->
  971
+    read_stdio(io:get_line(""),[]).
  972
+
  973
+read_stdio(eof, Data)->
  974
+    lists:flatten(Data);
  975
+read_stdio(Data,Acc) ->
  976
+    read_stdio(io:get_line(""),[Acc,Data]).
  977
+
28  src/tsung_controller/tsung_controller.erl
@@ -60,17 +60,23 @@ start(_Type, _StartArgs) ->
60 60
     end.
61 61
 
62 62
 start_phase(load_config, _StartType, _PhaseArgs) ->
63  
-    Conf = ?config(config_file),
64  
-    Timeout = case file:read_file_info(Conf) of
65  
-                  {ok, #file_info{size=Size}} when Size > 10000000 -> % > 10MB
66  
-                      erlang:display(["Can take up to 5mn to read config ",Size]),
67  
-                      300000; % 10mn
68  
-                  {ok, #file_info{size=Size}} when Size > 1000000 ->  % > 1MB
69  
-                      erlang:display(["Can take up to 3mn to read config ",Size]),
70  
-                      180000; % 5mn
71  
-                  {ok, #file_info{size=Size}} ->
72  
-                      120000  % 2mn
73  
-                  end,
  63
+    {Conf,Timeout} =
  64
+        case ?config(config_file) of
  65
+            "-"  ->
  66
+                {standard_io, 120000}; %2mn timeout
  67
+            File ->
  68
+                T = case file:read_file_info(File) of
  69
+                        {ok, #file_info{size=Size}} when Size > 10000000 -> % > 10MB
  70
+                            erlang:display(["Can take up to 5mn to read config ",Size]),
  71
+                            300000; % 10mn
  72
+                        {ok, #file_info{size=Size}} when Size > 1000000 ->  % > 1MB
  73
+                            erlang:display(["Can take up to 3mn to read config ",Size]),
  74
+                            180000; % 5mn
  75
+                        {ok, #file_info{size=_}} ->
  76
+                            120000  % 2mn
  77
+                    end,
  78
+                {File, T}
  79
+        end,
74 80
     case ts_config_server:read_config(Conf,Timeout) of
75 81
         {error,Reason}->
76 82
             erlang:display(["Config Error, aborting ! ", Reason]),
2  tsung.sh.in
@@ -90,7 +90,7 @@ version() {
90 90
 }
91 91
 
92 92
 checkconfig() {
93  
-    if [ ! -e $CONF_OPT_FILE ]
  93
+    if [ ! -e $CONF_OPT_FILE ] && [ $CONF_OPT_FILE != "-" ]
94 94
     then
95 95
         echo "Config file $CONF_OPT_FILE doesn't exist, aborting !"
96 96
         exit 1

0 notes on commit f7f7d08

Please sign in to comment.
Something went wrong with that request. Please try again.