Update Tizen 2.0 SDK source code
[sdk/tools/sdk-build.git] / src / build_server / RemoteBuildServer.rb
1 =begin
2  
3  RemoteBuildServer.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 require 'fileutils'
30 $LOAD_PATH.unshift File.dirname(__FILE__)
31 require "RemoteBuildJob.rb"
32 require "BuildComm.rb"
33 require 'thread'
34
35 class RemoteBuildServer
36         attr_accessor :ip, :port, :status, :host_os
37         attr_accessor :max_working_jobs, :working_jobs, :waiting_jobs
38         attr_accessor :pkgserver_url, :path
39
40         # initialize
41         def initialize(ip, port, parent)
42                 @ip = ip
43                 @port = port
44                 @status = "DISCONNECTED"
45                 @host_os = Utils::HOST_OS
46                 @max_working_jobs = 2
47                 @working_jobs = []
48                 @waiting_jobs = []
49                 @pkgserver_url = parent.pkgserver_url
50                 @path = ""
51                 @file_transfer_cnt_mutex = Mutex.new
52                 @file_transfer_cnt = 0
53         end
54
55
56         # check the job can be built on this server
57         def can_build?(job)
58
59                 # check me
60                 if job.can_be_built_on? @host_os then
61                         return true
62                 end
63
64                 return false
65         end
66
67
68         # query remote server info & update server state
69         def update_state
70
71                 # send 
72                 #@status = "DISCONNECTED"
73                 client = BuildCommClient.create( @ip, @port )
74                 if client.nil? then return end
75                 if client.send("QUERY|SYSTEM") then
76                         result = client.read_lines do |l|
77                                 tok = l.split(",").map { |x| x.strip }
78                                 @host_os = tok[0]
79                                 @max_working_jobs = tok[1].to_i
80                                 @status = "RUNNING"
81                         end
82                         if not result then @status = "DISCONNECTED" end
83                 end
84                 client.terminate
85                 if @status == "DISCONNECTED" then return end
86
87                 # send  
88                 @working_jobs = []
89                 @waiting_jobs = []
90                 client = BuildCommClient.create( @ip, @port )
91                 if client.nil? then return end
92                 if client.send("QUERY|JOB") then
93                         result = client.read_lines do |l|
94                                 tok = l.split(",").map { |x| x.strip }
95                                 
96                                 job_status = tok[0]
97                                 job_id = tok[1]
98                                 new_job = RemoteBuildJob.new(job_id,self)
99                                 case job_status
100                                 when "WAITING", "JUST_CREATED", "INITIALIZING"
101                                         @waiting_jobs.push new_job
102                                 when "WORKING" 
103                                         @working_jobs.push new_job
104                                 else
105                                         #puts "Uncontrolled status"
106                                 end     
107                         end
108                         if not result then @status = "DISCONNECTED" end
109                 else
110                         @status = "DISCONNECTED"
111                 end
112                 client.terminate
113         end
114
115
116         # return available working slot
117         def get_number_of_empty_room
118                 return @max_working_jobs - @working_jobs.count
119         end
120
121
122         # check there are working jobs
123         def has_working_jobs
124                 return (@working_jobs.count > 0)
125         end
126
127
128         # check there are waiting jobs
129         def has_waiting_jobs
130                 return (@waiting_jobs.count > 0)
131         end
132
133
134         def add_file_transfer()
135                 @file_transfer_cnt_mutex.synchronize {
136                         @file_transfer_cnt += 1
137                 }
138         end
139
140         def remove_file_transfer()
141                 @file_transfer_cnt_mutex.synchronize {
142                         @file_transfer_cnt -= 1
143                 }
144         end
145
146         def get_file_transfer_cnt()
147                 return @file_transfer_cnt
148         end
149 end
150