-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
171 lines (149 loc) · 3.4 KB
/
client.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
package main
import (
"encoding/gob"
"fmt"
"net"
"os"
"strconv"
"time"
)
type Task struct {
ID string
Command string
Interval time.Duration
Stop chan bool
Repeat int
Username string
}
type Message struct {
Type string // "task", "stop", or "list"
Task *Task
TaskID string
Username string
}
func main() {
if len(os.Args) < 2 {
fmt.Println("Usage: task_cli <command> [arguments...]")
return
}
command := os.Args[1]
switch command {
case "add":
if len(os.Args) != 6 {
fmt.Println("Usage: task_cli add <command> <interval> <repeat> <username>")
return
}
addTask(os.Args[2], os.Args[3], os.Args[4], os.Args[5])
case "stop":
if len(os.Args) != 3 {
fmt.Println("Usage: task_cli stop <task_id>")
return
}
stopTask(os.Args[2])
case "list":
if len(os.Args) == 3 {
listTasksByUsername(os.Args[2])
} else {
listTasks()
}
default:
fmt.Println("Unknown command:", command)
}
}
func addTask(command, intervalStr, repeatStr, username string) {
interval, err := time.ParseDuration(intervalStr)
if err != nil {
fmt.Println("Invalid interval:", err)
return
}
repeat, err := strconv.Atoi(repeatStr)
if err != nil {
fmt.Println("Invalid repeat count:", err)
return
}
task := &Task{
ID: generateID(),
Command: command,
Interval: interval,
Stop: make(chan bool),
Repeat: repeat,
Username: username,
}
sendMessageToService(&Message{Type: "task", Task: task})
}
func stopTask(taskId string) {
sendMessageToService(&Message{Type: "stop", TaskID: taskId})
}
func listTasks() {
conn, err := net.Dial("tcp", "localhost:54030")
if err != nil {
fmt.Println("Failed to connect to service:", err)
return
}
defer conn.Close()
encoder := gob.NewEncoder(conn)
err = encoder.Encode(&Message{Type: "list", Username: ""})
if err != nil {
fmt.Println("Failed to send list request:", err)
return
}
decoder := gob.NewDecoder(conn)
var taskList []string
err = decoder.Decode(&taskList)
if err != nil {
fmt.Println("Failed to receive task list:", err)
return
}
fmt.Println("Running tasks:")
for _, taskId := range taskList {
fmt.Println(taskId)
}
}
func listTasksByUsername(username string) {
conn, err := net.Dial("tcp", "localhost:54030")
if err != nil {
fmt.Println("Failed to connect to service:", err)
return
}
defer conn.Close()
encoder := gob.NewEncoder(conn)
err = encoder.Encode(&Message{Type: "list", Username: username})
if err != nil {
fmt.Println("Failed to send list request:", err)
return
}
decoder := gob.NewDecoder(conn)
var taskList []string
err = decoder.Decode(&taskList)
if err != nil {
fmt.Println("Failed to receive task list:", err)
return
}
fmt.Println("Running tasks for user", username, ":")
for _, taskId := range taskList {
fmt.Println(taskId)
}
}
func sendMessageToService(msg *Message) {
conn, err := net.Dial("tcp", "localhost:54030")
if err != nil {
fmt.Println("Failed to connect to service:", err)
return
}
defer conn.Close()
encoder := gob.NewEncoder(conn)
fmt.Println("Sending message to service:", msg)
err = encoder.Encode(msg)
if err != nil {
fmt.Println("Failed to send message to service:", err)
} else {
if msg.Type == "task" {
fmt.Printf("Task %s added\n", msg.Task.ID)
} else if msg.Type == "stop" {
fmt.Printf("Task %s stop request sent\n", msg.TaskID)
}
}
}
func generateID() string {
return fmt.Sprintf("%d", time.Now().UnixNano())
}