Initialize
[sdk/tools/sdk-build.git] / src / build_server / SocketJobRequestListener.rb
1 =begin
2  
3  SocketJobRequestListener.rb
4
5 Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6
7 Contact:
8 Taejun Ha <taejun.ha@samsung.com>
9 Jiil Hyoun <jiil.hyoun@samsung.com>
10 Donghyuk Yang <donghyuk.yang@samsung.com>
11 DongHee Yang <donghee.yang@samsung.com>
12
13 Licensed under the Apache License, Version 2.0 (the "License");
14 you may not use this file except in compliance with the License.
15 You may obtain a copy of the License at
16
17 http://www.apache.org/licenses/LICENSE-2.0
18
19 Unless required by applicable law or agreed to in writing, software
20 distributed under the License is distributed on an "AS IS" BASIS,
21 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 See the License for the specific language governing permissions and
23 limitations under the License.
24
25 Contributors:
26 - S-Core Co., Ltd
27 =end
28
29 $LOAD_PATH.unshift File.dirname(__FILE__)
30 require "GitBuildJob.rb"
31 require "LocalBuildJob.rb"
32 require "JobLog.rb"
33 require "BuildComm.rb"
34
35
36 class SocketJobRequestListener
37
38         # initialize
39         def initialize (parent)
40                 @parent_server = parent
41                 @thread = nil
42                 @finish_loop = false
43                 @log = @parent_server.log
44         end
45
46         # start listening
47         def start()
48                 @thread = Thread.new {
49                         main()          
50                 }
51         end
52
53
54
55         # quit listening
56         def stop_listening()
57                 @finish_loop = true
58         end
59         
60         private
61
62         # thread main  
63         def main()
64                 # server open
65                 begin
66                         server = BuildCommServer.new(@parent_server.port, @log)
67                 rescue
68                         @log.info "Server creation failed"
69                         return
70                 end
71
72                 # loop
73                 @log.info "Entering Control Listening Loop ... "        
74                 @finish_loop = false
75                 server.wait_for_connection(@finish_loop) do |req|
76                                 handle_job_request( req )
77                 end     
78
79                 # quit
80                 server.terminate
81         end
82
83
84         # wait for job requests
85         def wait_for_job_requests
86                 req_list = []
87                 req_list.push @tcp_server.accept                
88                 
89                 return req_list
90         end
91
92
93         # handle job request
94         def handle_job_request( req )
95
96                 # read request
97                 req_line = req.gets             
98                 if req_line.nil? then return end
99
100                 # parse request
101                 cmd = ""
102                 if req_line.split(",").count > 0 then
103                         cmd = req_line.split(",")[0].strip 
104                 end
105                 
106                 case  cmd
107                 when "BUILD"
108                         handle_cmd_build( req_line, req )
109                 when "RESOLVE"
110                         handle_cmd_resolve( req_line, req )
111                 when "QUERY"
112                         handle_cmd_query( req_line, req )
113                 else
114                         @log.info "Received Unknown REQ: #{req_line}" 
115                         raise "Unknown request: #{req_line}"
116                 end
117
118         end
119
120
121         # "BUILD"       
122         def handle_cmd_build( line, req )
123                 tok = line.split(",").map { |x| x.strip }
124                 if tok.count < 4 then 
125                         @log.info "Received Wrong REQ: #{line}" 
126                         raise "Invalid request format is used: #{line}"
127                 end
128
129                 case tok[1]
130                 # BUILD,GIT,repos,commit,os,url,async
131                 when "GIT"
132                         @log.info "Received BUILD GIT => #{tok[2]}"
133
134                         # check asynchronous job
135                         async = (not tok[6].nil? and tok[6]=="YES" ? true:false)        
136                         if async then
137                                 new_job = GitBuildJob.new( tok[2], tok[3], tok[4], tok[5], [], @parent_server, nil, nil, false)
138                         else    
139                                 new_job = GitBuildJob.new( tok[2], tok[3], tok[4], tok[5], [], @parent_server, nil, req, false)
140                         end
141                         BuildCommServer.send_begin(req)
142
143                         # start job. If log url is supported, show it
144                         if not @parent_server.job_log_url.empty? then
145                                 new_job.log.info( "Added new job \"#{new_job.id}\"! Check following URL", Log::LV_USER)
146                                 new_job.log.info( " * Log URL : #{@parent_server.job_log_url}/#{new_job.id}/log", Log::LV_USER)
147                         else
148                                 new_job.log.info( "Added new job \"#{new_job.id}\"!", Log::LV_USER)
149                         end
150
151                         # if asynchronouse, quit connection
152                         if async then
153                                 if not @parent_server.job_log_url.empty? then
154                                         req.puts ( "Info: Added new job \"#{new_job.id}\"! Check following URL")
155                                         req.puts ( "Info: * Log URL : #{@parent_server.job_log_url}/#{new_job.id}/log")
156                                 else
157                                         req.puts( "Info: Added new job \"#{new_job.id}\"!")
158                                 end
159
160                                 BuildCommServer.send_end(req)
161                                 BuildCommServer.disconnect(req)
162                         end     
163
164                         # add
165                         @parent_server.add_job( new_job )
166
167                 # BUILD,LOCAL,path,os,url
168                 when "LOCAL"
169                         @log.info "Received BUILD LOCAL => #{tok[2]}" 
170                         
171                         BuildCommServer.send_begin(req)
172                         @parent_server.add_job( 
173                                 LocalBuildJob.new( tok[2], tok[3], tok[4], [], @parent_server, nil, req, false))
174                 else
175                         @log.info "Received Wrong REQ: #{line}"
176                         raise "Invalid request format is used: #{line}"
177                 end     
178         end
179
180
181         # "RESOLVE"     
182         def handle_cmd_resolve( line ,req)
183                 tok = line.split(",").map { |x| x.strip }
184                 if tok.count < 4 then 
185                         @log.info "Received Wrong REQ: #{line}" 
186                         raise "Invalid request format is used: #{line}"
187                 end
188
189                 case tok[1]
190                 # RESOLVE,GIT,repos,commit,os,url
191                 when "GIT"
192                         @log.info "Received RESOLVE GIT => #{tok[2]}"
193
194                         BuildCommServer.send_begin(req)
195                         @parent_server.add_job( 
196                                 GitBuildJob.new( tok[2], tok[3], tok[4], tok[5], [], @parent_server, nil, req, true))
197                 # RESOLVE,LOCAL,path,os,url
198                 when "LOCAL"
199                         @log.info "Received RESOLVE LOCAL => #{tok[2]}" 
200
201                         BuildCommServer.send_begin(req)
202                         @parent_server.add_job( 
203                                 LocalBuildJob.new( tok[2], tok[3], tok[4], [], @parent_server, nil, req, true))
204                 else
205                         @log.info "Received Wrong REQ: #{line}" 
206                         raise "Invalid request format is used: #{line}"
207                 end     
208         end
209
210
211         # "QUERY"
212         def handle_cmd_query( line, req )
213                 tok = line.split(",").map { |x| x.strip }
214                 if tok.count < 2 then 
215                         @log.info "Received Wrong REQ: #{line}" 
216                         raise "Invalid request format is used: #{line}"
217                 end
218         
219                 case tok[1]
220                 # QUERY,JOB
221                 when "JOB"
222                         #puts "Received QUERY JOB"
223
224                         BuildCommServer.send_begin(req)
225                         for job in @parent_server.working_jobs
226                                 BuildCommServer.send(req,"WORKING,#{job.id},#{job.pkginfo.packages[0].source}")
227                         end
228                         for job in @parent_server.waiting_jobs
229                                 BuildCommServer.send(req,"WAITING,#{job.id},#{job.pkginfo.packages[0].source}")
230                         end
231                         for job in @parent_server.remote_jobs
232                                 BuildCommServer.send(req,"REMOTE ,#{job.id},#{job.pkginfo.packages[0].source}")
233                         end
234                         BuildCommServer.send_end(req)
235                         BuildCommServer.disconnect(req)
236
237                 # QUERY,SYSTEM
238                 when "SYSTEM"
239                         #puts "Received QUERY SYSTEM" 
240
241                         BuildCommServer.send_begin(req)
242                         BuildCommServer.send(req,"#{@parent_server.host_os},#{@parent_server.max_working_jobs}")
243                         BuildCommServer.send_end(req)
244                         BuildCommServer.disconnect(req)
245                 else
246                         @log.info "Received Wrong REQ: #{line}" 
247                         raise "Invalid request format is used: #{line}"
248                 end
249         end
250
251 end