package main
// C:\Users\ebata\goga\3-12
import (
"fmt"
"sync"
"time"
)
type BUS struct {
number int
}
//var l sync.Mutex
//var c1 chan int
func (bus *BUS) bus_func_recv(lr *sync.Mutex, cr *sync.Cond) {
// 受信(ブロードキャスト専用)
fmt.Println("bus.number by recv:", bus.number)
lr.Lock()
defer lr.Unlock()
fmt.Println("before cr.Wait")
for {
cr.Wait() // ロック
fmt.Println("hi ") // ここで受信処理を行う
}
}
func (bus *BUS) bus_func_send(lb *sync.Mutex, ch1 chan int) {
// 送信専用
fmt.Println("bus.number by send:", bus.number)
lb.Lock()
defer lb.Unlock()
ch1 <- bus.number
}
func main() {
//wg := sync.WaitGroup{}
l := sync.Mutex{}
c1 := make(chan int)
l2 := sync.Mutex{}
c2 := sync.NewCond(&l2)
for i := 0; i < 10; i++ {
bus := BUS{i}
go bus.bus_func_send(&l, c1)
go bus.bus_func_recv(&l2, c2)
}
time.Sleep(time.Second)
c2.Broadcast()
for {
select {
case v := <-c1:
fmt.Println(v)
//default:
// fmt.Println("default")
case <-time.After(3 * time.Second):
return
}
}
close(c1)
//wg.Wait()
//time.Sleep(3 * time.Second)
package main
import (
"fmt"
"math/rand"
"sync"
"time"
)
type PERSON struct {
lon_destination float64
lat_destination float64
lon_arrival float64
lat_arrival float64
bus BUS
}
type BUS struct {
lon_present float64
lat_present float64
// person []PERSON
}
func random(min, max float64) float64 {
rand.Seed(time.Now().UnixNano())
return rand.Float64()*(max-min) + min
}
func person(i int, wg *sync.WaitGroup) {
defer wg.Done()
person := PERSON{}
// 出発座標、到着座標の入力
person.lon_destination = random(139.480, 139.460)
person.lat_destination = random(35.602, 35.586)
person.lon_arrival = random(139.480, 139.460)
person.lat_arrival = random(35.602, 35.586)
fmt.Println(person, i)
}
func bus(i int, wg *sync.WaitGroup) {
defer wg.Done()
bus := BUS{}
fmt.Println(bus, i)
}
func main() {
var wg sync.WaitGroup
defer wg.Wait()
// バスエージェントの生成 3台
for i := 0; i < 3; i++ {
wg.Add(1)
go bus(i, &wg)
}
i := 0
for {
time.Sleep(3 * time.Second)
wg.Add(1)
i++
go person(i, &wg)
}
}
package main
// C:\Users\ebata\goga\3-15
import (
"fmt"
"sync"
"time"
)
// BroadCasterは管制システムのイメージに近い だから移動体のオブジェクトには含めない
type BroadCaster struct {
cond *sync.Cond
id int64
msg string
}
func (bc *BroadCaster) Send(msg string) {
bc.cond.L.Lock()
defer bc.cond.L.Unlock()
bc.id++
bc.msg = msg
bc.cond.Broadcast()
}
func (bc *BroadCaster) Recv(last int64) (int64, string) {
bc.cond.L.Lock()
defer bc.cond.L.Unlock()
for bc.id == last {
bc.cond.Wait()
}
return bc.id, bc.msg
}
// broadcaster_busをグローバルで実体化
var (
broadcaster_bus = &BroadCaster{
cond: sync.NewCond(&sync.Mutex{}),
}
)
// broadcaster_personをグローバルで実体化
var (
broadcaster_person = &BroadCaster{
cond: sync.NewCond(&sync.Mutex{}),
}
)
// 単一通信の構造体
type SingleCaster struct {
ch chan int // 単一通信路
lock sync.Mutex // 単一通信路のロック
}
// バス用単一通信の実体化
var (
sc_bus = &SingleCaster{
lock: sync.Mutex{},
ch: make(chan int),
}
)
// 人間用単一通信の実体化
var (
sc_person = &SingleCaster{
lock: sync.Mutex{},
ch: make(chan int),
}
)
// 人間用単一通信の実体化
type CONTROL struct {
number int // 管制番号
}
func (control *CONTROL) control_start() {
// バスへの一斉送信
for i := 0; i < 2; i++ {
time.Sleep(1 * time.Second)
broadcaster_bus.Send(fmt.Sprintf("hello, bus world %d", i))
}
// 人間への一斉送信
for i := 0; i < 2; i++ {
time.Sleep(1 * time.Second)
broadcaster_person.Send(fmt.Sprintf("hello, person world %d", i))
}
for {
select {
// case v := <-c1:
case v_b := <-sc_bus.ch:
fmt.Println("catched from bus send", v_b)
case v_p := <-sc_person.ch:
fmt.Println("catched from person send", v_p)
//default:
// fmt.Println("default")
case <-time.After(3 * time.Second):
return
}
}
}
type BUS struct {
number int // バス車両番号
}
func (bus *BUS) bus_func_recv() {
last := int64(0)
for {
id, msg := broadcaster_bus.Recv(last)
last = id
fmt.Println("broadcaset recv:", bus.number, msg)
}
}
func (bus *BUS) bus_func_send() {
// 送信専用
fmt.Println("bus.number by send:", bus.number)
sc_bus.lock.Lock()
defer sc_bus.lock.Unlock()
sc_bus.ch <- bus.number
}
type PERSON struct {
number int // 人間番号
live bool // 存在フラグ 存在:true 消滅:false
}
func (person *PERSON) person_func_recv() {
last := int64(0)
for {
if person.live {
return
}
id, msg := broadcaster_person.Recv(last)
last = id
fmt.Println("broadcaset recv:", person.number, msg)
}
}
func (person *PERSON) person_func_send() {
// 送信専用
fmt.Println("person.number by send:", person.number)
for {
sc_person.lock.Lock()
sc_person.ch <- person.number
sc_person.lock.Unlock()
time.Sleep(time.Second)
}
}
func main() {
// バス3台
for i := 0; i < 3; i++ {
bus := BUS{i}
go bus.bus_func_send()
go bus.bus_func_recv()
}
// 人間10人
for i := 0; i < 10; i++ {
person := PERSON{i, true}
go person.person_func_send()
go person.person_func_recv()
}
time.Sleep(time.Second)
control := CONTROL{}
go control.control_start()
//close(c1)
//wg.Wait()
time.Sleep(10 * time.Second)
}
package main
// C:\Users\ebata\goga\3-17
import (
"fmt"
"sync"
"time"
)
// BroadCasterは管制システムのイメージに近い だから移動体のオブジェクトには含めない
type BroadCaster struct {
cond *sync.Cond
id int64
msg string
}
func (bc *BroadCaster) Send(msg string) {
bc.cond.L.Lock()
defer bc.cond.L.Unlock()
bc.id++
bc.msg = msg
bc.cond.Broadcast()
}
func (bc *BroadCaster) Recv(last int64) (int64, string) {
bc.cond.L.Lock()
defer bc.cond.L.Unlock()
for bc.id == last {
bc.cond.Wait()
}
return bc.id, bc.msg
}
// broadcaster_busをグローバルで実体化
var (
broadcaster_bus = &BroadCaster{
cond: sync.NewCond(&sync.Mutex{}),
}
)
// broadcaster_personをグローバルで実体化
var (
broadcaster_person = &BroadCaster{
cond: sync.NewCond(&sync.Mutex{}),
}
)
// 単一通信の構造体
type SingleCaster struct {
ch chan int // 単一通信路
lock sync.Mutex // 単一通信路のロック
}
// バス用単一通信の実体化
var (
sc_bus = &SingleCaster{
lock: sync.Mutex{},
ch: make(chan int),
}
)
// 人間用単一通信の実体化
var (
sc_person = &SingleCaster{
lock: sync.Mutex{},
ch: make(chan int),
}
)
// 人間用単一通信の実体化
type CONTROL struct {
number int // 管制番号
}
func control_init(wg *sync.WaitGroup) {
defer wg.Done()
// バスへの一斉送信
for i := 0; i < 2; i++ {
time.Sleep(1 * time.Second)
broadcaster_bus.Send(fmt.Sprintf("hello, bus world %d", i))
}
// 人間への一斉送信
for i := 0; i < 2; i++ {
time.Sleep(1 * time.Second)
broadcaster_person.Send(fmt.Sprintf("hello, person world %d", i))
}
for {
select {
// case v := <-c1:
case v_b := <-sc_bus.ch:
fmt.Println("catched from bus send", v_b)
case v_p := <-sc_person.ch:
fmt.Println("catched from person send", v_p)
//default:
// fmt.Println("default")
case <-time.After(3 * time.Second):
return
}
}
}
type BUS struct {
number int // バス車両番号
person_list []*PERSON // バスに乗っている人
}
func (bus *BUS) bus_func_recv() {
last := int64(0)
for {
id, msg := broadcaster_bus.Recv(last)
last = id
fmt.Println("broadcaset recv:", bus.number, msg)
}
}
func (bus *BUS) bus_func_send() {
// 送信専用
fmt.Println("bus.number by send:", bus.number)
sc_bus.lock.Lock()
defer sc_bus.lock.Unlock()
sc_bus.ch <- bus.number
}
func (bus *BUS) add_person_list(person *PERSON) {
bus.person_list = append(bus.person_list, person)
}
func (bus *BUS) del_person_list(number int) {
for cnt := range bus.person_list {
if number == bus.person_list[cnt].number {
bus.person_list = append(bus.person_list[:number], bus.person_list[number+1:]...)
return
}
}
}
type PERSON struct {
number int // 人間番号
live bool // 存在フラグ 存在:true 消滅:false
}
func (person *PERSON) person_func_recv() {
last := int64(0)
for {
if person.live {
return
}
id, msg := broadcaster_person.Recv(last)
last = id
fmt.Println("broadcaset recv:", person.number, msg)
}
}
func (person *PERSON) person_func_send() {
// 送信専用
fmt.Println("person.number by send:", person.number)
for {
sc_person.lock.Lock()
sc_person.ch <- person.number
sc_person.lock.Unlock()
time.Sleep(time.Second)
}
}
func bus_init(wg *sync.WaitGroup, i int) {
defer wg.Done()
bus := BUS{number: i}
go bus.bus_func_send()
go bus.bus_func_recv()
}
func person_init(wg *sync.WaitGroup, i int) {
defer wg.Done()
person := PERSON{number: i}
go person.person_func_send()
go person.person_func_recv()
}
func main() {
wg := sync.WaitGroup{}
// バス3台
for i := 0; i < 3; i++ {
wg.Add(1)
go bus_init(&wg, i)
}
// 人間10人
for i := 0; i < 10; i++ {
wg.Add(1)
go person_init(&wg, i)
}
time.Sleep(time.Second)
// 管制センタ 1つ
wg.Add(1)
go control_init(&wg)
//close(c1)
//wg.Wait() //本来はこれだが、強制終了の為に
time.Sleep(10 * time.Second)
}