-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlockserver.go
More file actions
120 lines (104 loc) · 2.84 KB
/
lockserver.go
File metadata and controls
120 lines (104 loc) · 2.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package lockserver
import (
"context"
"errors"
"fmt"
"log"
"net/http"
"net/rpc"
"os"
"os/signal"
"strconv"
"sync"
"time"
)
// SafeLockMap is the lockserver's data structure
type SafeLockMap struct {
LockMap map[string]bool
Mutex sync.Mutex
}
// Service is the service exposed by this server to the clients
type Service int
var lockMap = newSafelockMap()
func newSafelockMap() (lockMap SafeLockMap) {
lockMap = SafeLockMap{}
lockMap.LockMap = make(map[string]bool)
return
}
// HealthCheck acts like a ping
func (s *Service) HealthCheck(ip string, counter *int) error {
fmt.Println("HealthCheck ping")
return nil
}
// CheckAcquire returns nil if the file is acquired
func (s *Service) CheckAcquire(fileID string, counter *float32) error {
lockMap.Mutex.Lock()
if lockMap.LockMap[fileID] {
lockMap.Mutex.Unlock()
return nil
}
lockMap.Mutex.Unlock()
return errors.New("Can't access file, locked by other user")
}
// Acquire function lets a client acquire a lock on an object.
func (s *Service) Acquire(fileID string, counter *float32) error {
lockMap.Mutex.Lock()
if lockMap.LockMap[fileID] {
lockMap.Mutex.Unlock()
return errors.New("Can't access file, locked by other user")
}
lockMap.LockMap[fileID] = true
lockMap.Mutex.Unlock()
log.Printf("File: %v locked\n", fileID)
return nil
}
// CheckRelease returns nil if the file is released
func (s *Service) CheckRelease(fileID string, counter *float32) error {
lockMap.Mutex.Lock()
if lockMap.LockMap[fileID] {
lockMap.Mutex.Unlock()
return errors.New("Can't release lock on file, wasn't locked before")
}
lockMap.Mutex.Unlock()
return nil
}
// Release lets a client to release a lock on an object.
func (s *Service) Release(fileID string, counter *float32) error {
fmt.Println("WE")
lockMap.Mutex.Lock()
if lockMap.LockMap[fileID] {
delete(lockMap.LockMap, fileID)
log.Printf("File: %v's lock released\n", fileID)
lockMap.Mutex.Unlock()
return nil
}
lockMap.Mutex.Unlock()
return errors.New("Can't release lock on file, wasn't locked before")
}
// StartServer starts the rpc server for lockserver
func StartServer(shutDownSignal chan os.Signal) {
service := new(Service)
port := 55550 //flag.Int("port", 55550, "service port")
ip := "0.0.0.0" //flag.String("ip", "0.0.0.0", "service ip")
rpcServer := rpc.NewServer()
err := rpcServer.Register(service)
if err != nil {
log.Fatalf("Error in registering the RPC server: %v\n", err)
}
server := &http.Server{
Addr: ip + ":" + strconv.Itoa(port),
Handler: rpcServer,
}
// stop := make(chan os.Signal, 1)
signal.Notify(shutDownSignal, os.Interrupt)
go func() {
err := server.ListenAndServe()
if err != nil && err != http.ErrServerClosed {
log.Fatal("listen error:", err)
}
}()
<-shutDownSignal
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Millisecond)
server.Shutdown(ctx)
cancel()
}