func main() {
	lsplog.SetVerbose(3)
	lsplog.Vlogf(3, "[Request] Args: %s", os.Args)
	var e error
	ww := new(Worker)
	ww.load = 0
	ww.endFailureRecovery = make(chan bool, 1)
	ww.startFailureRecovery = make(chan bool, 1)
	ww.reqLock = new(sync.Mutex)
	monitor := os.Args[1]
	port, _ := strconv.Atoi(os.Args[2])
	l, err := net.Listen("tcp", fmt.Sprintf(":%d", port))
	if err != nil {
		log.Fatal("listen error:", e)
	}
	_, _, _ = net.SplitHostPort(l.Addr().String())
	//port, _ = strconv.Atoi(listenport)
	rpc.Register(ww)
	rpc.HandleHTTP()
	go http.Serve(l, nil)
	// Register to monitor and get load balancer hostport and its buddy
	ww.cliM, _ = rpc.DialHTTP("tcp", monitor)
	name, _ := os.Hostname()
	addrs, _ := net.LookupHost(name)
	args := &commproto.RegisterWorkerArgs{fmt.Sprintf("%s:%s", addrs[0], port)}
	var reply commproto.RegisterWorkerReply
	ww.cliM.Call("MonitorRPC.RegisterWorker", args, &reply)
	lsplog.Vlogf(3, "[Worker] Received LoadBalancerHostPort: %s", reply.LoadBalancerHostPort)
	// connect to main LB
	parts := strings.Split(reply.LoadBalancerHostPort, ":")
	fakePort, _ := strconv.Atoi(parts[1])
	fakeHostPort := fmt.Sprintf("%s:%d", parts[0], fakePort+10)
	ww.cli, e = lsp12.NewLspClient(fakeHostPort, &lsp12.LspParams{5, 2000})
	for e != nil || ww.cli == nil {
		ww.cli, e = lsp12.NewLspClient(reply.LoadBalancerHostPort, &lsp12.LspParams{5, 2000})
		lsplog.Vlogf(3, "[Worker] Connection to load balancer failed. \n")
	}
	// queue of requests
	ww.reqList = list.New()

	// lauch the request handler
	go requestHandler(ww)

	// listen to requests
	for {
		req, err := ww.cli.Read()
		if err != nil {
			ww.startFailureRecovery <- true
			<-ww.endFailureRecovery
			continue
		}
		lsplog.Vlogf(3, "[Worker] Recieved: %s", string(req))
		responseParts := strings.Split(string(req), " ")
		i, _ := strconv.Atoi(responseParts[1])
		ww.reqLock.Lock()
		ww.load += i
		ww.reqList.PushBack(i)
		ww.reqLock.Unlock()
	}
}
func (w *Worker) UpdateWorker(args *commproto.UpdateWorkerArgs, reply *commproto.UpdateWorkerReply) error {
	<-w.startFailureRecovery
	w.cli, _ = lsp12.NewLspClient(reply.LoadBalancer, &lsp12.LspParams{5, 2000})
	w.endFailureRecovery <- true
	return nil
}