Skip to content

Commit 6d87f75

Browse files
authored
Ble hotspot (#223)
* BLE * Update scan.go * added test server file to run wap server * Update server.go * Update server.go
1 parent 6f5a65b commit 6d87f75

File tree

4 files changed

+152
-204
lines changed

4 files changed

+152
-204
lines changed

wap/pkg/server/server.go

Lines changed: 57 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,21 @@ type Config struct {
4545
IpniPublisherIdentity string `yaml:"ipniPublisherIdentity"`
4646
}
4747

48+
type multiCloser struct {
49+
listeners []io.Closer
50+
}
51+
52+
// Implement Close method for multiCloser
53+
func (mc *multiCloser) Close() error {
54+
var err error
55+
for _, l := range mc.listeners {
56+
if cerr := l.Close(); cerr != nil {
57+
err = cerr
58+
}
59+
}
60+
return err
61+
}
62+
4863
func checkPathExistAndFileNotExist(path string) string {
4964
dir := filepath.Dir(path)
5065

@@ -753,16 +768,21 @@ func Serve(peerFn func(clientPeerId string, bloxSeed string) (string, error), ip
753768
mux.HandleFunc("/account/id", accountIdHandler)
754769
mux.HandleFunc("/account/seed", accountSeedHandler)
755770

756-
listenAddr := ""
771+
mc := &multiCloser{
772+
listeners: []io.Closer{},
773+
}
774+
775+
// Track successful addresses
776+
var successfulAddresses []string
757777

778+
// Try first listener
779+
listenAddr := ""
758780
if ip == "" {
759781
ip = config.IPADDRESS
760782
}
761-
762783
if port == "" {
763784
port = config.API_PORT
764785
}
765-
766786
listenAddr = ip + ":" + port
767787

768788
ln, err := net.Listen("tcp", listenAddr)
@@ -771,11 +791,6 @@ func Serve(peerFn func(clientPeerId string, bloxSeed string) (string, error), ip
771791
ip, err = getIPFromSpecificNetwork(ctx, config.HOTSPOT_SSID)
772792
if err != nil {
773793
log.Errorw("Failed to use IP of hotspot", "err", err)
774-
/*ip, err = getNonLoopbackIP()
775-
if err != nil {
776-
log.Errorw("Failed to get non-loopback IP address for serve", "err", err)
777-
ip = "0.0.0.0"
778-
}*/
779794
ip = "0.0.0.0"
780795
}
781796
listenAddr = ip + ":" + port
@@ -784,17 +799,41 @@ func Serve(peerFn func(clientPeerId string, bloxSeed string) (string, error), ip
784799
if err != nil {
785800
listenAddr = "0.0.0.0:" + port
786801
ln, err = net.Listen("tcp", listenAddr)
787-
if err != nil {
788-
log.Errorw("Listen could not initialize for serve", "err", err)
789-
}
790802
}
791803
}
792804

793-
log.Info("Starting server at " + listenAddr)
794-
go func() {
795-
if err := http.Serve(ln, mux); err != nil {
796-
log.Errorw("Serve could not initialize", "err", err)
797-
}
798-
}()
799-
return ln
805+
if err == nil {
806+
mc.listeners = append(mc.listeners, ln)
807+
successfulAddresses = append(successfulAddresses, listenAddr)
808+
log.Info("Starting server at " + listenAddr)
809+
go func() {
810+
if err := http.Serve(ln, mux); err != nil && !strings.Contains(err.Error(), "use of closed network connection") {
811+
log.Errorw("Serve could not initialize", "err", err)
812+
}
813+
}()
814+
}
815+
816+
// Try second listener (localhost)
817+
localhostAddr := "127.0.0.1:" + port
818+
ln1, err1 := net.Listen("tcp", localhostAddr)
819+
if err1 == nil {
820+
mc.listeners = append(mc.listeners, ln1)
821+
successfulAddresses = append(successfulAddresses, localhostAddr)
822+
go func() {
823+
if err := http.Serve(ln1, mux); err != nil && !strings.Contains(err.Error(), "use of closed network connection") {
824+
log.Errorw("Serve could not initialize on 127.0.0.1", "err", err)
825+
}
826+
}()
827+
} else {
828+
log.Errorw("Failed to use 127.0.0.1 for serve", "err", err1)
829+
}
830+
831+
// Print summary of successful listeners
832+
if len(successfulAddresses) > 0 {
833+
log.Infof("Server successfully listening on: %s", strings.Join(successfulAddresses, ", "))
834+
} else {
835+
log.Error("Failed to start server on any address")
836+
}
837+
838+
return mc
800839
}

wap/pkg/wifi/init.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ var log = logging.Logger("fula/wap/wifi")
1515

1616
const maxRetries = 4
1717

18-
var TimeLimit = 10 * time.Second
18+
var TimeLimit = 25 * time.Second
1919

2020
func init() {
2121
// Working Directory

0 commit comments

Comments
 (0)