Best Gauge code snippet using parser.acceptor
syslog.go
Source:syslog.go
...23 protocol string24 listen string25 application string26 lineChannel chan<- string27 acceptor Acceptor28 parser *syslogParser29}30//CreateSyslogInputReader created BufferedReader31//dsn examples::32// syslog:udp:binding_ip:binding_port/application33// syslog:tcp:binding_ip:binding_port/application34// syslog:tcp:binding_ip:binding_port/application35func CreateSyslogInputReader(dsn string) (r *SyslogInputReader, err error) {36 r = &SyslogInputReader{}37 r.parser, err = newSyslogParser()38 check(err)39 //read dsn40 err = r.parseDsn(dsn)41 if err != nil {42 return43 }44 //create udp or tcp server45 err = r.startServer()46 return47}48func (r *SyslogInputReader) parseDsn(dsn string) (err error) {49 r.protocol, r.listen, r.application, err = parseSyslogDsn(dsn)50 return51}52//ReadToChannel implements BufferedReader ReadToBuffer method for SyslogInputReader53func (r *SyslogInputReader) ReadToChannel(lineChannel chan<- string) {54 r.lineChannel = lineChannel55 if r.protocol == "udp" {56 r.readToBufferUDP()57 } else {58 r.readToBufferTCP()59 }60}61//Close implements BufferedReader Close method for SyslogInputReader62func (r *SyslogInputReader) Close() {63 r.acceptor.Close()64}65func (r *SyslogInputReader) readToBufferTCP() {66 // accept new connect and send it to handler67 for {68 conn, err := r.acceptor.Accept()69 check(err)70 go r.handleConnectionTCP(conn)71 }72}73func (r *SyslogInputReader) readToBufferUDP() {74 //create one listener and handle messages in one loop75 conn, err := r.acceptor.Accept()76 check(err)77 r.handleConnectionUDP(conn)78}79func (r *SyslogInputReader) startServer() (err error) {80 var (81 acceptor Acceptor82 )83 if r.protocol == "udp" {84 acceptor, err = r.getUDPAcceptor()85 if err != nil {86 return err87 }88 } else {89 acceptor, err = r.getTCPAcceptor()90 if err != nil {91 return err92 }93 }94 if err != nil {95 return err96 }97 r.acceptor = acceptor98 return99}100func (r *SyslogInputReader) getUDPAcceptor() (acceptor Acceptor, err error) {101 addr, err := net.ResolveUDPAddr(r.protocol, r.listen)102 if err != nil {103 return acceptor, err104 }105 udpconn, err := net.ListenUDP(r.protocol, addr)106 acceptor = &udpAcceptor{acceptor: udpconn}107 return acceptor, err108}109func (r *SyslogInputReader) getTCPAcceptor() (acceptor Acceptor, err error) {110 addr, err := net.ResolveTCPAddr(r.protocol, r.listen)111 if err != nil {112 return acceptor, err113 }114 tcpl, err := net.ListenTCP(r.protocol, addr)115 acceptor = &tcpAcceptor{acceptor: tcpl}116 return acceptor, err117}118func (r *SyslogInputReader) handleConnectionUDP(conn net.Conn) {119 var (120 read int121 err error122 b []byte123 )124 b = make([]byte, udpSafePackSize)125 defer conn.Close()126 for {127 read, err = conn.Read(b)128 if err != nil {129 log.Println(err)130 }131 bytesBuf := b[0:read]132 r.m.Lock()133 if r.appendToBuffer(bytesBuf) {134 r.lineChannel <- string('\n')135 }136 r.m.Unlock()137 //log.Println(string(r.buffer))138 }139}140func (r *SyslogInputReader) handleConnectionTCP(conn net.Conn) {141 buffer := bufio.NewReader(conn)142 defer conn.Close()143 for {144 bytesBuf, err := buffer.ReadBytes('\n')145 if err == io.EOF {146 r.m.Lock()147 if r.appendToBuffer(bytesBuf) {148 r.lineChannel <- string('\n')149 }150 r.m.Unlock()151 break152 } else if err != nil {153 check(err)154 }155 r.m.Lock()156 r.appendToBuffer(bytesBuf)157 r.m.Unlock()158 }159 //log.Println(string(r.buffer))160}161func (r *SyslogInputReader) appendToBuffer(byteBuf []byte) bool {162 if len(byteBuf) == 0 {163 return false164 }165 //parse message.166 m, err := r.parser.parseSyslogMsg(string(byteBuf))167 if err == ErrorUnknownInputStringFormat {168 log.Println(ErrorUnknownInputStringFormat, string(byteBuf))169 }170 //Filter by application171 if m.Application != r.application {172 return false173 }174 r.lineChannel <- string(m.Message)175 return true176}177func parseSyslogDsn(dsn string) (protocol string, listen string, application string, err error) {178 r, err := re.Compile(`(syslog):([a-zA-Z0-9]+):([^/]+)/(\S+)`)179 if err != nil {180 return181 }182 matches := r.FindStringSubmatch(dsn)183 if len(matches) != 5 {184 err = ErrorIncorrectDSN185 return186 }187 protocol = matches[2]188 listen = matches[3]189 application = matches[4]190 if protocol != "udp" && protocol != "tcp" {191 err = ErrorUnknownProtocol192 }193 return194}195//Acceptor is an interface than Can Accept new connection and close it196type Acceptor interface {197 Accept() (net.Conn, error)198 Close() error199}200type tcpAcceptor struct {201 acceptor *net.TCPListener202}203//Accept accepts new tcp connection204func (l *tcpAcceptor) Accept() (net.Conn, error) {205 return l.acceptor.Accept()206}207//Close closes tcp connection208func (l *tcpAcceptor) Close() error {209 return l.acceptor.Close()210}211type udpAcceptor struct {212 acceptor *net.UDPConn213}214//Accept accepts new udp packet215func (l *udpAcceptor) Accept() (net.Conn, error) {216 return l.acceptor, nil217}218//Close closes udp file descriptor219func (l *udpAcceptor) Close() error {220 return l.acceptor.Close()221}...
parser.go
Source:parser.go
...117 }118 return119}120// testMysqlSyntax tests the mysql syntax with unit test.121func (p *Parser) testMysqlSyntax(prefix string, acceptor Acceptor, sql string) (v interface{}, err error) {122 defer func() {123 p := recover()124 if p != nil {125 switch e := p.(type) {126 case error:127 err = e128 default:129 err = fmt.Errorf("%+v", p)130 }131 }132 }()133 p.prefix = prefix134 inputStream := antlr.NewInputStream(sql)135 caseChangingStream := newCaseChangingStream(inputStream, true)136 lexer := gen.NewMySqlLexer(caseChangingStream)137 lexer.RemoveErrorListeners()138 tokens := antlr.NewCommonTokenStream(lexer, antlr.LexerDefaultTokenChannel)139 mysqlParser := gen.NewMySqlParser(tokens)140 mysqlParser.RemoveErrorListeners()141 mysqlParser.AddErrorListener(p)142 visitor := &visitor{143 prefix: prefix,144 debug: p.debug,145 logger: p.logger,146 }147 v = acceptor(mysqlParser, visitor)148 return149}...
acceptor.go
Source:acceptor.go
1package network2import (3 "errors"4 "fmt"5 "net"6)7type Acceptor struct {8 IP string9 Port int10 Listener net.Listener11 MsgParser *MsgParser12 MsgProcessor MsgProcessor13 // Output interface14 NewSession func(sess *SocketSession) Session15}16func NewAcceptor(addr string, port int, p *MsgParser, proc MsgProcessor) (*Acceptor, error) {17 if p == nil || proc == nil {18 return nil, errors.New("nil pointer")19 }20 return &Acceptor{21 IP: addr,22 Port: port,23 MsgParser: p,24 MsgProcessor: proc,25 }, nil26}27func (self *Acceptor) Start() error {28 // Socket Listen29 listen, err := net.ListenTCP("tcp", &net.TCPAddr{net.ParseIP(self.IP), self.Port, ""})30 if err != nil {31 fmt.Println("Listen failed", err.Error())32 return nil33 }34 self.Listener = listen35 go self.Accept()36 return nil37}38func (self *Acceptor) Accept() error {39 for {40 conn, err := self.Listener.Accept()41 if err != nil {42 fmt.Printf("Accepter Accept() error: %v \n ", err)43 return err44 }45 go self.OnAccept(conn)46 }47}48func (self *Acceptor) OnAccept(conn net.Conn) error {49 defer conn.Close()50 socketSession, err := NewSocketSession(conn, self.MsgParser, self.MsgProcessor)51 if err != nil {52 return err53 }54 if self.NewSession == nil {55 return errors.New("Acceptor NewSession function is nil")56 }57 // new user session, who impl Session interface58 sess := self.NewSession(socketSession)59 // recv messages, dispatch handler60 sess.Run()61 return nil62}...
acceptor
Using AI Code Generation
1import (2func main() {3 p.Init()4 p.Accept("1")5 p.Accept("2")6 p.Accept("3")7 p.Accept("4")8 fmt.Println(p.Get())9}
acceptor
Using AI Code Generation
1import (2func main() {3 scanner := scanner.NewScanner("input.txt")4 scanner.Scan()5 parser := parser.NewParser(scanner.Tokens)6 parser.Parse()7 fmt.Println("The parse tree is: ")8 parser.PrintParseTree()9}
acceptor
Using AI Code Generation
1import (2func main() {3 fmt.Printf("Enter a string to parse: ")4 fmt.Scanf("%s", &input)5 s.Init(input)6 t, err = p.Parse(s)7 if err != nil {8 fmt.Printf("Error: %s\n", err)9 } else {10 fmt.Printf("Parse Tree:\n%s\n", t)11 }12}
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!