Esempio n. 1
0
// This could find a happy home in a generalized worker package ...
// TODO
func (c *asyncConnHdl) worker(id int, name string, task workerTask, ctl workerCtl, fb chan workerStatus) {
	var signal interrupt_code
	var tstat *taskStatus

	// todo: add startup hook for worker

await_signal:
	log.Stdout(name, "_worker: await_signal.")
	signal = <-ctl

on_interrupt:
	//		log.Stdout(name, "_worker: on_interrupt: ", signal);
	switch signal {
	case stop:
		goto before_stop
	case pause:
		goto await_signal
	case start:
		goto work
	}

work:
	//		log.Stdout(name, "_worker: work!");
	select {
	case signal = <-ctl:
		goto on_interrupt
	default:
		is, stat := task(c, ctl) // todo is a task context type
		if stat == nil {
			log.Stderr("<BUG> nil stat from worker ", name)
		}
		if stat.code != ok {
			log.Stdout(name, "_worker: task error!")
			tstat = stat
			goto on_error
		} else if is != nil {
			signal = *is
			goto on_interrupt
		}
		goto work
	}

on_error:
	log.Stdout(name, "_worker: on_error!")
	// TODO: log it, send it, and go back to wait_start:
	log.Stderr(name, "_worker task raised error: ", tstat)
	fb <- workerStatus{id, faulted, tstat, &ctl}
	goto await_signal

before_stop:
	log.Stdout(name, "_worker: before_stop!")
	// TODO: add shutdown hook for worker

	log.Stdout(name, "_worker: STOPPED!")
}
Esempio n. 2
0
// Use regexp's to parse userstrings
func ParseUser(s string) (u *IRCUser) {
	u = new(IRCUser)
	log.Stdout("Got User", s)
	a := userExp.MatchStrings(s)
	log.Stdout("Parsed", len(a))
	if len(a) > 0 { // Got a nick
		u.Nick = a[1]
		u.User = a[2]
	}
	return u
}
Esempio n. 3
0
/**
 * GetUserTimeline gets the user's timeline (provided by screenName) using the credentials
 * saved in the Client provided.
 */
func GetUserTimeline(client *Client, screenName string) (statuses []Status, err os.Error) {
	log.Stdout("Get User's Timeline\n")
	// get json response from server
	var jsonStrResponse string
	var maxFetchCount = client.MaxFetchCount
	if maxFetchCount == 0 {
		maxFetchCount = DefaultMaxFetchCount
	} // use a default

	// send request
	jsonStrResponse, err = send(URLUserTimeline+"?screen_name="+screenName+"&count="+strconv.Itoa(maxFetchCount), "GET", nil, client, "")
	if err != nil {
		return nil, err
	}

	// parse json response
	var resultObj json.Json
	//log.Stdout("response: %s", jsonStrResponse);
	resultObj, err = parseJson(jsonStrResponse)
	if err != nil {
		return nil, err
	}

	// create status response
	var i int
	statuses = make([]Status, resultObj.Len())
	log.Stdoutf("result count: %d", resultObj.Len())
	for i = 0; i < len(statuses); i++ {
		var elem = resultObj.Elem(i)
		//log.Stdoutf("found status: %s\n", elem.Get("text").String());
		statuses[i] = *parseStatus(elem)
	}
	return
}
Esempio n. 4
0
/**
 * GetDirectMessages gets all of your direct messages. It uses the credentials
 * saved in the Client provided.
 */
func GetDirectMessages(client *Client) (statuses []Status, err os.Error) {
	log.Stdout("Get Direct Messages\n")
	// get json response from server
	var jsonStrResponse string
	var maxFetchCount = client.MaxFetchCount
	if maxFetchCount == 0 {
		maxFetchCount = DefaultMaxFetchCount
	} // use a default

	// send request
	jsonStrResponse, err = send(URLDirectMessages+"?count="+strconv.Itoa(maxFetchCount), "GET", nil, client, "")
	if err != nil {
		return nil, err
	}

	// parse json response
	var resultObj json.Json
	//log.Stdout("response: %s", jsonStrResponse);
	resultObj, err = parseJson(jsonStrResponse)
	if err != nil {
		return nil, err
	}

	// create status response
	var i int
	statuses = make([]Status, resultObj.Len())
	log.Stdoutf("result count: %d", resultObj.Len())
	for i = 0; i < len(statuses); i++ {
		statuses[i] = *parseDirectMessage(resultObj.Elem(i))
	}
	return
}
Esempio n. 5
0
/**
 * GetMentions gets all mentions of you using the credentials
 * saved in the Client provided.
 */
func GetMentions(client *Client) (statuses []Status, err os.Error) {
	log.Stdout("Get Mentions\n")
	// get json response from server
	var jsonStrResponse string
	var maxFetchCount = client.MaxFetchCount
	if maxFetchCount == 0 {
		maxFetchCount = DefaultMaxFetchCount
	} // use a default
	var form = map[string][]string{
		"count": []string{strconv.Itoa(maxFetchCount)},
	}

	// send request
	jsonStrResponse, err = send(URLMentions, "GET", form, client, "count="+strconv.Itoa(maxFetchCount))
	if err != nil {
		return nil, err
	}

	// parse json response
	var resultObj json.Json
	resultObj, err = parseJson(jsonStrResponse)
	if err != nil {
		return nil, err
	}

	// create status response
	var i int
	statuses = make([]Status, resultObj.Len())
	log.Stdoutf("result count: %d", resultObj.Len())
	for i = 0; i < len(statuses); i++ {
		var elem = resultObj.Elem(i)
		statuses[i] = *parseStatus(elem)
	}
	return
}
Esempio n. 6
0
func main() {
	addr, err := net.ResolveTCPAddr("127.0.0.1:4009")
	if err != nil {
		log.Exit("error:", err)
	}
	listener, err := net.ListenTCP("tcp", addr)
	if err != nil {
		log.Exit("error", err)
	}

	//1 channel for incoming connections, another for client communication
	connections := make(chan *net.TCPConn)
	clients := make(chan TClient)
	cMap := make(map[string]*net.TCPConn)
	fMap := make(map[string]string)

	go ListenConnections(listener, connections, clients)
	log.Stdout("Waiting for connections\n")
	for {
		select {
		case conn := <-connections:
			cMap[conn.RemoteAddr().String()] = conn
		case client := <-clients:
			if regexp.MustCompile("^have ").MatchString(client.msg) {
				fMap[string(client.msg[5:len(client.msg)])] = client.local
			}
			if regexp.MustCompile("^list").MatchString(client.msg) {
				for key, value := range fMap {
					cMap[client.forserver].Write(strings.Bytes(key + "->" + value))
				}
				cMap[client.forserver].Write(strings.Bytes("\n"))
			}
		}
	}
}
Esempio n. 7
0
// closes the connHdl's net.Conn connection.
// Is public so that connHdl struct can be used as SyncConnection (TODO: review that.)
//
func (hdl connHdl) Close() os.Error {
	err := hdl.conn.Close()
	if debug() {
		log.Stdout("[Go-Redis] Closed connection: ", hdl)
	}
	return err
}
Esempio n. 8
0
func ProcessConn(conn *net.TCPConn, client chan TClient) {
	log.Stdout("connected\n")

	//get client's ip and port
	conn.Write(strings.Bytes("hi"))
	go myReader(conn, client)
}
Esempio n. 9
0
func main() {
	var response *http.Response
	var err error

	api := notifo.New("gotest", "a25c4f206494150bddf2e716705c8bedcad0cb16")
	//api.SetEndpoint("http://localhost:8000/v1/");

	if response, err = api.SubscribeUser("devcamcar"); err != nil {
		log.Stderr("ERROR")
		log.Stderr(err)
	} else {
		dump, _ := httputil.DumpResponse(response, true)
		log.Stdout(response.StatusCode)
		log.Stdout(string(dump))
	}
}
Esempio n. 10
0
func handleMessage(s net.Conn, reqChannel chan MCRequest) (ret bool) {
	log.Stdoutf("Handling a message...")
	hdrBytes := make([]byte, HDR_LEN)
	ret = false

	log.Stdoutf("Reading header...")
	bytesRead, err := io.ReadFull(s, hdrBytes)
	if err != nil || bytesRead != HDR_LEN {
		log.Stderr("Error reading message: %s (%d bytes)", err, bytesRead)
		return
	}

	req := grokHeader(hdrBytes)

	readContents(s, req)

	log.Stdout("Processing message %s", req)
	req.ResponseChannel = make(chan MCResponse)
	reqChannel <- req
	res := <-req.ResponseChannel
	ret = !res.Fatal
	if ret {
		log.Stdoutf("Got response %s", res)
		transmitResponse(s, req, res)
	} else {
		log.Stderr("Something went wrong, hanging up...")
	}

	return
}
Esempio n. 11
0
func main() {
	// option setup
	opts.Description = "lightweight http blog server"
	// parse and handle options
	opts.Parse()

	confFile = path.Join(*blogroot, "config")
	templateDir = path.Join(*blogroot, "templates")
	postDir = path.Join(*blogroot, "posts")
	pageDir = path.Join(*blogroot, "pages")
	dataDir = path.Join(*blogroot, "data")

	config.ReadConfig(confFile)
	input.ReadTemplates(templateDir)
	input.ReadPosts(postDir)
	input.ReadPages(pageDir)
	input.ReadData(dataDir)
	makeTags()
	makeCategories()
	compile.CompileAll()
	serve.StartServers()
	log.Stdout("Server started in ",
		(time.Nanoseconds()-startTime)/1000,
		" microseconds")
	serve.Serve(*port)
}
Esempio n. 12
0
// Creates and opens a new connection to server per ConnectionSpec.
// The new connection is wrapped by a new connHdl with its bufio.Reader
// delegating to the net.Conn's reader.
//
func newConnHdl(spec *ConnectionSpec) (hdl *connHdl, err Error) {
	here := "newConnHdl"

	if hdl = new(connHdl); hdl == nil {
		return nil, NewError(SYSTEM_ERR, fmt.Sprintf("%s(): failed to allocate connHdl", here))
	}
	addr := fmt.Sprintf("%s:%d", spec.host, spec.port)
	raddr, e := net.ResolveTCPAddr(addr)
	if e != nil {
		msg := fmt.Sprintf("%s(): failed to resolve remote address %s", here, addr)
		return nil, NewErrorWithCause(SYSTEM_ERR, msg, e)
	}
	conn, e := net.DialTCP(TCP, nil, raddr)
	switch {
	case e != nil:
		err = NewErrorWithCause(SYSTEM_ERR, fmt.Sprintf("%s(): could not open connection", here), e)
	case conn == nil:
		err = NewError(SYSTEM_ERR, fmt.Sprintf("%s(): net.Dial returned nil, nil (?)", here))
	default:
		configureConn(conn, spec)
		hdl.spec = spec
		hdl.conn = conn
		bufsize := 4096
		hdl.reader, e = bufio.NewReaderSize(conn, bufsize)
		if e != nil {
			msg := fmt.Sprintf("%s(): bufio.NewReaderSize (%d) error", here, bufsize)
			err = NewErrorWithCause(SYSTEM_ERR, msg, e)
		} else {
			if err = hdl.onConnect(); err == nil && debug() {
				log.Stdout("[Go-Redis] Opened SynchConnection connection to ", addr)
			}
		}
	}
	return hdl, err
}
Esempio n. 13
0
func (v pageVisitor) VisitFile(path string, f *os.FileInfo) {
	// get a clean path
	relPath := strings.Replace(path, v.root, "", 1)
	log.Stdout("  Reading page ", relPath)
	// read in the posts
	Pages[relPath] = ReadPage(readFile(path), relPath)
}
Esempio n. 14
0
// Creates and opens a new connection to server per ConnectionSpec.
// The new connection is wrapped by a new connHdl with its bufio.Reader
// delegating to the net.Conn's reader. 
//
func newConnHdl (spec *ConnectionSpec) (hdl *connHdl, err os.Error) {
	here := "newConnHdl";

	if hdl = new(connHdl); hdl == nil { 
		return nil, withNewError (fmt.Sprintf("%s(): failed to allocate connHdl", here));
	}
	addr := fmt.Sprintf("%s:%d", spec.host, spec.port); 
	raddr, e:= net.ResolveTCPAddr(addr); 
	if e != nil {
		return nil, withNewError (fmt.Sprintf("%s(): failed to resolve remote address %s", here, addr));
	}	
	conn, e:= net.DialTCP(TCP, nil, raddr);
	switch {
		case e != nil:
			err = withOsError (fmt.Sprintf("%s(): could not open connection", here), e);
		case conn == nil:
			err = withNewError (fmt.Sprintf("%s(): net.Dial returned nil, nil (?)", here));
		default:
			configureConn(conn, spec);
			hdl.spec = spec;
			hdl.conn = conn;
			bufsize := 4096;
			hdl.reader, e = bufio.NewReaderSize(conn, bufsize);
			if e != nil {
				err = withNewError (fmt.Sprintf("%s(): bufio.NewReaderSize (%d) error", here, bufsize));
			}
			else {
				if debug() {log.Stdout("[Go-Redis] Opened SynchConnection connection to ", addr);}
			}
	}
	return hdl, err;
}
Esempio n. 15
0
func main() {
	var address, key string
	var count, delay, step int
	flag.StringVar(&address, "address", "127.0.0.1:6311", "Set the port (+optional address) to send packets to")
	flag.StringVar(&key, "key", "profile", "Set the key name (pakets data will be \"key:idx\")")
	flag.IntVar(&count, "count", 1000, "Set the number of packets to send")
	flag.IntVar(&delay, "delay", 1000000, "Set the delay between packets in nanoseconds (10^-9)")
	flag.IntVar(&step, "step", 100, "Log step (how many packets to send between logging)")
	flag.Parse()

	udp_address, error := net.ResolveUDPAddr(address)
	if error != nil {
		log.Exitf("Cannot parse \"%s\": %s", address, error)
	}

	log.Stdout(udp_address)

	ticker := time.NewTicker(int64(delay))
	defer ticker.Stop()

	for sent := 1; sent <= count; sent++ {
		<-ticker.C
		send(udp_address, key, sent)
		if sent%step == 0 {
			log.Stdoutf("Processed %d packets of %d", sent, count)
		}
	}
}
Esempio n. 16
0
func main() {
	// Dial freenode
	log.Stdout("Dialing server")
	server, err := irc.IRCDial("tcp", "", "irc.freenode.net:6667") // TODO: make this not hardcoded
	if err != nil {
		log.Exit("Dialing error:", err)
	}
	// Login to the server
	log.Stdout("Logging in to server")
	server.Login("goo_bot", irc.FlagInvisible)
	// Send a PM to NickServ to identify
	log.Stdout("Identifying to Nickserv")
	server.PrivMsg("NickServ", "identify go_bot turing")
	// Join a chat
	log.Stdout("Joining #bottest")
	bottest, _ := server.Join("#bottest") // TODO: log the errors
	// Send the chat a message
	log.Stdout("Greeting #bottest")
	bottest.Write("hi guys!")

	// We're done with the connection, close it
	log.Stdout("Sleeping before closing")
	time.Sleep(1e10)
	log.Stdout("Closing")
	server.Close()
}
Esempio n. 17
0
func main() {
	runtime.GOMAXPROCS(4)

	log.Stdout("Starting basic HTTP server...\n")

	tcp_listen_addr := &net.TCPAddr{net.IPv4(0, 0, 0, 0), 0}
	listener, listenErr := net.ListenTCP("tcp", tcp_listen_addr)

	if listenErr != nil {
		log.Stderr(listenErr.String())
	}
	log.Stdoutf("Listening on port: %s\n", listener.Addr().String())

	http.HandleFunc("/hello", handler)
	http.Serve(listener, nil)
	listener.Close()
	log.Stdout("Done.")
}
Esempio n. 18
0
File: web.go Progetto: tjweir/web.go
func routeHandler(req *Request) *Response {
	log.Stdout(req.RawURL)
	path := req.URL.Path

	ctx := Context{req, newResponse(200, "")}

	for cr, route := range routes {
		if !cr.MatchString(path) {
			continue
		}
		match := cr.MatchStrings(path)
		if len(match) > 0 {
			if len(match[0]) != len(path) {
				continue
			}
			if req.Method != route.method {
				continue
			}
			ai := 0
			handlerType := route.handler.Type().(*reflect.FuncType)
			expectedIn := handlerType.NumIn()
			args := make([]reflect.Value, expectedIn)

			if expectedIn > 0 {
				a0 := handlerType.In(0)
				ptyp, ok := a0.(*reflect.PtrType)
				if ok {
					typ := ptyp.Elem()
					if typ == contextType {
						args[ai] = reflect.NewValue(&ctx)
						ai += 1
						expectedIn -= 1
					}
				}
			}

			actualIn := len(match) - 1
			if expectedIn != actualIn {
				log.Stderrf("Incorrect number of arguments for %s\n", path)
				return newResponse(500, "")
			}

			for _, arg := range match[1:] {
				args[ai] = reflect.NewValue(arg)
			}
			ret := route.handler.Call(args)[0].(*reflect.StringValue).Get()
			var buf bytes.Buffer
			buf.WriteString(ret)
			resp := ctx.Response
			resp.Body = &buf
			return resp
		}
	}

	return newResponse(404, "")
}
Esempio n. 19
0
func CompileAll() {
	log.Stdout("Compiling all")
	CompilePosts()
	CompileExcerpts()
	CompileTags()
	CompileCategories()
	CompileIndex()
	Compile404()
	CompileFull()
}
Esempio n. 20
0
func ReadTemplate(templateDir, name string) *template.Template {
	log.Stdout("  Reading template ", name)
	templatePath := path.Join(templateDir, name)
	templateText := readFile(templatePath)
	template, err := template.Parse(templateText, nil)
	if err != nil {
		panic(err)
	}
	return template
}
Esempio n. 21
0
func myReader(conn *net.TCPConn, client chan TClient) {
	var rcvStr string
	var localAddr string
	for {
		rcvStr = ""
		read := true
		for read {
			rcvd := make([]byte, 1)
			size, err := conn.Read(rcvd)
			switch err {
			case os.EOF:
				//log.Stdout("Warning: End of data reached: ", err);
				read = false
			case nil:
				if util.Streq(string(rcvd[0:1]), "\n") {
					read = false
				} else {
					rcvStr = rcvStr + string(rcvd[0:size])
				}
			default:
				log.Stdout("Error: Reading data: ", err)
				read = false
			}
			if util.Streq(string(rcvd[0:1]), "\n") {
				read = false
			}
		}
		if regexp.MustCompile("^I'm").MatchString(rcvStr) {
			ladr, _ := net.ResolveTCPAddr(strings.Split(rcvStr, " ", 2)[1])
			localAddr = ladr.String()
			log.Stdout(localAddr)
			client <- TClient{localAddr, conn.RemoteAddr().String(), "new"}
		} else {
			if len(rcvStr) > 0 {
				client <- TClient{localAddr, conn.RemoteAddr().String(), rcvStr}
				log.Stdout("Data sent by client: " + rcvStr)
			}
			time.Sleep(5 * 1000 * 1000)
		}
	}
}
Esempio n. 22
0
func main() {
	flag.Parse()

	http.Handle("/", http.HandlerFunc(WriteResponse))
	//http.Handle("/", http.HandlerFunc(TestCode))
	//http.Handle("/", http.HandlerFunc(TestCode2))
	log.Stdout("start server")
	err := http.ListenAndServe(":8080", nil)
	if err != nil {
		log.Exit("ListenAndServe: ", err.String())
	}
}
Esempio n. 23
0
func CompileExcerpts() {
	log.Stdout("  Compiling post excerpts")
	// compile against the "excerpt" template
	tmpl := Templates["excerpt"]
	for _, post := range Posts {
		// extract excerpt
		post.Excerpt = strings.Split(post.Content, "<!--more-->", 2)[0]
		w := &stringWriter{}
		tmpl.Execute(post, w)
		post.CompiledExcerpt = w.buff
	}
}
Esempio n. 24
0
func ListenConnections(listener *net.TCPListener, connections chan *net.TCPConn, clients chan TClient) {
	for {
		conn, err := listener.AcceptTCP()
		if err != nil {
			log.Stdout("error in Accept():", err)
		} else {
			conn.SetKeepAlive(true)
			conn.SetReadTimeout(5 * 1000 * 1000 * 1000)
			go ProcessConn(conn, clients)
			connections <- conn
		}
	}
}
Esempio n. 25
0
func TestCode2(c *http.Conn, req *http.Request) {
	dump, err := http.DumpRequest(req, true)
	if err != nil {
		io.WriteString(c, err.String())
		return
	}
	_, err = c.Write(dump)
	if err != nil {
		io.WriteString(c, err.String())
		return
	}
	log.Stdout(string(dump))
}
Esempio n. 26
0
func CompileTags() {
	log.Stdout("  Compiling tags")
	// compile against the "tag" template
	tmpl := Templates["tab"]
	for _, tag := range Tags {
		w := &stringWriter{}
		tmpl.Execute(tag, w)
		Pages["/tag/"+tag.Name] = &Page{
			URL:     "/tag/" + tag.Name,
			Content: w.buff,
		}
	}
}
Esempio n. 27
0
func CompileCategories() {
	log.Stdout("  Compiling categories")
	// compile against the "tag" template
	tmpl := Templates["category"]
	for _, cat := range Categories {
		w := &stringWriter{}
		tmpl.Execute(cat, w)
		Pages["/category/"+cat.Name] = &Page{
			URL:     "/category/" + cat.Name,
			Content: w.buff,
		}
	}
}
Esempio n. 28
0
func CompileIndex() {
	log.Stdout("  Compiling index page")
	// compile list of all pages against the "index" template
	tmpl := Templates["index"]
	w := &stringWriter{}
	tmpl.Execute(map[string]interface{}{
		"Posts": Posts,
	},
		w)
	Pages["/"] = &Page{
		URL:     "/",
		Content: w.buff,
	}
}
Esempio n. 29
0
func Compile404() {
	log.Stdout("  Compiling 404 page")
	if tmpl, ok := Templates["404"]; ok {
		w := &stringWriter{}
		tmpl.Execute(map[string]interface{}{
			"Config": config.Configuration,
		},
			w)
		Pages["/404"] = &Page{
			URL:     "/-", // prevent 404 page from ever being served as a valid page
			Content: w.buff,
		}
	}
}
Esempio n. 30
0
func ReadTemplates(templateDir string) {
	Templates = make(map[string]*template.Template)
	// read the templates
	log.Stdout("Reading templates")
	flist, err := ioutil.ReadDir(templateDir)
	if err != nil {
		fmt.Fprintf(os.Stderr, "%s\n", err.String())
		panic("Couldn't read template directory!")
	}
	for _, finfo := range flist {
		fname := strings.Replace(finfo.Name, ".html", "", -1)
		Templates[fname] = ReadTemplate(templateDir, fname+".html")
	}
}