func (l *Nodb) ReplicateFromReader(rb io.Reader) error { b := new(replBatch) b.wb = l.ldb.NewWriteBatch() b.l = l f := func(head *BinLogHead, event []byte) error { if b.lastHead == nil { b.lastHead = head } else if !b.lastHead.InSameBatch(head) { if err := b.Commit(); err != nil { log.Fatal("replication error %s, skip to next", err.Error()) return ErrSkipEvent } b.lastHead = head } err := l.replicateEvent(b, event) if err != nil { log.Fatal("replication error %s, skip to next", err.Error()) return ErrSkipEvent } return nil } err := ReadEventFromReader(rb, f) if err != nil { b.Rollback() return err } return b.Commit() }
func main() { log.SetFlags(log.Flags() | log.Lshortfile) var clientPairId string meetAddr := "localhost:8080" flag.StringVar(&meetAddr, "meetAddr", meetAddr, "Server for discover other") flag.StringVar(&clientPairId, "pair", "", "key to macth service") flag.Parse() log.Printf("Discover server is %s\n", meetAddr) log.Printf("unique ID for the client pair: %s\n", clientPairId) if len(clientPairId) == 0 { log.Fatal("Must config the pair") } firstConn, err := net.Dial("tcp", meetAddr) if err != nil { log.Fatal(err) } fmt.Fprintf(firstConn, "GET /meet/"+clientPairId+" HTTP/1.0\r\n\r\n") firstConn.Close() // Block to let the OS free the port. time.Sleep(5 * time.Second) log.Println("firstConn.LocalAddr", firstConn.LocalAddr().String()) ln, err := net.Listen("tcp", firstConn.LocalAddr().String()) if err != nil { log.Println("Could not open listening socket! " + err.Error()) return } log.Println("Listening on address " + firstConn.LocalAddr().String()) for { conn, err := ln.Accept() if err != nil { fmt.Println(err.Error()) continue } go handleConnection(conn) } }
func main() { log.SetFlags(log.Flags() | log.Lshortfile) meetAddrFmt := "http://%s/meet/" meetAddr := "localhost:8080" var clientPairId string flag.StringVar(&meetAddr, "meetAddr", meetAddr, "Server for discover other") flag.StringVar(&clientPairId, "pair", "", "key to macth service") flag.Parse() meetAddr = fmt.Sprintf(meetAddrFmt, meetAddr) log.Printf("Discover server is %s\n", meetAddr) log.Printf("unique ID for the client pair: %s\n", clientPairId) if len(clientPairId) == 0 { log.Fatal("Must config the pair") } response, err := http.Get(meetAddr + clientPairId) if err != nil { log.Fatal(err) } address, err := ioutil.ReadAll(response.Body) if err != nil { log.Fatal(err) } log.Printf("Dial %s\n", string(address)) conn, err := net.Dial("tcp", string(address)) if err != nil { log.Fatal(err) } go readSocket(conn) writeSocket(conn) }
func main() { flag.StringVar(&cfgPath, "config", "config.ini", "config file path, default is config.ini and custom.ini") flag.Parse() if len(cfgPath) <= 0 { cfgPath = "config.ini" customPath = "custom.ini" } else { f, _ := filepath.Abs(cfgPath) customPath = filepath.Join(filepath.Dir(f), "custom.ini") } var err error cfg, err = goconfig.LoadConfigFile(cfgPath, customPath) if err != nil { fmt.Println(err) return } log.Info("Loaded config files:", cfgPath, customPath) port, _ := cfg.Int("server", "port") db, err := leveldb.OpenFile("./authperm.db", nil) if err != nil { fmt.Println(err) return } var auth = &ldbauth.LDBAuth{db} var perm server.Perm if cfg.MustValue("perm", "type") == "leveldb" { perm = ldbperm.NewLDBPerm(db, "root", "root", os.ModePerm) } else { perm = server.NewSimplePerm("root", "root") } typ, _ := cfg.GetValue("driver", "type") var factory server.DriverFactory if typ == "file" { rootPath, _ := cfg.GetValue("file", "rootpath") _, err = os.Lstat(rootPath) if os.IsNotExist(err) { os.MkdirAll(rootPath, os.ModePerm) } else if err != nil { fmt.Println(err) return } factory = &filedriver.FileDriverFactory{ rootPath, perm, } } else if typ == "qiniu" { accessKey, _ := cfg.GetValue("qiniu", "accessKey") secretKey, _ := cfg.GetValue("qiniu", "secretKey") bucket, _ := cfg.GetValue("qiniu", "bucket") factory = qiniudriver.NewQiniuDriverFactory(accessKey, secretKey, bucket) } else { log.Fatal("no driver type input") } // start web manage UI useweb, _ := cfg.Bool("web", "enable") if useweb { web.DB = auth web.Perm = perm web.Factory = factory weblisten, _ := cfg.GetValue("web", "listen") admin, _ := cfg.GetValue("admin", "user") pass, _ := cfg.GetValue("admin", "pass") tls, _ := cfg.Bool("web", "tls") certFile, _ := cfg.GetValue("web", "certFile") keyFile, _ := cfg.GetValue("web", "keyFile") go web.Web(weblisten, "static", "templates", admin, pass, tls, certFile, keyFile) } ftpName, _ := cfg.GetValue("server", "name") opt := &server.ServerOpts{ Name: ftpName, Factory: factory, Port: port, Auth: auth, } // start ftp server ftpServer := server.NewServer(opt) log.Info("FTP Server", version) err = ftpServer.ListenAndServe() if err != nil { log.Fatal("Error starting server:", err) } }