Skip to content

Commit

Permalink
debug
Browse files Browse the repository at this point in the history
  • Loading branch information
marinhero committed Feb 19, 2015
1 parent e05cd1d commit 0a6dfda
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 24 deletions.
20 changes: 15 additions & 5 deletions UDPush/UDPush.go
Expand Up @@ -3,7 +3,7 @@
** Author: Marin Alcaraz
** Mail <marin.alcaraz@gmail.com>
** Started on Mon Feb 09 14:36:00 2015 Marin Alcaraz
** Last update Thu Feb 19 12:58:32 2015 Marin Alcaraz
** Last update Thu Feb 19 14:41:39 2015 Marin Alcaraz
*/

package UDPush
Expand Down Expand Up @@ -56,6 +56,7 @@ type Watcher struct {
ClientID int
SessionKey string
Action bool
Connection net.Conn
}

// Methods for struct to satisfy the notificationEngine interface
Expand Down Expand Up @@ -84,7 +85,8 @@ func (e *Pusher) Attach(w Watcher) (err error) {
//Detach Remove a watcher from the notification slice
func (e *Pusher) Detach(w Watcher) (err error) {
//Check if element already exists
if _, k := e.Watchers[w.SessionKey]; k {
if item, ok := e.Watchers[w.SessionKey]; ok {
item.Connection.Close()
delete(e.Watchers, w.SessionKey)
return nil
}
Expand All @@ -94,7 +96,6 @@ func (e *Pusher) Detach(w Watcher) (err error) {
//Notify Tell the watcher {clientID} to update
func (e *Pusher) Notify(sessionkey string) {
for _, k := range e.Watchers {
e.ShowWatchers()
//if k.SessionKey == sessionkey {
k.Update()
k.Action = true
Expand All @@ -106,7 +107,6 @@ func (e *Pusher) Notify(sessionkey string) {

//ShowWatchers Print current watchers in pusher
func (e *Pusher) ShowWatchers() {
fmt.Printf("Current watchers in %s:\n", e.ServerID)
for _, k := range e.Watchers {
fmt.Println("Watcher: ", k)
}
Expand All @@ -117,6 +117,16 @@ func (e *Pusher) ShowWatchers() {
// Update Get update from pusher... Golint forces me to do this
// http://tinyurl.com/lhzjvmm
func (w *Watcher) Update() {
w.Action = true
fits := w.SessionKey[:2]
fmt.Println("[!]Attempting to update watcher: %s", fits)
writen, err := w.Connection.Write([]byte("Y"))
if writen != len([]byte("Y")) {
fmt.Println("[!]Error writting: unable to write")
}
if err != nil {
fmt.Printf("%s", err)
}

}

Expand All @@ -140,7 +150,6 @@ func (e *Pusher) InitUDPush() error {
fmt.Println("[+] UDP Listening on: ", connectionString)
for {
conn, err := ln.Accept()
defer conn.Close()
fmt.Println("Host connected: ", ln.Addr())
if err != nil {
return fmt.Errorf("Error at initUDPush: %s", err)
Expand All @@ -149,6 +158,7 @@ func (e *Pusher) InitUDPush() error {
conn.Read(session)
e.Attach(Watcher{
SessionKey: string(session),
Connection: conn,
})
}
}
19 changes: 13 additions & 6 deletions client/api/api.go
Expand Up @@ -51,6 +51,7 @@ func (c *Api) SendFileActionsToServer(

jsonBytes, err := json.Marshal(fileActions)
if err != nil {
fmt.Println("[+]")
return
}

Expand All @@ -60,22 +61,25 @@ func (c *Api) SendFileActionsToServer(
"application/json",
)

if err != nil {
return
}
//if err != nil {
//fmt.Println("[!] API")
//return
//}

contents, err := ioutil.ReadAll(resp.Body)
contents, _ := ioutil.ReadAll(resp.Body)
fmt.Println(contents)
if err != nil {
fmt.Println("[*]")
return
}

if resp.StatusCode != http.StatusOK {
err = fmt.Errorf(string(contents))
return
}

err = json.Unmarshal(contents, &filesToUpload)
if err != nil {
fmt.Println("[-]")
return
}
return
Expand Down Expand Up @@ -134,23 +138,26 @@ func (c *Api) DownloadClientFileActions(lastId int64) (
var lastIdString string
lastIdString = strconv.FormatInt(lastId, 32)
resp, err := http.PostForm(
ApiEndpoint+"/clients/",
ApiEndpoint+"clients/",
url.Values{
"SessionKey": {c.SessionKey},
"lastID": {lastIdString},
},
)
if err != nil {
fmt.Println("[!]")
return
}
contents, err := ioutil.ReadAll(resp.Body)
if resp.StatusCode != http.StatusOK {
err = fmt.Errorf(string(contents))
fmt.Println(resp.StatusCode)
return
}

err = json.Unmarshal(contents, &clientFileActionsResponse)
if err != nil {
fmt.Println("[+]")
return
}
return
Expand Down
19 changes: 12 additions & 7 deletions client/client.go
Expand Up @@ -156,17 +156,23 @@ func serverActions(UDPing <-chan bool, fileActionIdPath string,
<-UDPing
fmt.Println("-----------------PING RECIEVED--------------------")
// these return values are obviously wrong right now
fmt.Println("1")
clientFileActionResponse, err := client.DownloadClientFileActions(
fileActionId)
fmt.Println("2")
// need to rethink errors, assumption of a statechange is invalid
if err != nil {
fmt.Println(err)
writeError(err, structs.StateChange{}, "serverActions")
}
fileActionId = clientFileActionResponse.LastId
fmt.Println("3")
for _, fileAction := range clientFileActionResponse.FileActions {
change := createServerStateChange(fileAction)
out <- change
fmt.Println("loop")
}
fmt.Println("4")
err = writeFileActionIDToLocalFile(fileActionId, fileActionIdPath)
if err != nil {
fmt.Println("Couldn't write fileActionId to path : ",
Expand All @@ -191,10 +197,13 @@ func initUDPush(sessionKey string) (notification chan bool, err error) {
if err != nil {
return
}
response := make([]byte, 1)
response := make([]byte, 21)
for {
read, _ := conn.Read(response)
fmt.Println("message read from socket", read)
read, err := conn.Read(response)
if err != nil {
fmt.Println(err)
}
fmt.Println("Message read from socket: ", read, string(response))
notification <- true
fmt.Println("YO")
}
Expand Down Expand Up @@ -349,7 +358,6 @@ func uploader(path string, change structs.StateChange, fa structs.FileAction) {
gracefulQuit(change)
return
default:
fmt.Println(path)
buf, err := ioutil.ReadFile(path)
if err != nil {
writeError(err, change, "uploader")
Expand Down Expand Up @@ -594,7 +602,6 @@ func run(path string) {
watcherInitScanDone := make(chan struct{})
serverActionsInitScanDone := make(chan struct{})
client = api.New("")
fmt.Println(client.SessionKey)
err := os.Chdir(path)
if err != nil {
fmt.Println("unable to change dir, quitting")
Expand All @@ -612,15 +619,13 @@ func run(path string) {
watcherInitScanDone,
serverActionsInitScanDone,
)
fmt.Println("after init scan started")
if err != nil {
panic("Could not start init scan")
}
watcherActions, err := startWatcher(goboxDirectory, watcherInitScanDone)
if err != nil {
panic("Could not start watcher")
}
fmt.Println("RUN")
UDPNotification, err := initUDPush(client.SessionKey)
if err != nil {
panic("Could not start UDP socket")
Expand Down
11 changes: 6 additions & 5 deletions main_test.go
Expand Up @@ -7,6 +7,7 @@ import (
"time"

// "github.com/golangbox/gobox/boxtools"
"github.com/golangbox/gobox/boxtools"
"github.com/golangbox/gobox/server"
"github.com/golangbox/gobox/server/model"
"github.com/golangbox/gobox/structs"
Expand Down Expand Up @@ -37,12 +38,12 @@ func TestEverything(t *testing.T) {
"sandbox/client1/",
"sandbox/client2/",
}
// ignores := make(map[string]bool)
ignores := make(map[string]bool)
for _, value := range paths {
// err := boxtools.CleanTestFolder(value, ignores, true)
// if err != nil {
// panic("Could not delete folder contents")
// }
err := boxtools.CleanTestFolder(value, ignores, true)
if err != nil {
panic("Could not delete folder contents")
}
go func(value string) {
cmd := exec.Command(
"go",
Expand Down
1 change: 0 additions & 1 deletion server/api/api.go
Expand Up @@ -146,7 +146,6 @@ func FileActionsHandler(w http.ResponseWriter, req *http.Request,
model.DB.Where("user_id = ?", user.Id).
Not("id = ?", client.Id).
Find(&clients)
//How to get the right clients to notify?

for _, value := range clients {
Pusher.Notify(value.SessionKey)
Expand Down

0 comments on commit 0a6dfda

Please sign in to comment.