Skip to content

Commit

Permalink
Merge pull request #17 from JackOBrien/develop
Browse files Browse the repository at this point in the history
Sprint 2
  • Loading branch information
Jack O'Brien committed Nov 4, 2015
2 parents 1ab7c1a + 91b741c commit 5204016
Show file tree
Hide file tree
Showing 19 changed files with 540 additions and 183 deletions.
2 changes: 1 addition & 1 deletion META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
<actions>
<!-- Add your actions here -->

<action id="io.startProject" class="com.io.gui.StartIo" text="Start Project Io As Server">
<action id="io.startProject" class="com.io.gui.StartIoServer" text="Start Project Io As Server">
<add-to-group group-id="ToolsMenu" anchor="first"/>
</action>

Expand Down
19 changes: 19 additions & 0 deletions src/com/io/domain/ConnectionUpdate.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.io.domain;


import com.io.gui.UserInfo;

import java.util.ArrayList;

public class ConnectionUpdate extends Packet {
private ArrayList<UserInfo> userTable;

public ConnectionUpdate(int userId, ArrayList<UserInfo> userTable){
super(userId, PacketType.CONNECTION_UPDATE);
this.userTable = userTable;
}

public ArrayList<UserInfo> getUserList() {
return userTable;
}
}
4 changes: 2 additions & 2 deletions src/com/io/domain/FileTransfer.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.io.domain;

public class FileTransfer extends Packet{
public FileTransfer(){
super(PacketType.FILE_TRANSFER.id());
public FileTransfer(int userId){
super(userId, PacketType.FILE_TRANSFER);
}
}
15 changes: 3 additions & 12 deletions src/com/io/domain/Login.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,13 @@

public class Login extends Packet {
private String username;
private int userId;

public Login(String username){
super(PacketType.LOGIN.id());
public Login(int userId, String username) {
super(userId, PacketType.LOGIN);
this.username = username;
}

public String getUsername(){
public String getUsername() {
return this.username;
}

public void setUserId(int userId) {
this.userId = userId;
}

public int getUserId() {
return userId;
}
}
17 changes: 14 additions & 3 deletions src/com/io/domain/Packet.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,24 @@

public abstract class Packet implements Serializable {

private int packetType;
private int userId;

public Packet(int packetType) {
private PacketType packetType;

public Packet(int userId, PacketType packetType) {
this.userId = userId;
this.packetType = packetType;
}

public int getPacketType() {
public int getUserId() {
return userId;
}

public void setUserId(int userId) {
this.userId = userId;
}

public PacketType getPacketType() {
return packetType;
}
}
19 changes: 5 additions & 14 deletions src/com/io/domain/PacketType.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,9 @@
package com.io.domain;

public enum PacketType {
DOCUMENT_EDIT(0),
CURSOR_MOVE(1),
LOGIN(2),
FILE_TRANSFER(3);

private int packetID;

PacketType(int packetID){
this.packetID = packetID;
}

public int id() {
return packetID;
}
DOCUMENT_EDIT,
CURSOR_MOVE,
LOGIN,
FILE_TRANSFER,
CONNECTION_UPDATE
}
27 changes: 15 additions & 12 deletions src/com/io/domain/UserEdit.java
Original file line number Diff line number Diff line change
@@ -1,30 +1,28 @@
package com.io.domain;

public class UserEdit extends Packet {
private int userId;

private String editText;

private String filePath;
private int lengthDifference;

private int offset;

public UserEdit(int userId, String editText, int offset, int lengthDifference) {
public UserEdit(int userId, String editText, String filePath, int offset, int lengthDifference) {

super(PacketType.DOCUMENT_EDIT.id());
super(userId, PacketType.DOCUMENT_EDIT);

this.userId = userId;
this.editText = editText;
this.filePath = filePath;
this.offset = offset;
this.lengthDifference = lengthDifference;
}

public UserEdit(int userId, int offset, int lengthDifference) {
public UserEdit(int userId, String filePath, int offset, int lengthDifference) {

super(PacketType.CURSOR_MOVE.id());
super(userId, PacketType.CURSOR_MOVE);

this.userId = userId;
this.editText = null;
this.filePath = filePath;
this.offset = offset;
this.lengthDifference = lengthDifference;
}
Expand All @@ -41,13 +39,18 @@ public int getLengthDifference() {
return this.lengthDifference;
}

public String getFilePath() {
return this.filePath;
}

@Override
public String toString() {
return "UserEdit{" +
"userId=" + userId +
", editText='" + editText + '\'' +
"userId=" + super.getUserId() +
", editText='" + editText + "'" +
", filePath='" + filePath + "'" +
", offset=" + offset +
", lengthDifference=" + lengthDifference +
'}';
"}";
}
}
93 changes: 93 additions & 0 deletions src/com/io/gui/Client.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package com.io.gui;


import com.intellij.openapi.editor.Editor;
import com.io.domain.ConnectionUpdate;
import com.io.domain.Login;
import com.io.domain.UserEdit;
import com.io.net.Connector;
import com.io.net.ConnectorEvent;

import javax.swing.*;
import java.io.IOException;
import java.util.ArrayList;

public class Client {

public static final int INITIAL_USER_ID = -1;

public static final String INITIAL_USER_NAME = "user";

public StartListening listening; //TODO: Make private

public StartReceiving receiving;

private String username;

private int userId;

private Connector connector;

private UserListWindow userListWindow;

public Client (final Editor editor) {

listening = new StartListening(editor);
receiving = new StartReceiving(editor, listening);

userListWindow = new UserListWindow(editor.getProject());

try {
connector = new Connector();
} catch(IOException ex) {
System.out.println("Failed to connect to server");
return;
}

listening.addEventListener(new EditorEvent() {
@Override
public void sendChange(UserEdit userEdit) {
userEdit.setUserId(userId);
connector.sendUserEdit(userEdit);
}
});

connector.addEventListener(new ConnectorEvent() {
@Override
public void applyUserEdit(UserEdit userEdit) {
userEdit.setUserId(userId);
receiving.applyUserEditToDocument(editor, userEdit);
}

@Override
public void applyUserId(Login login, Connector connector) {
userId = login.getUserId();
username = login.getUsername();
userListWindow.addUser(new UserInfo(userId, username));
System.out.println(editor.getProject().getName() + ": User id is now " + userId);
}

@Override
public void applyConnectionUpdate(ConnectionUpdate connectionUpdate) {
ArrayList<UserInfo> users = connectionUpdate.getUserList();

System.out.println("Client received " + users.size());

for (UserInfo user : users) {
userListWindow.addUser(user);
}
}
});

(new Thread(connector)).start();

login();
}

private void login() {
username = JOptionPane.showInputDialog("Please enter a username");

Login login = new Login(INITIAL_USER_ID, username);
connector.sendObject(login);
}
}
51 changes: 0 additions & 51 deletions src/com/io/gui/StartIo.java

This file was deleted.

46 changes: 2 additions & 44 deletions src/com/io/gui/StartIoClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,54 +4,12 @@
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.LangDataKeys;
import com.intellij.openapi.editor.Editor;
import com.io.domain.UserEdit;
import com.io.net.Connector;
import com.io.net.ConnectorEvent;

import java.io.IOException;
import java.util.ArrayList;

public class StartIoClient extends AnAction {

ArrayList<Editor> editors;
public StartListening listening;
public StartReceiving receiving;

public void actionPerformed(AnActionEvent e) {
editors = new ArrayList<Editor>();
final Editor editor = e.getData(LangDataKeys.EDITOR);
editors.add(editor);

listening = new StartListening(editor);
receiving = new StartReceiving(editor, listening.getDocumentListener());

final Connector connector;

try {
connector = new Connector();
}
catch(IOException ex) {
System.out.println("Failed to connect to server");
return;
}


listening.addEventListener(new EditorEvent() {
@Override
public void sendChange(UserEdit userEdit) {
connector.sendUserEdit(userEdit);
}
});

connector.addEventListener(new ConnectorEvent() {
@Override
public void applyUserEdit(UserEdit userEdit) {
receiving.applyUserEditToDocument(editor, userEdit);
}
});

(new Thread(connector)).start();

Editor editor = e.getData(LangDataKeys.EDITOR);
new Client(editor);
}

}
16 changes: 16 additions & 0 deletions src/com/io/gui/StartIoServer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.io.gui;

import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.LangDataKeys;
import com.intellij.openapi.editor.Editor;
import com.io.net.Server;

public class StartIoServer extends AnAction {

public void actionPerformed(AnActionEvent e) {

final Editor editor = e.getData(LangDataKeys.EDITOR);
new Server(editor);
}
}
Loading

0 comments on commit 5204016

Please sign in to comment.