Mercurial > hg > Database > Alice
changeset 601:30f2c04571c2 dispose
add chat, add Star topology to topology manager&node
author | Nozomi Teruya <e125769@ie.u-ryukyu.ac.jp> |
---|---|
date | Wed, 27 Apr 2016 16:28:10 +0900 |
parents | 0564f38e9bfe |
children | 8a9fd716c335 |
files | src/main/java/alice/datasegment/Receiver.java src/main/java/alice/test/chat/ChatWindow.java src/main/java/alice/test/chat/ConnectRoot.java src/main/java/alice/test/chat/CreateChatWindow.java src/main/java/alice/test/chat/FirstWindow.java src/main/java/alice/test/chat/ReceiveMessage.java src/main/java/alice/test/chat/SendMessage.java src/main/java/alice/test/chat/TreeChat.java src/main/java/alice/topology/manager/ComingStarHosts.java src/main/java/alice/topology/manager/CreateStarTopology.java src/main/java/alice/topology/manager/StartTopologyManager.java src/main/java/alice/topology/manager/TopologyType.java src/main/java/alice/topology/node/IncomingConnectionInfo.java |
diffstat | 13 files changed, 393 insertions(+), 5 deletions(-) [+] |
line wrap: on
line diff
--- a/src/main/java/alice/datasegment/Receiver.java Tue Feb 16 19:12:52 2016 +0900 +++ b/src/main/java/alice/datasegment/Receiver.java Wed Apr 27 16:28:10 2016 +0900 @@ -87,7 +87,7 @@ } public void setKey(String key, int index) { - + this.managerKey="local"; this.key = key; this.index = index; switch (type) {
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/alice/test/chat/ChatWindow.java Wed Apr 27 16:28:10 2016 +0900 @@ -0,0 +1,55 @@ +package alice.test.chat; + +import alice.codesegment.CodeSegment; +import alice.datasegment.CommandType; +import alice.datasegment.Receiver; + +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +/** + * Created by e125769 on 3/29/16. + */ +public class ChatWindow extends CodeSegment{ + + public JTextArea textArea; + public Receiver name = ids.create(CommandType.PEEK); + + public ChatWindow(){ + name.setKey("local","userName"); + } + + @Override + public void run() { + JFrame mainFrame = new JFrame("Chat"); + mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + mainFrame.setSize(300, 500); + mainFrame.setLocationRelativeTo(null); + Container contentPane = mainFrame.getContentPane(); + + textArea = new JTextArea(5,0); + JScrollPane scrollPane = new JScrollPane(textArea); + + final JTextField textField = new JTextField(); + textField.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + send("(" + name+ ") " + textField.getText() + "\n"); + textField.setText(""); + } + }); + + contentPane.add(scrollPane, BorderLayout.CENTER); + contentPane.add(textField, BorderLayout.SOUTH); + mainFrame.setVisible(true); + } + + public void show(String message){ + textArea.append(message); + } + + public void send(String message){ + ods.put("sendMessage", message); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/alice/test/chat/ConnectRoot.java Wed Apr 27 16:28:10 2016 +0900 @@ -0,0 +1,40 @@ +package alice.test.chat; + +import alice.codesegment.CodeSegment; +import alice.datasegment.CommandType; +import alice.datasegment.DataSegment; +import alice.datasegment.Receiver; + +import java.net.InetAddress; +import java.net.UnknownHostException; +import java.util.ArrayList; + +/** + * Created by e125769 on 3/29/16. + */ +public class ConnectRoot extends CodeSegment{ + private Receiver info = ids.create(CommandType.PEEK); + + public ConnectRoot() { + info.setKey("_CLIST"); + info.setKey("host"); + } + + @Override + public void run() { + ArrayList<String> clist = info.asClass(ArrayList.class); + + if (clist.contains("parent")){ + + } else { + InetAddress addr; + try { + addr = InetAddress.getLocalHost(); + ods.put("root", addr.getHostAddress()); + DataSegment.connect("root", "root", addr.getHostAddress(), 20000); + } catch (UnknownHostException e) { + e.printStackTrace(); + } + } + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/alice/test/chat/CreateChatWindow.java Wed Apr 27 16:28:10 2016 +0900 @@ -0,0 +1,28 @@ +package alice.test.chat; + +import alice.codesegment.CodeSegment; +import alice.datasegment.CommandType; +import alice.datasegment.Receiver; + +import javax.swing.*; + +/** + * Created by e125769 on 3/29/16. + */ +public class CreateChatWindow extends CodeSegment{ + + Receiver name = ids.create(CommandType.PEEK); + + public CreateChatWindow(){ + name.setKey("local", "userName"); + } + + @Override + public void run() { + ChatWindow window = new ChatWindow(); + new SendMessage(window); + new ReceiveMessage(window); + } + + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/alice/test/chat/FirstWindow.java Wed Apr 27 16:28:10 2016 +0900 @@ -0,0 +1,64 @@ +package alice.test.chat; + +import alice.codesegment.CodeSegment; +import alice.topology.node.TopologyNode; +import alice.topology.node.TopologyNodeConfig; + +import javax.swing.*; +import java.awt.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +/** + * Created by e125769 on 3/29/16. + */ +public class FirstWindow extends CodeSegment{ + + @Override + public void run() { + final JFrame mainFrame = new JFrame("ChatInfo"); + final JTextField TopM_IP; + final JTextField Port; + final JTextField Name; + + mainFrame.setBounds(400, 400, 300, 250); + mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + + JPanel p = new JPanel(); + + TopM_IP = new JTextField(10); + Port = new JTextField(10); + Name = new JTextField(10); + + JButton button = new JButton("Submit"); + button.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + //もし未入力があればアラートを出す + + String[] config = new String[]{"-host", TopM_IP.getText(), "-p", Port.getText()}; + ods.put("userName", Name.getText()); + + //new TopologyNode(new TopologyNodeConfig(config), new CreateChatWindow()); + new CreateChatWindow(); + + mainFrame.dispose(); + } + }); + + p.setLayout(new FlowLayout()); + p.add(new JLabel("TopologyManagerIP:", SwingConstants.LEFT)); + p.add(TopM_IP); + p.add(new JLabel("TopologyManagerPort:", SwingConstants.LEFT)); + p.add(Port); + p.add(new JLabel("Name:", SwingConstants.LEFT)); + p.add(Name); + p.add(button); + + + Container contentPane = mainFrame.getContentPane(); + contentPane.add(p, BorderLayout.CENTER); + //contentPane.add(label, BorderLayout.SOUTH); + + mainFrame.setVisible(true); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/alice/test/chat/ReceiveMessage.java Wed Apr 27 16:28:10 2016 +0900 @@ -0,0 +1,29 @@ +package alice.test.chat; + +import alice.codesegment.CodeSegment; +import alice.datasegment.CommandType; +import alice.datasegment.Receiver; + +/** + * Created by e125769 on 3/29/16. + */ +public class ReceiveMessage extends CodeSegment{ + + public Receiver info1 = ids.create(CommandType.TAKE); + public Receiver info2 = ids.create(CommandType.PEEK); + private ChatWindow chatWindow; + + ReceiveMessage(ChatWindow chatWindow){ + this.chatWindow = chatWindow; + info1.setKey("local", "receiveMessage"); + info2.setKey("local", "_CLIST"); + } + + @Override + public void run() { + + chatWindow.show(info1.asString()); + + new ReceiveMessage(chatWindow); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/alice/test/chat/SendMessage.java Wed Apr 27 16:28:10 2016 +0900 @@ -0,0 +1,39 @@ +package alice.test.chat; + +import alice.codesegment.CodeSegment; +import alice.datasegment.CommandType; +import alice.datasegment.Receiver; + +import java.util.ArrayList; + +/** + * Created by e125769 on 3/29/16. + */ +public class SendMessage extends CodeSegment{ + + private Receiver info = ids.create(CommandType.TAKE); + private Receiver info1 = ids.create(CommandType.PEEK); + private ChatWindow chatWindow; + + public SendMessage(ChatWindow chatWindow) { + this.chatWindow = chatWindow; + info.setKey("local", "sendMessage"); + info1.setKey("local", "_CLIST"); + } + + @Override + public void run() { + ArrayList<String> clist = info1.asClass(ArrayList.class); + + if (clist.contains("parent")){//子ノード + ods.put("parent", "sendMessage", info.asString()); + } else {//親ノード + chatWindow.show(info.asString()); + for (String node : clist){ + ods.put(node, "receiveMessage", info.asString()); + } + } + + new SendMessage(chatWindow); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/alice/test/chat/TreeChat.java Wed Apr 27 16:28:10 2016 +0900 @@ -0,0 +1,15 @@ +package alice.test.chat; + +import org.apache.log4j.BasicConfigurator; + + +/** + * Created by e125769 on 3/29/16. + */ +public class TreeChat { + + public static void main(String args[]){ + new FirstWindow().execute(); + } + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/alice/topology/manager/ComingStarHosts.java Wed Apr 27 16:28:10 2016 +0900 @@ -0,0 +1,49 @@ +package alice.topology.manager; + +import alice.codesegment.MetaCodeSegment; +import alice.datasegment.CommandType; +import alice.datasegment.DataSegment; +import alice.datasegment.Receiver; +import alice.topology.HostMessage; +import org.msgpack.type.ValueFactory; + +import java.util.HashMap; + +/** + * Created by e125769 on 4/12/16. + * this class put root info + */ +public class ComingStarHosts extends MetaCodeSegment { + private Receiver info = ids.create(CommandType.TAKE); + private Receiver info1 = ids.create(CommandType.TAKE); + private Receiver info2 = ids.create(CommandType.TAKE); + + public ComingStarHosts(){ + info.setKey("newHost"); + info1.setKey("hostCount"); //incoming host count number for put node name like node0, node1, node2 + info2.setKey("nameTable"); //HashMap.<String:nodeName, HostMessage> + } + + @Override + public void run() { + HostMessage host = info.asClass(HostMessage.class); + int comingHostCount = info1.asInteger(); + + HashMap<String, HostMessage> nameTable = info2.asClass(HashMap.class); + + String nodeName = "node"+comingHostCount; + // Manager connect to Node + DataSegment.connect(nodeName, nodeName, host.name, host.port); + ods.put(nodeName, "host"+host.topMNum, nodeName); + + ods.put(info1.key, comingHostCount+1); + host.alive = true; + nameTable.put(nodeName, host); + + ods.put("root", host); + ods.put("start", ValueFactory.createNilValue()); + ods.put(info2.key, nameTable); + + new CreateStarTopology(); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/alice/topology/manager/CreateStarTopology.java Wed Apr 27 16:28:10 2016 +0900 @@ -0,0 +1,66 @@ +package alice.topology.manager; + +import alice.codesegment.MetaCodeSegment; +import alice.datasegment.CommandType; +import alice.datasegment.DataSegment; +import alice.datasegment.Receiver; +import alice.topology.HostMessage; +import org.msgpack.type.ValueFactory; + +import java.util.HashMap; + +/** + * Created by e125769 on 4/12/16. + * this class put node info + */ +public class CreateStarTopology extends MetaCodeSegment { + + private Receiver info = ids.create(CommandType.TAKE); + private Receiver info1 = ids.create(CommandType.TAKE); + private Receiver info2 = ids.create(CommandType.TAKE); + private Receiver info3 = ids.create(CommandType.PEEK); + + public CreateStarTopology(){ + info.setKey("newHost"); //HostMessage + info1.setKey("hostCount"); //incoming host count number for put node name like node0, node1, node2 + info2.setKey("nameTable"); //HashMap.<String:nodeName, HostMessage> + info3.setKey("root"); + } + + @Override + public void run() { + HostMessage host = info.asClass(HostMessage.class); + HostMessage root = info3.asClass(HostMessage.class); + int comingHostCount = info1.asInteger(); + + HashMap<String, HostMessage> nameTable = info2.asClass(HashMap.class); + + String nodeName = "node"+comingHostCount; + // Manager connect to Node + DataSegment.connect(nodeName, nodeName, host.name, host.port); + ods.put(nodeName, "host"+host.topMNum, nodeName); + + ods.put(info1.key, comingHostCount+1); + host.alive = true; + nameTable.put(nodeName, host); + + + // put parent information own + HostMessage newHost = new HostMessage(root.name, root.port, "parent", "child"+comingHostCount); + newHost.absName = root.name; + newHost.remoteAbsName = nodeName; // address + ods.put(newHost.remoteAbsName, newHost); + + // put own information parent + newHost = new HostMessage(host.name, host.port, "child"+comingHostCount, "parent"); + newHost.absName = nodeName; + newHost.remoteAbsName = root.name; + ods.put(newHost.remoteAbsName, newHost); + + + ods.put(info2.key, nameTable); + + new CreateStarTopology(); + + } +}
--- a/src/main/java/alice/topology/manager/StartTopologyManager.java Tue Feb 16 19:12:52 2016 +0900 +++ b/src/main/java/alice/topology/manager/StartTopologyManager.java Wed Apr 27 16:28:10 2016 +0900 @@ -96,15 +96,18 @@ ods.put("running", true); HashMap<String, HostMessage> nameTable = new HashMap<String, HostMessage>(); - + int cominghostCount = 0; if (conf.type == TopologyType.Tree) { - int cominghostCount = 0; ParentManager manager = new ParentManager(conf.hasChild); ods.put("parentManager", manager); ods.put("nameTable", nameTable); ods.put("hostCount", cominghostCount); new ComingServiceHosts(); new ReceiveDisconnectMessage(); + } else if (conf.type == TopologyType.Star){ + ods.put("nameTable", nameTable); + ods.put("hostCount", cominghostCount); + new ComingStarHosts(); } }
--- a/src/main/java/alice/topology/manager/TopologyType.java Tue Feb 16 19:12:52 2016 +0900 +++ b/src/main/java/alice/topology/manager/TopologyType.java Wed Apr 27 16:28:10 2016 +0900 @@ -1,13 +1,14 @@ package alice.topology.manager; public enum TopologyType { - Tree(1); + Tree(1), Star(2); private final int id; private TopologyType(final int id) { this.id = id; } + public int getId() { return id; }
--- a/src/main/java/alice/topology/node/IncomingConnectionInfo.java Tue Feb 16 19:12:52 2016 +0900 +++ b/src/main/java/alice/topology/node/IncomingConnectionInfo.java Wed Apr 27 16:28:10 2016 +0900 @@ -39,7 +39,6 @@ ods.put("cMember", hostInfo.connectionName); new CreateConnectionList(); } - } IncomingConnectionInfo cs = new IncomingConnectionInfo(absName, count, topMNumber);