Ejemplo n.º 1
0
/*
ServiceClient.NewRequestInfo() create a new RequestInfo object specific to this service
*/
func (c *ServiceClient) NewRequestInfo() (ri *skynet.RequestInfo) {
	// TODO: Set
	ri = &skynet.RequestInfo{
		RequestID: config.NewUUID(),
	}

	return
}
Ejemplo n.º 2
0
func (s *SkynetDaemon) StartSubService(requestInfo *skynet.RequestInfo, in daemon.StartSubServiceRequest, out *daemon.StartSubServiceResponse) (err error) {
	out.UUID = config.NewUUID()

	log.Printf(log.TRACE, "%+v", SubserviceStart{
		BinaryName: in.BinaryName,
		Args:       in.Args,
	})

	ss, err := NewSubService(in.BinaryName, in.Args, out.UUID, in.Registered)
	if err != nil {
		return
	}

	s.serviceLock.Lock()
	s.Services[out.UUID] = ss
	s.serviceLock.Unlock()

	start, startErr := ss.Start()

	if startErr != nil {
		return errors.New("Service failed to start: " + startErr.Error())
	} else if !start {
		return errors.New("Service failed to start")
	}

	tc := time.Tick(RerunWait * 2)

	go func() {
		// Wait for startup timer to see if we're still running
		// We want to avoid keeping a state of a large list of services that failed to start
		<-tc

		if ss.IsRunning() {
			s.saveState()
		}
	}()

	return
}
Ejemplo n.º 3
0
// this function is the goroutine that owns this service - all thread-sensitive data needs to
// be manipulated only through here.
func (s *Service) mux() {
loop:
	for {
		select {
		case conn := <-s.connectionChan:
			go func() {
				clientID := config.NewUUID()

				s.clientMutex.Lock()
				s.ClientInfo[clientID] = ClientInfo{
					Address: conn.RemoteAddr(),
				}
				s.clientMutex.Unlock()

				// send the server handshake
				sh := skynet.ServiceHandshake{
					Registered: s.Registered,
					ClientID:   clientID,
					Name:       s.Name,
				}

				codec := bsonrpc.NewServerCodec(conn)

				log.Println(log.TRACE, "Sending ServiceHandshake")
				err := codec.Encoder.Encode(sh)
				if err != nil {
					log.Println(log.ERROR, "Failed to encode server handshake", err.Error())
					conn.Close()
					return
				}
				if !s.Registered {
					log.Println(log.ERROR, "Connection attempted while unregistered. Closing connection")
					conn.Close()
					return
				}

				// read the client handshake
				var ch skynet.ClientHandshake
				log.Println(log.TRACE, "Reading ClientHandshake")
				err = codec.Decoder.Decode(&ch)
				if err != nil {
					log.Println(log.ERROR, "Error decoding ClientHandshake: "+err.Error())
					conn.Close()
					return
				}

				// here do stuff with the client handshake
				log.Println(log.TRACE, "Handing connection to RPC layer")
				s.RPCServ.ServeCodec(codec)
			}()
		case register := <-s.registeredChan:
			if register {
				s.register()
			} else {
				s.unregister()
			}
		case <-s.shutdownChan:
			s.shutdown()
		case _ = <-s.doneChan:
			break loop
		}
	}
}