<commit_before>package valente

import (
	"errors"
	"log"
	"time"

	"github.com/trumae/valente/action"

	"golang.org/x/net/websocket"
)

const (
	endofmessage = "___ENDOFMESSAGE___"
)

var (
	ProtocolError = errors.New("Protocol Error")
)

//HandlerFunc is a function of handle an event received into websocket.Conn
type HandlerFunc func(*websocket.Conn, *App, []string)

//Form represents the unit of user interaction
type Form interface {
	AddEventHandler(evt string, f HandlerFunc) Form
	Run(ws *websocket.Conn, app *App) error
	Initialize(ws *websocket.Conn) Form
	Render(ws *websocket.Conn, app *App, params []string) error
}

//FormImpl its a simple Form
type FormImpl struct {
	trans map[string]HandlerFunc
}

//AddEventHandler add an f function to handle evt event
func (form FormImpl) AddEventHandler(evt string, f HandlerFunc) Form {
	if form.trans == nil {
		form.trans = map[string]HandlerFunc{}
	}
	form.trans[evt] = f
	return form
}

//Run execs the form
func (form FormImpl) Run(ws *websocket.Conn, app *App) error {
	msgs := []string{}
	for {
		msg := ""
		err := websocket.Message.Receive(ws, &msg)
		if err != nil {
			log.Println("Error on WS Receive", err)
			return err
		}
		if msg == endofmessage {
			break
		} else {
			msgs = append(msgs, msg)
		}
	}
	log.Printf("currentForm = %v msgs = %v\n", app.CurrentForm, msgs)
	if len(msgs) < 1 {
		log.Println("Protocol error len(msgs) < 1", msgs)
		return ProtocolError
	}

	f, present := form.trans[msgs[0]]
	if present {
		f(ws, app, msgs)
	} else {
		log.Println("Evt not found", msgs[0])
	}
	return nil
}

//Initialize inits the form
func (form FormImpl) Initialize(ws *websocket.Conn) Form {
	log.Println("FormImpl Initialize")
	return form
}

func (form FormImpl) Render(ws *websocket.Conn, app *App, params []string) error {
	log.Println("FormImpl Render")
	return nil
}

//App is a Web Application representation
type App struct {
	WS          *websocket.Conn
	Forms       map[string]Form
	Data        map[string]interface{}
	CurrentForm Form
}

//GoTo replace the current form into app
func (app *App) GoTo(formName string, params []string) error {
	log.Println("App goto", formName)
	form, present := app.Forms[formName]
	if present {
		app.CurrentForm = form.Initialize(app.WS)
		err := form.Render(app.WS, app, params)
		if err != nil {
			log.Println("Error on goto", err)
		}
		action.UnblockUI(app.WS)
	} else {
		log.Println("[ERROR] Form not registred", formName)
	}
	return nil
}

//Run handle events
func (app *App) Run() {
	app.Data = map[string]interface{}{}
	go func() {
		c := time.Tick(10 * time.Second)
		for _ = range c {
			err := action.Exec(app.WS, "1 == 1;")
			if err != nil {
				log.Println("Error in connection probe", err)
				return
			}
		}
	}()

	for {
		err := app.CurrentForm.Run(app.WS, app)
		if err != nil {
			log.Println(err)
			continue
		}
	}
}

//Initialize inits the App
func (app *App) Initialize() {
	log.Println("App Initialize")
}

//AddForm add a new form to App
func (app *App) AddForm(name string, f Form) {
	log.Println("AddForm", name, f)
	if app.Forms == nil {
		app.Forms = map[string]Form{}
	}

	app.Forms[name] = f
}<commit_msg>handle different error types in protocol<commit_after>package valente

import (
	"errors"
	"log"
	"time"

	"github.com/trumae/valente/action"

	"golang.org/x/net/websocket"
)

const (
	endofmessage = "___ENDOFMESSAGE___"
)

var (
	ProtocolError = errors.New("Protocol Error")
	EOFWSError    = errors.New("Protocol Error")
)

//HandlerFunc is a function of handle an event received into websocket.Conn
type HandlerFunc func(*websocket.Conn, *App, []string)

//Form represents the unit of user interaction
type Form interface {
	AddEventHandler(evt string, f HandlerFunc) Form
	Run(ws *websocket.Conn, app *App) error
	Initialize(ws *websocket.Conn) Form
	Render(ws *websocket.Conn, app *App, params []string) error
}

//FormImpl its a simple Form
type FormImpl struct {
	trans map[string]HandlerFunc
}

//AddEventHandler add an f function to handle evt event
func (form FormImpl) AddEventHandler(evt string, f HandlerFunc) Form {
	if form.trans == nil {
		form.trans = map[string]HandlerFunc{}
	}
	form.trans[evt] = f
	return form
}

//Run execs the form
func (form FormImpl) Run(ws *websocket.Conn, app *App) error {
	msgs := []string{}
	for {
		msg := ""
		err := websocket.Message.Receive(ws, &msg)
		if err != nil {
			log.Println("Error on WS Receive", err)
			return EOFWSError
		}
		if msg == endofmessage {
			break
		} else {
			msgs = append(msgs, msg)
		}
	}
	log.Printf("currentForm = %v msgs = %v\n", app.CurrentForm, msgs)
	if len(msgs) < 1 {
		log.Println("Protocol error len(msgs) < 1", msgs)
		return ProtocolError
	}

	f, present := form.trans[msgs[0]]
	if present {
		f(ws, app, msgs)
	} else {
		log.Println("Evt not found", msgs[0])
	}
	return nil
}

//Initialize inits the form
func (form FormImpl) Initialize(ws *websocket.Conn) Form {
	log.Println("FormImpl Initialize")
	return form
}

func (form FormImpl) Render(ws *websocket.Conn, app *App, params []string) error {
	log.Println("FormImpl Render")
	return nil
}

//App is a Web Application representation
type App struct {
	WS          *websocket.Conn
	Forms       map[string]Form
	Data        map[string]interface{}
	CurrentForm Form
}

//GoTo replace the current form into app
func (app *App) GoTo(formName string, params []string) error {
	log.Println("App goto", formName)
	form, present := app.Forms[formName]
	if present {
		app.CurrentForm = form.Initialize(app.WS)
		err := form.Render(app.WS, app, params)
		if err != nil {
			log.Println("Error on goto", err)
		}
		action.UnblockUI(app.WS)
	} else {
		log.Println("[ERROR] Form not registred", formName)
	}
	return nil
}

//Run handle events
func (app *App) Run() {
	app.Data = map[string]interface{}{}
	go func() {
		c := time.Tick(10 * time.Second)
		for _ = range c {
			err := action.Exec(app.WS, "1 == 1;")
			if err != nil {
				log.Println("Error in connection probe", err)
				return
			}
		}
	}()

	for {
		err := app.CurrentForm.Run(app.WS, app)
		if err != nil {
			log.Println(err)
			switch {
			case err == ProtocolError:
				continue
			case err == EOFWSError:
				return
			default:
				continue
			}
		}
	}
}

//Initialize inits the App
func (app *App) Initialize() {
	log.Println("App Initialize")
}

//AddForm add a new form to App
func (app *App) AddForm(name string, f Form) {
	log.Println("AddForm", name, f)
	if app.Forms == nil {
		app.Forms = map[string]Form{}
	}

	app.Forms[name] = f
}
{'fraction_non_alphanumeric': 0.13565831925026886, 'fraction_numerical': 0.0024581348901520973, 'mean_word_length': 3.58128078817734, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '42483', 'n_tokens_mistral': 2487, 'n_tokens_neox': 2341, 'n_words': 674}