Beispiel #1
0
func (jar *Jar) SpillCookies() {
	for i, v := range jar.cookies {
		for j, w := range v {
			// fmt.Printf ( "Path(%s) [%d]: %v\n", i, j, w )
			if db1 {
				fmt.Printf("Path(%s) [%d]: %s\n", i, j, godebug.SVar(w))
			}
		}
	}
}
Beispiel #2
0
func (h *socketHandler) onPacket(decoder *decoder, packet *packet) ([]interface{}, error) {
	if db1 {
		fmt.Printf("At:%s\n", godebug.LF())
	}
	var message string
	switch packet.Type {
	case _CONNECT:
		message = "connection"
	case _DISCONNECT:
		message = "disconnect"
	case _ERROR:
		message = "error"
	case _ACK:
	case _BINARY_ACK:
		return nil, h.onAck(packet.Id, decoder, packet)
	default:
		message = decoder.Message()
	}
	if db1 {
		fmt.Printf("At:%s\n", godebug.LF())
	}
	h.PrintEventsRespondedTo()
	if DbLogMessage {
		fmt.Printf("Message [%s] ", message)
		if db1 {
			fmt.Printf("%s\n", godebug.LF())
		}
	}

	/*
		// xyzzy - allEvents
		for _, c2 := range h.allEvents {
			args := c2.GetArgs() // returns Array of interface{}
			olen := len(args)
		}
	*/

	h.lock.RLock()
	c, ok := h.events[message]
	xc, ok1 := h.x_events[message]
	h.lock.RUnlock()

	if !ok && !ok1 {
		if db1 {
			fmt.Printf("Did not have a handler for %s At:%s\n", message, godebug.LF())
		}
		// If the message is not recognized by the server, the decoder.currentCloser
		// needs to be closed otherwise the server will be stuck until the e xyzzy
		fmt.Printf("Error: %s was not found in h.events\n", message)
		decoder.Close()
		return nil, nil
	}

	_ = xc
	/* New -----------------------------------------------------------------------------------------------------------------
	if ok1 {
		// type EventHandlerFunc func(so *Socket, message string, args [][]byte) error
		err, xargs, nargs := decoder.DecodeDataX(packet)
		if err != nil {
			fmt.Printf("Unable to decode packet, %s, %s\n", err, godebug.LF())
			return nil, err
		}
		err := xc(xyzzy, message, xargs, nargs)
		if err != nil {
			fmt.Printf("Handler reported an error: %s, message=%s\n", err, message, godebug.LF())
			return nil, err
		}
		return nil, nil

	}
	*/

	args := c.GetArgs() // returns Array of interface{}
	if db1 {
		fmt.Printf("len(args) = %d At:%s\n", len(args), godebug.LF())
	}
	olen := len(args)
	if db1 {
		fmt.Printf("args = %v, %s\n", args, godebug.LF())
	}
	if olen > 0 {
		packet.Data = &args
		if err := decoder.DecodeData(packet); err != nil {
			if db1 {
				fmt.Printf("At:%s, err=%s, an error at this point means that your handler did not get called\n", godebug.LF(), err)
			}
			fmt.Printf("Try a `map[string]interface{}` for a parameter type, %s\n", godebug.LF())
			return nil, err
		}
	}

	// Padd out args to olen
	for i := len(args); i < olen; i++ {
		args = append(args, nil)
	}

	if DbLogMessage {
		if db1 {
			fmt.Printf("\tArgs = %s, %s\n", godebug.SVar(args), godebug.LF())
		} else {
			fmt.Printf("Args = %s\n", godebug.SVar(args))
		}
	}
	if LogMessage {
		logrus.Infof("Message [%s] Auruments %s", message, godebug.SVar(args))
	}

	// ------------------------------------------------------ call ---------------------------------------------------------------------------------------
	retV := c.Call(h.socket, args)
	if len(retV) == 0 {
		if db1 {
			fmt.Printf("At:%s\n", godebug.LF())
		}
		return nil, nil
	}

	var err error
	if last, ok := retV[len(retV)-1].Interface().(error); ok {
		err = last
		retV = retV[0 : len(retV)-1]
	}
	ret := make([]interface{}, len(retV))
	for i, v := range retV {
		ret[i] = v.Interface()
	}
	if db1 {
		fmt.Printf("At:%s\n", godebug.LF())
	}
	if DbLogMessage {
		if err != nil {
			fmt.Printf("Response/Error %s", err)
		} else {
			fmt.Printf("Response %s", godebug.SVar(ret))
		}
	}
	if LogMessage {
		if err != nil {
			logrus.Infof("Response/Error %s", err)
		} else {
			logrus.Infof("Response %s", godebug.SVar(ret))
		}
	}
	return ret, err
}
Beispiel #3
0
func main() {

	tmp1, err := flags.ParseArgs(&opts, os.Args)
	if err != nil {
		panic(err)
		os.Exit(1)
	}
	flist := tmp1[1:]

	// Get to the correct directory to run stuff from
	if opts.CdTo != "" {
		err := os.Chdir(opts.CdTo)
		if err != nil {
			fmt.Printf("Unable to change directories to %s, error: %s\n", opts.CdTo, err)
			os.Exit(1)
		}
	}

	var gCfg JSONCfg

	// Read in JSON config file if it exists
	if Exists(opts.Cfg) {
		fb, err := ioutil.ReadFile(opts.Cfg)
		if err != nil {
			fmt.Fprintf(os.Stderr, "Error reading config file %s, %s\n", opts.Cfg, err)
			os.Exit(1)
		}

		err = json.Unmarshal(fb, &gCfg)
		if err != nil {
			fmt.Fprintf(os.Stderr, "Error reading config file %s, %s - file did not parse\n", opts.Cfg, err)
			os.Exit(1)
		}

		fmt.Printf("Config file %s read in, watching %s\n", opts.Cfg, gCfg.FilesToWatch)

		flist = append(flist, gCfg.FilesToWatch...)
		if gCfg.CmdToRun != "" {
			opts.Cmd = gCfg.CmdToRun
		}
		if gCfg.CdTo != "" {
			opts.CdTo = gCfg.CdTo
		}
	}

	// setup signal handler to ignore some signals
	go handleSignals()

	// Delcare stuff --------------------------------------------------------------------------------------
	cli_buf := strings.Split(opts.Cmd, " ")

	// Do periodic Stuff ----------------------------------------------------------------------------------
	ticker := time.NewTicker(1 * time.Second)
	quit := make(chan struct{})
	go func() {
		for {
			// fmt.Printf("AT: %s\n", godebug.LF())
			select {
			case <-ticker.C:
				// do stuff
				if getClearRunCmd() {
					// setRunCmd(false)
					// fmt.Printf("AT: %s\n", godebug.LF())
					cmd := exec.Command(cli_buf[0], cli_buf[1:]...)
					// cmd.Stdin = strings.NewReader("some input")
					var out bytes.Buffer
					cmd.Stdout = &out
					err := cmd.Run()
					if err != nil {
						fmt.Printf("Run Errors: %s", err)
					}
					// fmt.Printf("AT: %s\n", godebug.LF())
					fmt.Printf("%s\n", out.String())
				}
			case <-quit:
				// fmt.Printf("AT: %s\n", godebug.LF())
				ticker.Stop()
				return
			}
		}
	}()

	// Watch file for changes -----------------------------------------------------------------------------
	watcher, err := fsnotify.NewWatcher()
	if err != nil {
		log.Fatal(err)
	}

	// fmt.Printf("AT: %s\n", godebug.LF())
	done := make(chan bool)

	// Process events
	go func() {
		for {
			// fmt.Printf("AT: %s\n", godebug.LF())
			select {
			case ev := <-watcher.Event:
				// fmt.Printf("AT: %s\n", godebug.LF())
				fmt.Printf("Event: %+v\n", ev) // log.Println("event:", ev)
				name := ev.Name
				isRen := ev.IsRename()
				fmt.Printf("Caught an event, %s\n", godebug.SVar(ev))

				setRunCmd(true)

				if isRen {
					err = watcher.Watch(name)
					if err != nil {
						fmt.Printf("Failed to set watch on %s, %s, -- will try again in 1/10 of second %s\n", name, err, godebug.LF())
						go func(watcher *fsnotify.Watcher, name string) {
							time.Sleep(100 * time.Millisecond)
							err := watcher.Watch(name)
							if err != nil {
								fmt.Printf("Failed to set watch on %s, %s, -- 2nd try%s\n", name, err, godebug.LF())
							} else {
								fmt.Printf("Success on 2nd try - watch on %s, %s\n", name, godebug.LF())
							}
						}(watcher, name)
					}

				}
			case err := <-watcher.Error:
				// fmt.Printf("AT: %s\n", godebug.LF())
				log.Println("error:", err)
			}
		}
		// fmt.Printf("AT: %s\n", godebug.LF())
	}()

	fmt.Printf("***************************************\n")
	fmt.Printf("* watching %s \n", flist)
	fmt.Printf("***************************************\n")
	for _, fn := range flist {
		// fmt.Printf("AT: %s\n", godebug.LF())
		if DirExists(fn) {
			var fns []string
			if !optsRecursive {
				fns, _ = GetFilenames(fn)
			} else {
				fns, _, _ = GetFilenamesRecrusive(fn)
			}
			for _, fn0 := range fns {
				err = watcher.Watch(fn0)
				if err != nil {
					fmt.Printf("Failed to set watch on %s, %s, %s\n", fn0, err, godebug.LF())
				}
			}
		} else {
			// fmt.Printf("AT: %s\n", godebug.LF())
			err = watcher.Watch(fn)
			if err != nil {
				fmt.Printf("Failed to set watch on %s, %s, %s\n", fn, err, godebug.LF())
			}
		}
		// fmt.Printf("AT: %s\n", godebug.LF())
	}
	// fmt.Printf("AT: %s\n", godebug.LF())

	<-done

	// fmt.Printf("AT: %s\n", godebug.LF())
	/* ... do stuff ... */
	watcher.Close()
}