2014-01-27 52 views
1
//package cluster 
package main 

import (
"encoding/csv" 
"encoding/json" 
"fmt" 
"io" 
"math/rand" 
"net" 
"os" 
"strconv" 
"time" 
//"bytes" 
) 

const (
BROADCAST = -1 
) 

var outbox, inbox chan *Envelope 
var pids [10]int 
var ips [10]string 

type Envelope struct { 
Pid int 
MsgId int64 
Msg interface{} 
} 

type Server interface { 
Pid() int 
Peers() []int 
Outbox() chan *Envelope 
Inbox() chan *Envelope 
} 

/* 
func (envelope Envelope) Pid() int { 
return envelope.MsgId 
} 
*/ 
var server Envelope 

func (envelope Envelope) Peers() []int { 
return nil //pids 
} 

func (envelope Envelope) Outbox() chan *Envelope { 
return outbox 
} 

func (envelope Envelope) Inbox() chan *Envelope { 
return inbox 
} 

func server_() { 
// listen on a port 
ln, err := net.Listen("tcp", ":9999") 
//time.Sleep(time.Second * 2) 
if err != nil { 
    fmt.Println(err) 
    return 
} 
for { 
    c, err := ln.Accept() 
    if err != nil { 
     fmt.Println(err) 
     continue 
    } else { 
     fmt.Println("handle") 
     go handleServerConnection(c) 
    } 
} 
} 

func handleServerConnection(c net.Conn) { 
// receive the message 
var msg Envelope 
var b []byte 
// for{ 
c.Read(b) 
/* 
    if b == nil{ 
     continue 
    }else{ 
     break 
    } 
*/ 
// } 

fmt.Println(b) 
err := json.Unmarshal(b, &msg) 
if err != nil { //ERORR !!!! 
    fmt.Println(err) 
} else { 
    fmt.Println("Received : %+v", msg) 
} 
c.Close() 
} 

func client() { 
// connect to the server 
for msg := range outbox { 
    c, err := net.Dial("tcp", "127.0.0.1:9999") 
    if err != nil { 
     fmt.Println(err) 
     return 
    } 

    if err != nil { 
     fmt.Println("error encoding the response to a join request") 
     fmt.Println(err) 
    } 
    b, _ := json.Marshal(msg) 
    fmt.Printf("the json: %s\n", b) 
    c.Write(b) 
    c.Close() 
} 
time.Sleep(time.Second * 2) 
} 

func New(myPid int, ConFile string) Envelope { 
inbox = make(chan *Envelope, 100) 
outbox = make(chan *Envelope, 100) 
file, err := os.Open(ConFile) 
if err != nil { 
    fmt.Println("Error:", err) 
} 
defer file.Close() 
reader := csv.NewReader(file) 

i := 0 
j := 0 
for { 
    record, err := reader.Read() 
    if err == io.EOF { 
     break 
    } else if err != nil { 
     fmt.Println("Error:", err) 
    } 
    x, _ := strconv.Atoi(record[0]) 
    pids[i] = x 
    i++ 
    ips[j] = record[1] 
    j++ 
} 

fmt.Println("\n", ips) 
fmt.Println("\n", pids) 
MsgId := rand.Int63n(0x10000000) 
server = Envelope{Pid: myPid, MsgId: MsgId, Msg: "Hello World :)"} 

go server_() 
go client() 
var input string 
fmt.Println("\n\nPress enter ---\n\n") 
fmt.Scanln(&input) 
return server 
} 

func main() { 
//for i := 1; i <= 10; i++ { 
server := New(12, "config.txt") 

server.Outbox() <- &Envelope{Pid: BROADCAST, MsgId: server.MsgId, Msg: "hello there"} 
select { 
case envelope := <-server.Inbox(): 
    fmt.Printf("Received msg from %d: '%s'\n", envelope.Pid, envelope.Msg) 

case <-time.After(2 * time.Second): 
    println("Waited and waited. Ab thak gaya\n") 
} 

//fmt.Println(server.Pid, server.MsgId, server.Msg) 
//} 

} 

從客戶端功能,我想發送一些數據,但無法在handleServerConnection()來接收數據沒有得到使用JSON編碼正確的數據結構中去語言

我的計劃是寫在去語言 幫我出

我所提到的大部分書中和堆棧溢出給予

我得到中的handleConnection功能的空對象的實例

+4

您可以發佈一個簡短的自包含您的問題的例子嗎?最好在運動場上運行一個(play.golang.org)。你正在解碼的JSON在哪裏?你有什麼?你能指望什麼? –

+0

您正在捕獲和打印代碼中的大量錯誤。他們有沒有打印出來?如果是這樣,你可以將它們添加到你的問題?確保捕捉到c.Read()中的任何錯誤。我的猜測是那個電話會給你一個錯誤。 – rexposadas

回答

0

你能分享一個你試圖解碼的json的例子嗎?我懷疑這是一個註釋問題。由於JavaScript語法,大多數json都是小寫字母。

舉例來說,我懷疑你的JSON看起來是這樣的:

{"pid":"0", "msgId":"500", "msg":"this is the message"} 

如果是這樣的話,那麼你就需要註解你的結構是這樣的:

type Envelope struct { 
    Pid int   `json:"pid"` 
    MsgId int64  `json:"msgId"` 
    Msg interface{} `json:"msg"` 
} 

同樣,沒有一個例子,我只是猜測這是你的問題。

相關問題