-
Notifications
You must be signed in to change notification settings - Fork 0
/
Exercise5_fileio.go~
143 lines (108 loc) · 2.71 KB
/
Exercise5_fileio.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
// go run Exercise4.go
package main
import (
"fmt"
"time"
"os"
"strconv"
"strings"
"sync"
)
type Message struct {
ID string
Word string
CurrTime time.Time
LocalIP string
RemoteIP string
RawWord string
}
func write_to_file(filename string, i int){
fmt.Println("I am OPTIMUS Primary")
// use flag os.O_APPEND|os.O_RDWR to append
//Truncate a new file
//f, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0660)
//err = f.Close()
//Write to the file
//if (err != nil) {fmt.Println(err)}
var mutex = &sync.Mutex{}
for {
time.Sleep(100*time.Millisecond)
mutex.Lock()
f, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0660)
if (err != nil) {fmt.Println(err)}
i++
data1 := strconv.Itoa(i)
data2 := []byte(data1)
_, err1 := f.Write(data2)
if (err1 != nil){fmt.Println(err)}
//Close the file
err = f.Close()
if (err != nil) {fmt.Println(err)}
mutex.Unlock()
}
}
func read_from_file(filename string, sendCh chan int) {
var mutex = &sync.Mutex{}
for {
mutex.Lock()
f, err := os.Open(filename)
//f, err := os.OpenFile(filename, os.O_RDWR, 0660)
if (err != nil) {fmt.Println(err)}
data2 := make([]byte, 100)
n, err1 := f.Read(data2)
if (err1 != nil){fmt.Println("Error at reading\n")}
data1, err2 := strconv.Atoi(strings.TrimSuffix(string(data2[:n]),"\x00"))
if (err2 != nil){fmt.Println(err2)}
sendCh <- data1
err = f.Close()
if (err != nil) {fmt.Println(err)}
mutex.Unlock()
}
}
func main() {
var compare int
var current int
var counter int
var i int
const filename string = "Counter.txt"
compare = 0
for {
f, err := os.Open(filename)
//f, err := os.OpenFile(filename, os.O_RDWR, 0660)
if (err != nil) {fmt.Println(err)}
data2 := make([]byte, 100)
n, err1 := f.Read(data2)
if (err1 != nil){fmt.Println("Error at reading\n")}
data1, err2 := strconv.Atoi(strings.TrimSuffix(string(data2[:n]),"\x00"))
if (err2 != nil){fmt.Println(err2)}
current = data1
err = f.Close()
if (err != nil) {fmt.Println(err)}
if (current == compare) {
counter++
} else {
counter = 0
compare = current
}
if (counter == 5) {
i = current
for {
fmt.Println("Sup. Im Optimus Primary")
f, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0660)
if (err != nil) {fmt.Println(err)}
i++
fmt.Println(i)
data1 := strconv.Itoa(i)
data2 := []byte(data1)
_, err1 := f.Write(data2)
if (err1 != nil){fmt.Println(err)}
//Close the file
err = f.Close()
if (err != nil) {fmt.Println(err)}
time.Sleep(100*time.Millisecond)
}
}
time.Sleep(500*time.Millisecond)
fmt.Println("Sup. Im a waste of recources")
}
}