func eachReport(l *list.List) <-chan Report { ch := make(chan Report, l.Len()) for val := range l.Iter() { if name, ok := val.(Report); !ok { panic("typecast error") } else { ch <- name } } close(ch) return ch }
// handlingINOUT(): handle inputs from client, and send it to all other client via channels. func handlingINOUT(IN <-chan string, lst *list.List) { for { Log("handlingINOUT(): wait for input") input := <-IN // input, get from client // send to all client back Log("handlingINOUT(): handling input: ", input) for value := range lst.Iter() { client := value.(ClientChat) Log("handlingINOUT(): send to client: ", client.Name) client.IN <- input } } }
// Dump the DHT // First line of response is number of items // It is important that the DHT is delivered in one go as // otherwise there will be sync issues. Imagine you dump a bit now, then delete // a message what was part of that dump and then dump some more, the delete would // be lost. // Will this work for 1,000,000 messages in the DHT? func (myDHTServer *DHTServerStruct) dumpDHT(con *net.TCPConn) { // Query DHT sql := fmt.Sprintf("SELECT id,sha1,mailbox,size,orignodeid FROM DHT order by id") G_dhtDBLock.Lock() stmt, serr := myDHTServer.dht.Prepare(sql) defer stmt.Finalize() defer G_dhtDBLock.Unlock() if serr == nil { var id int var sha1 string var mailbox string var size int var orignodeid int var r list.List r.Init() stmt.Exec() rowcount := 0 for stmt.Next() { err := stmt.Scan(&id, &sha1, &mailbox, &size, &orignodeid) if err != nil { myDHTServer.logger.Logf(LMIN, "Unexpected error using DB: %s", err) break } reply := fmt.Sprintf("%d,%s,%s,%d,%d\r\n", id, sha1, mailbox, size, orignodeid) r.PushBack(reply) rowcount++ } con.Write([]byte(fmt.Sprintf("%d\r\n", rowcount))) for c := range r.Iter() { con.Write([]byte(c.(string))) } } else { myDHTServer.logger.Logf(LMIN, "Unexpected error using DB (%s): %s", sql, serr) } }
// Send the New Message Log to a remote node // First line of response is number of items func (myDHTServer *DHTServerStruct) sendNewMessageLog(con *net.TCPConn, hid string) { // Query newMessageLog sql := fmt.Sprintf("SELECT id, sha1, mailbox, size FROM newMessageLog where id > %s order by id", hid) G_nmlDBLock.Lock() stmt, serr := myDHTServer.nml.Prepare(sql) defer stmt.Finalize() defer G_nmlDBLock.Unlock() if serr == nil { var id int var sha1 string var mailbox string var size int var r list.List r.Init() stmt.Exec() rowcount := 0 for stmt.Next() { err := stmt.Scan(&id, &sha1, &mailbox, &size) if err != nil { myDHTServer.logger.Logf(LMIN, "Unexpected error using DB: %s", err) break } reply := fmt.Sprintf("%d,%s,%s,%d,%s\r\n", id, sha1, mailbox, size, G_nodeID) r.PushBack(reply) rowcount++ } con.Write([]byte(fmt.Sprintf("%d\r\n", rowcount))) for c := range r.Iter() { con.Write([]byte(c.(string))) } } else { myDHTServer.logger.Logf(LMIN, "Unexpected error using DB (%s): %s", sql, serr) } }
func main() { lst := new(list.List) for _ = range lst.Iter() { } }