实现获取当前设备地址和负载均衡

remotes/1718373473996375672/master
zimujun20 2 years ago
parent 7b70cc660e
commit 38f3778b7a

@ -1,47 +0,0 @@
package android
import (
"fmt"
"net/http"
"database/sql"
_ "go-sqlite3-master"
"application/pkg/app"
"github.com/gin-gonic/gin"
)
func getMacAddress() (macAddress []string) {
netInterfaces, err := net.Interfaces()
if err != nil {
fmt.Printf("fail to get net interfaces: %v", err)
return macAddress
}
for _, netInterface := range netInterfaces {
MacAddress := netInterface.HardwareAddr.String()
if len(MacAddress) == 0 {
continue
}
macAddress = append(macAddress, MacAddress)
}
return macAddress
}
func sendMacAddress(c *gin.Context) {
appG := app.Gin{C: c}
macAddress = getMacAddress()
db, err := sql.Open("sqlite3", "info.db")
if err != nil {
fmt.Println(err)
}
defer db.Close()
db.Exec(`insert into Endpoint values (?,?)`, macAddress, 0)
appG.Response(http.StatusOK, "成功", null)
}
func mediaPipe(c *gin.Context) {
appG := app.Gin{C: c}
//这里将视频流转化为自然语言
data :=
appG.Response(http.StatusOK, "成功", data)
}

@ -1,54 +0,0 @@
package pc
import (
"database/sql"
_ "go-sqlite3-master"
"net/http"
)
//拉取在线的算力设备
func endpoint(w http.ResponseWriter, r *http.Request) {
db, err := sql.Open("sqlite3", "info.db")
if err != nil {
fmt.Println(err)
}
defer db.Close()
rows, _ := db.Query(`select endpoint.macAddress from Endpoint where endpoint.times < Average.average;`)
defer rows.Close()
var endpoint Endpoint
var endpoints = []string {}
for rows.Next() {
rows.Scan(&endpoint.macAddress)
endpoints.append(endpoint.macAddress)
}
rows.Close()
}
func shuffle(n int) []int {
b := rand.Perm(n)
return b
}
func request(params map[string]interface{}) error {
var indexes = []int {0,1,2,3,4,5,6}
var err error
shuffle(indexes)
maxRetryTimes := 3
idx := 0
for i :=0; i < maxRetryTimes; i++ {
err = apiRequest(params, indexes[idx])
if err == nil {
break
}
idx++
}
if err != nil {
//logging
return err
}
return nil
}

@ -0,0 +1,5 @@
module main.go
go 1.19
require github.com/mattn/go-sqlite3 v1.14.15 // indirect

@ -0,0 +1,2 @@
github.com/mattn/go-sqlite3 v1.14.15 h1:vfoHhTN1af61xCRSWzFIWzx2YskyMTwHLrExkBOjvxI=
github.com/mattn/go-sqlite3 v1.14.15/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=

Binary file not shown.

@ -1,11 +1,9 @@
package main
import (
"fmt"
"log"
"net/http"
"application/routers"
"webserver/routers"
)
func main() {
@ -18,4 +16,4 @@ func main() {
if err := server.ListenAndServe(); err != nil {
log.Printf("start http server failed %s", err)
}
}
}

@ -0,0 +1,92 @@
package android
import (
"fmt"
"net"
"database/sql"
"time"
"pkg/app"
"errors"
_ "github.com/mattn/go-sqlite3"
"github.com/gin-gonic/gin"
)
//获取本设备的mac地址
func getMacAddress() (macAddress string) {
netInterfaces, err := net.Interfaces()
if err != nil {
fmt.Printf("fail to get net interfaces: %v", err)
return macAddress
}
for _, netInterface := range netInterfaces {
if ( netInterface.Name == "WLAN" ) {
macAddress := netInterface.HardwareAddr
return macAddress.String()
}
}
return macAddress
}
//获取本设备的IP地址
func getIpAddress() (addr string, err error) {
var (
ief *net.Interface
addrs []net.Addr
ipv4Addr net.IP
)
if ief, err = net.InterfaceByName("WLAN"); err != nil {
return
}
if addrs, err = ief.Addrs(); err != nil {
return
}
for _, addr := range addrs {
if ipv4Addr = addr.(*net.IPNet).IP.To4(); ipv4Addr != nil {
break
}
}
if ipv4Addr == nil {
return "", errors.New(fmt.Sprintf("interface WLAN don't have an ipv4 address\n"))
}
return ipv4Addr.String(), nil
}
//向服务器发送本设备的mac地址和IP地址
func sendAddress(c *gin.Context) {
appG := app.Gin{C: c}
now := time.Now()
hour := now.Hour()
minute := now.Minute()
t := hour*100 + minute
macAddress := getMacAddress()
ipAddress, err := getIpAddress()
if err != nil {
fmt.Println(err)
}
db, err := sql.Open("sqlite3", "info.db")
if err != nil {
fmt.Println(err)
}
defer db.Close()
rows, err := db.Query(`select macAddress from Endpoint where macAddress = ?;`, macAddress)
defer rows.Close()
var MacAddress string
rows.Next()
rows.Scan(&MacAddress)
if MacAddress == "" {
db.Exec(`insert into Endpoint values (?,?,?,?);`, macAddress, ipAddress, 0, t)
} else {
db.Exec(`update Endpoint set ipAddress = ? where macAddress = ?;`, ipAddress, macAddress)
db.Exec(`update Endpoint set isOnLine = ? where macAddress = ?;`, t, macAddress)
}
appG.Response(http.StatusOK, "成功", null)
}
func mediaPipe(c *gin.Context) {
appG := app.Gin{C: c}
//这里将视频流转化为自然语言
data :=
appG.Response(http.StatusOK, "成功", data)
}

@ -9,27 +9,25 @@ import (
"github.com/gin-gonic/gin"
)
func updateAverage (c *gin.Context) {
appG := app.Gin{C: c}
func updateAverage () {
macAddress = getMacAddress()
db, err := sql.Open("sqlite3", "info.db")
if err != nil {
fmt.Println(err)
}
defer db.Close()
db.Exec(`update Endpoint set times = times + 1 where macAddress = ?`, macAddress)
db.Exec(`update Endpoint set times = times + 1 where macAddress = ?;`, macAddress)
rows, err := db.Query(`select SUM( times ) from Endpoint`)
defer rows.Close()
var sumOfTimes int
rows.Next()
rows.Scan(&sumOfTimes)
rows, err := db.Query(`select COUNT( macAddress ) from Endpoint`)
rows, err = db.Query(`select COUNT( macAddress ) from Endpoint;`)
defer rows.Close()
var countOfMacAddress int
rows.Next()
rows.Scan(&countOfMacAddress)
db.Exec(`update Average set average = ?/?`sendMacAddress, countOfMacAddress)
appG.Response(http.StatusOK, "成功", null)
db.Exec(`update Average set average = ?/?;`sumOfTimes, countOfMacAddress)
}
func wordSend (c *gin.Context) {

@ -0,0 +1,62 @@
package pc
import (
"database/sql"
_ "github.com/mattn/go-sqlite3"
"time"
"math/rand"
"strings"
)
//拉取在线的算力设备
func ipAddress() []string {
now := time.Now()
hour := now.Hour()
minute := now.Minute()
t := hour*100 + minute -100
db, err := sql.Open("sqlite3", "info.db")
if err != nil {
fmt.Println(err)
}
defer db.Close()
rows, _ := db.Query(`select average from Average;`)
defer rows.Close()
var average int
rows.Next()
rows.Scan(&average)
rows, _ = db.Query(`select ipAddress from Endpoint where times <= ? and isOnLine >= ?;`, average, t)
defer rows.Close()
var endpoint Endpoint
ipAddress := ""
for rows.Next() {
rows.Scan(&endpoint.ipAddress)
ipAddress += endpoint.ipAddress + ","
}
rows.Close()
sep := ","
IpAddress := strings.Split(ipAddress, sep)
return IpAddress
}
func init() {
rand.Seed(time.Now().UnixNano())
}
//洗牌算法
func shuffle(n []string) {
for i := 0; i < len(n); i++ {
a := rand.Intn(len(n))
b := rand.Intn(len(n))
n[a], n[b] = n[b], n[a]
}
}
//返回可进行运算的设备地址
func request() []string {
var ipAddress = ipAddress()
for i := 0; i < 3; i++ {
shuffle(ipAddress)
}
return ipAddress
}

@ -1,8 +1,10 @@
package pkg
type Endpoint struct {
macAddress string //设备地址
macAddress string //设备mac地址
ipAddress string //设备IP地址
times int //贡献算力次数
isOnLine int //设备登录时间
}
type Average struct {

@ -3,8 +3,8 @@ package routers
import (
"net/http"
android "application/api/android"
pc "application/api/pc"
android "webserver/api/android"
pc "webserver/api/pc"
"github.com/gin-gonic/gin"
)

@ -0,0 +1,8 @@
//获取手语视频
func getMedia() {}
//翻译视频为自然语言
func mediaToWords() {}
//显示自然语言
func display() {}

@ -0,0 +1,24 @@
//输入自然语言
function enterWords() {
words = infos.words.value
wordsToMedia(words)
}
//翻译自然语言为动画
function wordsToMedia(words) {}
//显示动画
function display(media) {
var xmlhttp;
if (window.XMLHttpRequest) xmlhttp=new XMLHttpRequest();
else xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
if (xmlhttp.responseText=="成功") alert("删除成功!")
else alert("操作失败")
}
}
xmlhttp.open("GET","http://127.0.0.1:8000//"+"?user="+user+"&ps="+ps,true);
xmlhttp.send();
}
Loading…
Cancel
Save