Ejemplo n.º 1
0
//	Returns string representation of the message.
func (msg *Message) ToString() string {
	if msg.Data == nil {
		msg.Data = make(map[string]string)
	}

	return fmt.Sprintf("%s%s%s%s%s", msg.Type, protocols.GetSep(), ParseDataToString(msg.Data), protocols.GetSep(), msg.Error)
}
Ejemplo n.º 2
0
//	Asks computer from network where to connect to.
//	Returns address of the computer to which client should connect.
func (c *Client) Connect() (string, error) {
	log.Printf("Connecting client %s using %s\n", c.Address, c.KnownIp)

	//	Setup connection
	log.Printf("Creating socket to %s\n", c.KnownIp)
	conn, err := net.Dial("tcp", c.KnownIp+":666")
	if err != nil {
		return "", err
	}

	//	Create request
	log.Println("Creating message")
	request := sip.Message{}
	request.Type = "REQ"
	request.AddData("ip", c.Address)
	request.AddData("capacity", string(c.Capacity))

	//	Send request
	err = sip.Request(conn, request)

	//	Check if sending was successful
	if err != nil {
		return "", err
	}

	//	Get response
	log.Println("Waiting for response")
	var respBytes = make([]byte, 1024)
	n, err := conn.Read(respBytes)

	//	Check if there was error with read
	if err != nil {
		return "", err
	}

	//	Save response
	log.Println("Saving response")
	response := strings.Split(string(respBytes[:n]), protocols.GetSep())

	//	Get received data
	log.Printf("Getting data from response: %s\n", response)
	data := strings.Split(response[1], sip.GetDataSep())

	//	Find server address
	log.Printf("Looking for parent IP in data: %s\n", data)
	for _, s := range data {
		kv := strings.Split(s, "=")
		if kv[0] == "parent" {
			return kv[1], nil
		}
	}

	log.Println("No IP received")
	return "", nil
}
Ejemplo n.º 3
0
//	Returns data as string from string.
func ExtractData(msg string) string {
	splited_msg := strings.Split(msg, protocols.GetSep())
	return splited_msg[1]
}
Ejemplo n.º 4
0
//	Returns type of given string representing message.
func ExtractType(msg string) string {
	return strings.Split(msg, protocols.GetSep())[0]
}
Ejemplo n.º 5
0
//	Returns struct Message while given a string describing it.
func GetMessage(msg string) Message {
	splited_msg := strings.Split(msg, protocols.GetSep())

	return Message{splited_msg[0], InterpreteData(splited_msg[1]), splited_msg[2]}
}
Ejemplo n.º 6
0
func (msg *Message) ToString() string {
	return fmt.Sprintf("%s%s%s", msg.Data, protocols.GetSep(), msg.Error)
}