Esempio n. 1
0
File: client.go Progetto: WnP/go-osc
// TODO: Revise the client!
func main() {
	ip := "localhost"
	port := 8765
	client := osc.NewClient(ip, port)

	fmt.Println("### Welcome to go-osc transmitter demo")
	fmt.Println("Please, select the OSC packet type you would like to send:")
	fmt.Println("\tm: OSCMessage")
	fmt.Println("\tb: OSCBundle")
	fmt.Println("\tPress \"q\" to exit")
	fmt.Printf("# ")

	reader := bufio.NewReader(os.Stdin)
	for {
		line, err := reader.ReadBytes('\n')
		if err == io.EOF {
			break
		}

		if err != nil {
			fmt.Println("Error: " + err.Error())
			os.Exit(1)
		}

		sline := strings.TrimRight(string(line), "\n")
		if sline == "m" {
			message := osc.NewMessage("/message/address")
			message.Append(int32(12345))
			message.Append("teststring")
			message.Append(true)
			message.Append(false)
			client.Send(message)
		} else if sline == "b" {
			bundle := osc.NewBundle(time.Now())
			message1 := osc.NewMessage("/bundle/message/1")
			message1.Append(int32(12345))
			message1.Append("teststring")
			message1.Append(true)
			message1.Append(false)
			message2 := osc.NewMessage("/bundle/message/2")
			message2.Append(int32(3344))
			message2.Append(float32(101.9))
			message2.Append("string1")
			message2.Append("string2")
			message2.Append(true)
			bundle.Append(message1)
			bundle.Append(message2)
			client.Send(bundle)
		} else if sline == "q" {
			fmt.Println("Exit!")
			os.Exit(0)
		}

		fmt.Printf("# ")
	}
}
Esempio n. 2
0
func register(msg *osc.Message) {
	if len(msg.Arguments) != 2 {
		fmt.Println("Wrong num args")
		return
	}

	ip, ok := msg.Arguments[0].(string)
	if !ok {
		fmt.Println("IP not a string")
		return
	}

	port, ok := msg.Arguments[1].(int32)
	if !ok {
		fmt.Println("Port not an int")
		return
	}

	clients = append(clients, osc.NewClient(ip, int(port)))
	fmt.Println(clients)
}
Esempio n. 3
0
func main() {
	flag.Parse()
	client := osc.NewClient(*serverHost, *serverPort)
	fmt.Println("server:", *serverHost, "port:", *serverPort)

	c := 0

	for {
		//testMessage(client)
		sendMessage(client, ((c+rand.Intn(4))%4)+1, rand.Intn(1)*3, "kick")
		sendMessage(client, ((c+rand.Intn(4))%4)+1, rand.Intn(1)*3, "snare")
		sendMelodyMessage(client, ((c+rand.Intn(4))%4)+1, ((c+rand.Intn(4))%4)+1, rand.Intn(1)*3, "bass")
		sendMessage(client, ((c+rand.Intn(4))%4)+1, rand.Intn(1)*3, "hh")
		sendMelodyMessage(client, ((c+rand.Intn(3))%3)+1, ((c+rand.Intn(4))%4)+1, rand.Intn(1)*3, "chords")
		sendMelodyMessage(client, ((c+rand.Intn(8))%8)+1, ((c+rand.Intn(4))%4)+1, rand.Intn(1)*3, "melody")

		dur := time.Duration(rand.Intn(5)) * time.Second
		fmt.Println(dur)
		time.Sleep(dur)
		c++

	}

}
Esempio n. 4
0
func main() {
	flag.Parse()

	devices, err := pcap.FindAllDevs()
	if err != nil {
		log.Fatal(err)
	}

	filter := ""
	for _, dev := range devices {
		if dev.Name == *device {
			fmt.Println(dev)
			for _, address := range dev.Addresses {
				filter = fmt.Sprintf("not ip host %s ", address.IP)
				hostIP = address.IP.String()
				break
			}
		}
	}

	handle, err := pcap.OpenLive(*device, 65536, true, 0)
	if err != nil {
		defer handle.Close()
		panic(err)
	}

	err = handle.SetBPFFilter(filter)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(handle.LinkType())
	packetSource := gopacket.NewPacketSource(handle, handle.LinkType())
	fmt.Println("Connection to ", *serverHost, *serverPort)
	client := osc.NewClient(*serverHost, *serverPort)

	var clients = struct {
		sync.RWMutex
		m              map[string]*activity
		instrumentPool []int
	}{m: make(map[string]*activity)}

	instruments := map[int]*instrument{
		0: &instrument{"snare", mapSnareLevel, adjustDrumLevel, sendDrumMessage, mapPitchLevel},
		1: &instrument{"kick", mapKickLevel, adjustDrumLevel, sendDrumMessage, mapPitchLevel},
		2: &instrument{"bass", mapMelodyLevel, adjustMelodyLevel, sendMelodyMessage, mapPitchLevel},
		3: &instrument{"hh", mapDrumLevel, adjustDrumLevel, sendDrumMessage, mapPitchLevel},
		4: &instrument{"melody", mapMelodyLevel, adjustMelodyLevel, sendMelodyMessage, mapPitchLevel},
		5: &instrument{"melody2", mapMelodyLevel, adjustMelodyLevel, sendMelodyMessage, mapPitchLevel},
		6: &instrument{"chords", mapChordLevel, adjustLevel, sendMelodyMessage, mapChordPitchLevel},
		7: &instrument{"conga", mapDrumLevel, adjustDrumLevel, sendDrumMessage, mapPitchLevel},
		8: &instrument{"clap", mapDrumLevel, adjustDrumLevel, sendDrumMessage, mapPitchLevel},
	}

	server := serv()
	t := &ticker{msgDelay: time.Duration(2) * time.Second, resetDelay: time.Duration(16) * time.Second}
	oscServ(t)

	go func() {
		for {
			clients.Lock()

			var keys []string
			for k := range clients.m {
				keys = append(keys, k)
			}
			sort.Strings(keys)

			delayValue := 0
			if len(keys) > 4 {
				delayValue = 1
			}
			sendDelayMessage(client, delayValue)

			server.BroadcastTo("chat", "chat clear", "")
			for _, key := range keys {
				value := clients.m[key]
				elapsed := time.Since(value.since)
				pps := float64(value.currentPackets()) / elapsed.Seconds()
				bps := float64(value.sizeSum) / elapsed.Seconds()

				instrument, ok := instruments[value.instrument]
				info := ""

				if ok {
					targetLevel, offbeat := instrument.mapLevel(bps)
					if targetLevel == 0 {
						clients.instrumentPool = append(clients.instrumentPool, value.instrument)
						sort.Ints(clients.instrumentPool)
						delete(clients.m, key)
					}
					pitch := instrument.mapPitchLevel(pps)
					instrument.adjustCurrentLevel(value, targetLevel)
					instrument.sendMessage(client, value.currentLevel, pitch, offbeat, instrument.name)

					info = fmt.Sprintf("MAC: %s, instrument: %s, pps: %.2f, bps: %.2f, elapsed: %.2f, level: %d, pitch: %d", key, instrument.name, pps, bps, elapsed.Seconds(), value.currentLevel, pitch)
					fmt.Println(info)
					server.BroadcastTo("chat", "chat message", info)
				} else {
					clients.instrumentPool = append(clients.instrumentPool, value.instrument)
					sort.Ints(clients.instrumentPool)
					delete(clients.m, key)
				}
			}

			if len(keys) > 0 {
				fmt.Println()
			}
			clients.Unlock()

			t.RLock()
			var dl = t.msgDelay
			fmt.Println("msg tick at", t.msgDelay)
			t.RUnlock()
			time.Sleep(dl)
		}
	}()

	go func() {
		for {
			clients.Lock()

			for _, value := range clients.m {
				value.since = time.Now()
				value.packets = 0
				value.sizeSum = 0
			}
			clients.Unlock()

			t.RLock()
			var dl = t.resetDelay
			fmt.Println("reset tick at", t.resetDelay)
			t.RUnlock()
			time.Sleep(dl)
		}
	}()

	for packet := range packetSource.Packets() {
		// fmt.Println(packet)
		// Let's see if the packet is an ethernet packet
		ethernetLayer := packet.Layer(layers.LayerTypeEthernet)
		if ethernetLayer != nil {
			// fmt.Println("Ethernet layer detected.")
			ethernetPacket, _ := ethernetLayer.(*layers.Ethernet)
			// fmt.Println("Source MAC: ", ethernetPacket.SrcMAC, "Destination MAC: ", ethernetPacket.DstMAC)
			// Ethernet type is typically IPv4 but could be ARP or other
			// fmt.Println("Ethernet type: ", ethernetPacket.EthernetType)

			clients.Lock()
			packetLength := len(ethernetPacket.Payload)

			_, ok := clients.m[ethernetPacket.DstMAC.String()]
			if ok {
				p := clients.m[ethernetPacket.DstMAC.String()]
				p.increment()
				p.addPacketSize(packetLength)
			} else {
				var instrument int
				if len(clients.instrumentPool) != 0 {
					instrument = clients.instrumentPool[0]
					clients.instrumentPool = append(clients.instrumentPool[:0], clients.instrumentPool[0+1:]...)
				} else {
					instrument = len(clients.m)
				}
				clients.m[ethernetPacket.DstMAC.String()] = &activity{1, packetLength, time.Now(), instrument, 0}
			}
			clients.Unlock()
		}
	}
}