Example #1
0
// Match all parsers, collecting their outputs into a vector.
// If one parser fails, the whole thing fails.
// NOTE: Consumes input on failure. Wrap calls in Try(...) to avoid.
func Collect(parsers ...Parser) Parser {
	return func(in Vessel) (Output, bool) {
		p := reflect.NewValue(parsers).(*reflect.SliceValue)

		matches := new(vector.Vector)
		for i := 0; i < p.Len(); i++ {
			parser := p.Elem(i).Interface().(Parser)
			match, ok := parser(in)
			if !ok {
				return nil, false
			}
			matches.Push(match)
		}

		return matches.Copy(), true
	}
}
Example #2
0
// A very simple chat server
func main() {
	buffer := new(vector.Vector)
	mutex := new(sync.Mutex)

	// create the socket.io server and mux it to /socket.io/
	config := socketio.DefaultConfig
	config.Origins = []string{"localhost:8080"}
	sio := socketio.NewSocketIO(&config)

	go func() {
		if err := sio.ListenAndServeFlashPolicy(":843"); err != nil {
			log.Println(err)
		}
	}()

	// when a client connects - send it the buffer and broadcasta an announcement
	sio.OnConnect(func(c *socketio.Conn) {
		mutex.Lock()
		c.Send(Buffer{buffer.Copy()})
		mutex.Unlock()
		sio.Broadcast(Announcement{"connected: " + c.String()})
	})

	// when a client disconnects - send an announcement
	sio.OnDisconnect(func(c *socketio.Conn) {
		sio.Broadcast(Announcement{"disconnected: " + c.String()})
	})

	// when a client send a message - broadcast and store it
	sio.OnMessage(func(c *socketio.Conn, msg socketio.Message) {
		payload := Message{[]string{c.String(), msg.Data()}}
		mutex.Lock()
		buffer.Push(payload)
		mutex.Unlock()
		sio.Broadcast(payload)
	})

	log.Println("Server starting. Tune your browser to http://localhost:8080/")

	mux := sio.ServeMux()
	mux.Handle("/", http.FileServer(http.Dir("www/")))

	if err := http.ListenAndServe(":8080", mux); err != nil {
		log.Fatal("ListenAndServe:", err)
	}
}
Example #3
0
// Match a parser 0 or more times.
func Many(match Parser) Parser {
	return func(in Vessel) (Output, bool) {
		matches := new(vector.Vector)
		for {
			out, parsed := match(in)
			if !parsed {
				break
			}

			if out != nil {
				matches.Push(out)
			}
		}

		return matches.Copy(), true
	}
}
Example #4
0
// turn string of children id's into a vector
func getCVec(s string) vector.Vector {
	var t string = ""
	v := new(vector.Vector)
	for i := 0; i < len(s); i++ {
		if string(s[i]) != "," {
			t += string(s[i])
		} else {
			if t != "" {
				r, _ := strconv.Atoi(t)
				v.Push(r)
				t = ""
			}
		}
	}
	if t != "" {
		r, _ := strconv.Atoi(t)
		v.Push(r)
	}
	return v.Copy()
}
Example #5
0
// returns a vector representing a dag, each element is of type Node (see "type Node struct..." below
func ParseFile(fName string) vector.Vector {

	// get file contents
	buf, err := ioutil.ReadFile(fName)
	if err != nil {
		fmt.Printf("Error: %s\n", err)
		os.Exit(1)
	}

	// some things
	var s string = ""
	var bIdx int = 0
	var nCount int = 0
	const space byte = ' '
	const null byte = '-'
	const nL byte = '\n'
	nTArray := new(vector.Vector)

	// get to node count
	for i := 0; i < len(buf); i++ {

		if buf[i] != space && buf[i] != nL {
			s += string(buf[i])
		} else {
			if checkNC(s) {
				s = ""
				bIdx = i
				break
			} else {
				s = ""
			}
		}
	}

	// get the node count
	for i := bIdx; i < len(buf); i++ {

		if buf[i] != space && buf[i] != nL {
			s += string(buf[i])
		} else {
			if s != "" {
				nCount, _ = strconv.Atoi(s)
				//fmt.Printf("node count: %d\n", nCount)
				s = ""
				bIdx = i
				break
			}
		}
	}

	// for each node, skip, get id int, childList vector, compCost int, skip
	for i := 0; i < nCount; i++ {
		nT := new(nodeTemp)
		for j := 0; j < 6; j++ {
			for k := bIdx; k < len(buf); k++ {
				if buf[k] != space && buf[k] != nL {
					s += string(buf[k])
				} else {
					if s != "" {
						switch j {
						case 0: // NODE - unused
						case 1:
							nT.id, _ = strconv.Atoi(s)
						case 2:
							nT.cl = getCVec(s)
						case 3:
							nT.ty = s
						case 4:
							nT.cc, _ = strconv.Atoi64(s)
						case 5: // PAR COST - unused
						}
						s = ""
						bIdx = k
						break
					}
				}
			}
		}
		nTArray.Push(nT)
	}

	// check for correctness
	//for i:=0; i<nCount; i++ {
	//	(nTArray.At(i).(*nodeTemp)).printNT()
	//}

	// vector to be returned, note that child lists are set up
	// in the following for loops (indexed by 'i')
	dagVec := new(vector.Vector)

	// push root
	for i := 0; i < nCount; i++ {
		temp := (nTArray.At(i).(*nodeTemp))
		if temp.ty == "ROOT" {
			n := new(Node)
			n.Id = 0
			n.Ty = "ROOT"
			n.Ex = 0
			n.Lev = -1
			for j := 0; j < (temp.cl).Len(); j++ {
				r := new(Rel)
				r.Id = ((temp.cl).At(j)).(int)
				r.Cc = 0
				(n.Cl).Push(r)
			}
			dagVec.Push(n)
			break
		}
	}

	// push computation nodes
	for i := 0; i < nTArray.Len(); i++ {
		temp := nTArray.At(i).(*nodeTemp)
		if temp.ty == "COMPUTATION" {
			n := new(Node)
			n.Id = temp.id
			n.Ty = temp.ty
			n.Ex = temp.cc
			n.Lev = -1
			for j := 0; j < (temp.cl).Len(); j++ {
				r := new(Rel)
				tId := ((temp.cl).At(j)).(int)
				for k := 0; k < nTArray.Len(); k++ {
					tNo := nTArray.At(k).(*nodeTemp)
					if tNo.id == tId {
						if tNo.ty == "TRANSFER" {
							r.Id = (tNo.cl).At(0).(int)
							r.Cc = tNo.cc
						} else if tNo.ty == "END" {
							r.Id = tNo.id
							r.Cc = 0
						}
						break
					}
				}
				(n.Cl).Push(r)
			}
			dagVec.Push(n)
		}
	}

	// push end node (no child list)
	for i := 0; i < nTArray.Len(); i++ {
		temp := nTArray.At(i).(*nodeTemp)
		if temp.ty == "END" {
			n := new(Node)
			n.Id = temp.id
			n.Ty = temp.ty
			n.Ex = 0
			n.Lev = -1
			dagVec.Push(n)
			break
		}
	}

	// set up parent lists
	for i := 0; i < dagVec.Len(); i++ {
		tId := (dagVec.At(i).(*Node)).Id
		for j := 0; j < dagVec.Len(); j++ {
			temp := dagVec.At(j).(*Node)
			for k := 0; k < (temp.Cl).Len(); k++ {
				relT := (temp.Cl).At(k).(*Rel)
				if relT.Id == tId {
					r := new(Rel)
					r.Id = temp.Id
					r.Cc = relT.Cc
					(dagVec.At(i).(*Node)).Pl.Push(r)
				}
			}
		}
	}

	// return dag vector
	return dagVec.Copy()
}
Example #6
0
func handlePreUpload(conn http.ResponseWriter, req *http.Request) {
	if !(req.Method == "POST" && req.URL.Path == "/camli/preupload") {
		httputil.BadRequestError(conn, "Inconfigured handler.")
		return
	}

	req.ParseForm()
	camliVersion := req.FormValue("camliversion")
	if camliVersion == "" {
		httputil.BadRequestError(conn, "No camliversion")
		return
	}
	n := 0
	haveVector := new(vector.Vector)

	haveChan := make(chan *map[string]interface{})
	for {
		key := fmt.Sprintf("blob%v", n+1)
		value := req.FormValue(key)
		if value == "" {
			break
		}
		ref := blobref.Parse(value)
		if ref == nil {
			httputil.BadRequestError(conn, "Bogus blobref for key "+key)
			return
		}
		if !ref.IsSupported() {
			httputil.BadRequestError(conn, "Unsupported or bogus blobref "+key)
		}
		n++

		// Parallel stat all the files...
		go func() {
			fi, err := os.Stat(BlobFileName(ref))
			if err == nil && fi.IsRegular() {
				info := make(map[string]interface{})
				info["blobRef"] = ref.String()
				info["size"] = fi.Size
				haveChan <- &info
			} else {
				haveChan <- nil
			}
		}()
	}

	if n > 0 {
		for have := range haveChan {
			if have != nil {
				haveVector.Push(have)
			}
			n--
			if n == 0 {
				break
			}
		}
	}

	ret := commonUploadResponse(req)
	ret["alreadyHave"] = haveVector.Copy()
	httputil.ReturnJson(conn, ret)
}
Example #7
0
// returns a topologically sorted vector of Node's
func TopSort(dag vec.Vector, s byte) vec.Vector {

	sortDag := new(vec.Vector)
	tempDag := new(vec.Vector)
	destDag := new(vec.Vector)
	setVec := new(vec.Vector)

	for i := 0; i < dag.Len(); i++ {
		tempDag.Push((dag.At(i).(*par.Node)).Copy())
		destDag.Push((dag.At(i).(*par.Node)).Copy())
	}
	// t-level gets regular top sort
	if s == 't' {
		setVec.Push(tempDag.At(0))
		destDag.Delete(0)
		for i := setVec.Len(); i > 0; i = setVec.Len() {
			n := (setVec.Pop().(*par.Node)).Copy()
			sortDag.Push(n)
			for j := 0; j < (n.Cl).Len(); j++ {
				c := ((n.Cl).At(j).(*par.Rel)).Id
				for k := 0; k < destDag.Len(); k++ {
					if (destDag.At(k).(*par.Node)).Id == c {
						for l := 0; l < (destDag.At(k).(*par.Node)).Pl.Len(); l++ {
							if (destDag.At(k).(*par.Node)).Pl.At(l).(*par.Rel).Id == n.Id {
								(destDag.At(k).(*par.Node)).Pl.Delete(l)
								break
							}
						}
					}
				}
			}
			for j := 0; j < destDag.Len(); j++ {
				if (destDag.At(j).(*par.Node)).Pl.Len() == 0 {
					c := (destDag.At(j).(*par.Node)).Id
					for k := 0; k < tempDag.Len(); k++ {
						if (tempDag.At(k).(*par.Node)).Id == c {
							setVec.Push(tempDag.At(k))
							break
						}
					}
					destDag.Delete(j)
					j--
				}
			}
		}
		// b-level gets reverse top sort
	} else if s == 'b' {
		setVec.Push(tempDag.At(tempDag.Len() - 1))
		destDag.Delete(destDag.Len() - 1)
		for i := setVec.Len(); i > 0; i = setVec.Len() {
			n := (setVec.Pop().(*par.Node)).Copy()
			sortDag.Push(n)
			for j := 0; j < (n.Pl).Len(); j++ {
				c := ((n.Pl).At(j).(*par.Rel)).Id
				for k := 0; k < destDag.Len(); k++ {
					if (destDag.At(k).(*par.Node)).Id == c {
						for l := 0; l < (destDag.At(k).(*par.Node)).Cl.Len(); l++ {
							if (destDag.At(k).(*par.Node)).Cl.At(l).(*par.Rel).Id == n.Id {
								(destDag.At(k).(*par.Node)).Cl.Delete(l)
								break
							}
						}
					}
				}
			}
			for j := 0; j < destDag.Len(); j++ {
				if (destDag.At(j).(*par.Node)).Cl.Len() == 0 {
					c := (destDag.At(j).(*par.Node)).Id
					for k := 0; k < tempDag.Len(); k++ {
						if (tempDag.At(k).(*par.Node)).Id == c {
							setVec.Push(tempDag.At(k))
							break
						}
					}
					destDag.Delete(j)
					j--
				}
			}
		}
	} else {
		fmt.Printf("Error")
	}

	return sortDag.Copy()

}