PHP Classes

File: tests/server.go

Recommend this page to a friend!
  Classes of Wolfy-J  >  goridge  >  tests/server.go  >  Download  
File: tests/server.go
Role: Auxiliary data
Content type: text/plain
Description: Auxiliary data
Class: goridge
Run Golang code from PHP calling its RPC server
Author: By
Last change: CS and linters
CS and linters
CS and linters
CS and linters
release 2.0.0
Date: 3 years ago
Size: 1,407 bytes
 

Contents

Class file image Download
package main

import (
	"github.com/spiral/goridge"
	"net"
	"net/rpc"
	"os"
	"strings"
)

// Service sample
type Service struct{}

// Payload sample
type Payload struct {
	Name  string            `json:"name"`
	Value int               `json:"value"`
	Keys  map[string]string `json:"keys,omitempty"`
}

// Negate number
func (s *Service) Negate(i int64, r *int64) error {
	*r = -i
	return nil
}

// Ping pong
func (s *Service) Ping(msg string, r *string) error {
	if msg == "ping" {
		*r = "pong"
	}
	return nil
}

// Echo returns incoming message
func (s *Service) Echo(msg string, r *string) error {
	*r = msg
	return nil
}

// Process performs payload conversion
func (s *Service) Process(msg Payload, r *Payload) error {
	r.Name = strings.ToUpper(msg.Name)
	r.Value = -msg.Value

	if len(msg.Keys) != 0 {
		r.Keys = make(map[string]string)
		for n, v := range msg.Keys {
			r.Keys[v] = n
		}
	}

	return nil
}

// EchoBinary work over binary data
func (s *Service) EchoBinary(msg []byte, out *[]byte) error {
	*out = append(*out, msg...)

	return nil
}

func main() {
	var ln net.Listener
	var err error
	if len(os.Args) == 2 {
		ln, err = net.Listen("unix", os.Args[1])
	} else {
		ln, err = net.Listen("tcp", ":7079")
	}

	if err != nil {
		panic(err)
	}

	rpc.Register(new(Service))

	for {
		conn, err := ln.Accept()
		if err != nil {
			continue
		}
		go rpc.ServeCodec(goridge.NewCodec(conn))
	}
}
For more information send a message to info at phpclasses dot org.