-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
207 lines (175 loc) · 4.28 KB
/
main.go
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package main
import (
"encoding/gob"
"fmt"
"log"
"net"
"sync"
)
type Server struct {
sync.Mutex
hashMap map[int][]string
clientData map[string][]int
}
func NewServer() *Server {
return &Server{
hashMap: make(map[int][]string),
clientData: make(map[string][]int),
}
}
func (s *Server) handleConnection(conn net.Conn) {
defer func() {
clientIP := conn.RemoteAddr().String()
s.cleanupClientData(clientIP)
conn.Close()
}()
for {
var requestType string
decoder := gob.NewDecoder(conn)
if err := decoder.Decode(&requestType); err != nil {
if err.Error() == "EOF" {
log.Println("Client disconnected")
return
}
log.Println("Error decoding request type:", err)
return
}
if requestType == "store" {
s.handleStoreRequest(conn, decoder)
} else if requestType == "create" {
s.handleCreateRequest(conn, decoder)
} else if requestType == "delete" {
s.handleDeleteRequest(conn, decoder)
} else if requestType == "query" {
s.handleQueryRequest(conn, decoder)
} else {
log.Println("Unknown request type:", requestType)
}
}
}
func (s *Server) handleStoreRequest(conn net.Conn, decoder *gob.Decoder) {
var clientHashes []int
if err := decoder.Decode(&clientHashes); err != nil {
log.Println("Error decoding data:", err)
return
}
s.Lock()
clientIP := conn.RemoteAddr().String()
for _, hash := range clientHashes {
if _, exists := s.hashMap[hash]; !exists {
s.hashMap[hash] = []string{}
}
s.hashMap[hash] = append(s.hashMap[hash], clientIP)
s.clientData[clientIP] = append(s.clientData[clientIP], hash)
}
s.Unlock()
printHashMap(s.hashMap)
}
func (s *Server) handleCreateRequest(conn net.Conn, decoder *gob.Decoder) {
var fileHash int
if err := decoder.Decode(&fileHash); err != nil {
log.Println("Error decoding file hash:", err)
return
}
clientIP := conn.RemoteAddr().String()
s.Lock()
if _, exists := s.hashMap[fileHash]; !exists {
s.hashMap[fileHash] = []string{}
}
s.hashMap[fileHash] = append(s.hashMap[fileHash], clientIP)
s.clientData[clientIP] = append(s.clientData[clientIP], fileHash)
s.Unlock()
fmt.Printf("File created by %s: Hash %d\n", clientIP, fileHash)
}
func (s *Server) handleDeleteRequest(conn net.Conn, decoder *gob.Decoder) {
var fileHash int
if err := decoder.Decode(&fileHash); err != nil {
log.Println("Error decoding file hash:", err)
return
}
clientIP := conn.RemoteAddr().String()
s.Lock()
if ips, exists := s.hashMap[fileHash]; exists {
for i, ip := range ips {
if ip == clientIP {
s.hashMap[fileHash] = append(ips[:i], ips[i+1:]...)
break
}
}
if len(s.hashMap[fileHash]) == 0 {
delete(s.hashMap, fileHash)
}
}
s.clientData[clientIP] = removeFromSlice(s.clientData[clientIP], fileHash)
s.Unlock()
fmt.Printf("File deleted by %s: Hash %d\n", clientIP, fileHash)
}
func (s *Server) handleQueryRequest(conn net.Conn, decoder *gob.Decoder) {
var hash int
if err := decoder.Decode(&hash); err != nil {
log.Println("Error decoding hash:", err)
return
}
s.Lock()
ips := s.hashMap[hash]
s.Unlock()
encoder := gob.NewEncoder(conn)
encoder.Encode(ips)
}
func (s *Server) cleanupClientData(clientIP string) {
s.Lock()
defer s.Unlock()
hashes, exists := s.clientData[clientIP]
if !exists {
return
}
for _, hash := range hashes {
ips := s.hashMap[hash]
for i, ip := range ips {
if ip == clientIP {
s.hashMap[hash] = append(ips[:i], ips[i+1:]...)
break
}
}
if len(s.hashMap[hash]) == 0 {
delete(s.hashMap, hash)
}
}
delete(s.clientData, clientIP)
log.Printf("Cleaned up data for client: %s\n", clientIP)
}
func printHashMap(hashMap map[int][]string) {
fmt.Println("Hash Map:")
for hash, ips := range hashMap {
fmt.Printf("Hash: %d\n", hash)
fmt.Println(" IPs:")
for _, ip := range ips {
fmt.Printf(" %s\n", ip)
}
}
}
func removeFromSlice(slice []int, val int) []int {
for i, v := range slice {
if v == val {
return append(slice[:i], slice[i+1:]...)
}
}
return slice
}
func main() {
server := NewServer()
ln, err := net.Listen("tcp", ":8080")
if err != nil {
log.Fatal(err)
}
defer ln.Close()
fmt.Println("Server is listening on port 8080...")
for {
conn, err := ln.Accept()
if err != nil {
log.Println("Error accepting connection:", err)
continue
}
go server.handleConnection(conn)
}
}