id
stringlengths
36
36
text
stringlengths
1
1.25M
0df9e452-fa01-48fb-8058-ab91b86f2376
@Override public boolean equals(final Object obj) { if (this == obj) { return true; } if (obj == null) { return false; } if (getClass() != obj.getClass()) { return false; } final Exam other = (Exam) obj; if (mark == null) { if (other.mark != null) { return false; } } else if (!mark.equals(other.mark)) { return false; } if (name == null) { if (other.name != null) { return false; } } else if (!name.equals(other.name)) { return false; } return true; }
4ec902a9-aef1-47c8-a94c-81267786bd6e
public Integer getMark() { return mark; }
9bede27f-539b-40d0-a490-5ecc8ec33795
public String getName() { return name; }
1069c362-ae20-41bd-9008-f331ba5f04f7
@Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((mark == null) ? 0 : mark.hashCode()); result = prime * result + ((name == null) ? 0 : name.hashCode()); return result; }
ace62fdd-decb-4859-878a-38d14ac5e07c
public boolean isEmpty() { return (name == null && mark == null) || "" == name; }
2f44b158-70a9-4605-ad39-2ceec8b1d6af
public void openXML(final File file, final Model model) { final Document doc = parseForDOM(file); final List<Student> students = parse(doc); model.setStudents(students); }
89dd5bb9-bb94-4656-aacb-dbf559d6b241
private List<Student> parse(final Document doc) { final List<Student> students = new Vector<Student>(); if (doc == null) { return students; } final Element root = doc.getDocumentElement(); final NodeList nodeStudents = root.getChildNodes(); if (nodeStudents != null) { if (nodeStudents.getLength() != 0) { for (int i = 0; i < nodeStudents.getLength(); ++i) { final Node nodeStudent = nodeStudents.item(i); if (nodeStudent != null) { if (nodeStudent.getNodeType() == Node.ELEMENT_NODE) { final Student student = parseStudent(nodeStudent); students.add(student); } } } } } return students; }
a004825c-beb7-4b63-a23f-48b42ba55afe
private Exam parseExam(final Node exam) { String name = ""; String mark = ""; final NodeList fields = exam.getChildNodes(); for (int i = 0; i < fields.getLength(); ++i) { final Node field = fields.item(i); if (field != null) { if (field.getNodeType() == Node.ELEMENT_NODE) { final Node item = field.getChildNodes().item(0); if (item != null) { if (field.getNodeName() == Model.FIELD_NAME) { name = item.getNodeValue(); } if (field.getNodeName() == Model.FIELD_MARK) { mark = item.getNodeValue(); } } } } } return new Exam(name, Util.isNumeric(mark) ? Integer.parseInt(mark) : null); }
ececb525-27f4-4ec6-9da3-2359b25dd4bf
private Document parseForDOM(final File docFile) { Document document = null; try { final DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory .newInstance(); documentBuilderFactory.setValidating(true); final DocumentBuilder documentBuilder = documentBuilderFactory .newDocumentBuilder(); documentBuilder.setErrorHandler(new MyErrorHandler()); document = documentBuilder.parse(docFile); return document; } catch (Exception e) { XMLReader.LOG.log(Level.SEVERE, XMLReader.PROBLEM_PARSING_THE_FILE + e.getMessage(), e); e = null; return null; } }
730d046f-27ce-401f-9e15-6d5212b79bc3
private Student parseStudent(final Node nodeStudent) { boolean quantityError = false; String name = ""; String group = ""; final Exam[] exams = new Exam[Desktop.EXAMS_COUNT]; int index = 0; final NodeList fields = nodeStudent.getChildNodes(); if (fields != null) { for (int i = 0; i < fields.getLength(); ++i) { final Node field = fields.item(i); if (field != null) { if (field.getNodeType() == Node.ELEMENT_NODE) { final NodeList childFields = field.getChildNodes(); if (childFields.getLength() == 1) { final Node item = childFields.item(0); if (item != null) { if (field.getNodeName() == Model.FIELD_NAME) { name = item.getNodeValue(); } if (field.getNodeName() == Model.FIELD_GROUP) { group = item.getNodeValue(); } } } else { int counter = 0; for (int j = 0; j < childFields.getLength(); ++j) { final Node examField = childFields.item(j); if (examField != null) { if (examField.getNodeType() == Node.ELEMENT_NODE) { if (counter < Desktop.EXAMS_COUNT) { final Exam exam = parseExam(examField); exams[index] = exam; index++; counter++; } else { quantityError = true; } } } } } } } } } for (; index < Desktop.EXAMS_COUNT; ++index) { exams[index] = new Exam("", null); } if (quantityError) { JOptionPane.showMessageDialog(null, XMLReader.ERROR_INCORRECT_QUANTITY_IN_EXAMS + "(>" + Desktop.EXAMS_COUNT + ") of student " + name); } return new Student(name, Util.isNumeric(group) ? Integer.parseInt(group) : null, exams); }
e3477520-b956-4d3c-a9d4-d4946eb9dbba
public static void main(final String[] args) { try { LogManager.getLogManager().readConfiguration( Start.class.getResourceAsStream("/logging.properties")); final Window window = new Window(); window.setVisible(true); } catch (final IOException e) { System.err.println("Could not setup logger configuration: " + e.toString()); } }
0890b8c1-eb97-41d8-8db2-59831ff0373e
public Server() { model = new Model(); }
3c7f4f12-0dba-4db6-b8a9-cdb579096f0f
public Server(final Model model) { this.model = model; }
33a9bb8e-599d-4a9f-9f35-32c4d32da0ad
private final Object getFirstElementIfClassEqualent(final Class inputClass, final List<Object> objects) { if (objects.size() != 0) { final Object object = objects.get(0); if (isObjectClassEqualent(object, inputClass)) { return object; } } return null; }
c14730ed-dca0-489a-ace8-3e0dc58d44fb
private String getString(final List<Object> objects, final int num, String name) { if (num >= 0 && num < objects.size()) { final Object object = objects.get(num); if (isObjectClassEqualent(objects, String.class)) { name = (String) object; } } return name; }
369b60e5-684b-44bb-9f25-7a3fbcb92b62
private boolean isEqualentClasses(final Class class1, final Class class2) { return class1 == class2; }
3e689903-3d5f-4ec5-8b6b-f18245fd8840
private boolean isObjectClassEqualent(final Object object, final Class inputClass) { return object != null && isEqualentClasses(object.getClass(), inputClass); }
6f9179d7-9703-4241-863b-38c6234ca363
public void open() { try { server = ServerSocketChannel.open(); print("Server: start"); server.configureBlocking(true); final int port = 12345; server.socket().bind(new InetSocketAddress(port)); print("Server: port " + port); } catch (final IOException e) { print("Server: Unable to open port"); } }
f9c5324d-24ef-48b8-8f62-5cf90d7dd71d
private void print(final String text) { Server.LOG.info(text); System.out.println(text); }
582258e1-d78c-4469-a4fc-2154375cd1dd
public void read() { SocketChannel client; try { print("Server: waiting for connection..."); client = server.accept(); } catch (final IOException e) { print("Server: can't accept"); return; } ObjectOutputStream oos = null; ObjectInputStream ois = null; try { oos = new ObjectOutputStream(client.socket().getOutputStream()); ois = new ObjectInputStream(client.socket().getInputStream()); } catch (final IOException e) { print("Server: can't get streams"); return; } Object obj, object; List<Object> objects; List<Student> students; Integer viewSize; String name, topStr, botStr; Integer group; while (true) { try { obj = ois.readObject(); if (obj != null) { if (isEqualentClasses(obj.getClass(), Package.class)) { final Package pack = (Package) obj; final Mode mode = pack.getMode(); switch (mode) { case ADD_STUDENT: print("Client: addStudent"); objects = pack.getObjects(); if ((object = getFirstElementIfClassEqualent( Student.class, objects)) != null) { final Student student = (Student) object; model.addStudent(student); print("Server: add student " + student.getName()); } break; case DELETE_STUDENTS: print("Client: deleteStudents"); objects = pack.getObjects(); students = new ArrayList<Student>(); for (final Object studObject : objects) { if (isEqualentClasses(studObject.getClass(), Student.class)) { final Student student = (Student) studObject; students.add(student); } } model.deleteStudents(students); print("Server: delete " + students.size() + " students"); break; case GET_CURR_PAGE: print("Client: getCurrPage"); students = model.getCurrPageOfStudent(); sendPackage(oos, Mode.GET_CURR_PAGE, students); print("Server: send " + students.size() + " students"); break; case GET_NEXT_PAGE: print("Client: getNextPage"); students = model.getNextPageOfStudents(); sendPackage(oos, Mode.GET_NEXT_PAGE, students); print("Server: send " + students.size() + " students"); break; case GET_PREV_PAGE: print("Client: getPrevPage"); students = model.getPrevPageOfStudents(); sendPackage(oos, Mode.GET_PREV_PAGE, students); print("Server: send " + students.size() + " students"); break; case GET_STUDENTS_COUNT: print("Client: getStudentsCount"); final Integer studentsCount = model.getStudentsCount(); sendPackage(oos, Mode.GET_STUDENTS_COUNT, studentsCount); print("Server: studentsCount = " + studentsCount); break; case GET_VIEWSIZE: print("Client: getViewSize"); viewSize = model.getViewSize(); sendPackage(oos, Mode.GET_VIEWSIZE, viewSize); print("Server: viewSize = " + viewSize); break; case GET_FILES_LIST: print("Client: getFileList"); sendPackage(oos, Mode.GET_FILES_LIST, Files.getObjectKeys()); print("Server: send " + Files.size() + " files"); break; case LEAF_NEXT_PAGE: print("Client: leafNext"); model.leafNext(); print("Server: leafNext"); break; case LEAF_PREV_PAGE: print("Client: leafPrev"); model.leafPrev(); print("Server: leafPrev"); break; case OPEN_FILE: print("Client: openFile"); objects = pack.getObjects(); if ((object = getFirstElementIfClassEqualent( String.class, objects)) != null) { String fileName = (String) object; print("Server: open path " + fileName); fileName = Files.getAddress(fileName); if (fileName != null) { model.openXML(new File(fileName)); } } break; case SAVE_FILE: print("Client: saveFile"); objects = pack.getObjects(); if ((object = getFirstElementIfClassEqualent( String.class, objects)) != null) { final String fileName = (String) object; final String path = Server.DRIVE_C + File.separator + fileName; model.saveXML(new File(path)); print("Server: save to " + path); Files.addFile(fileName, path); } break; case SEARCH1: print("Client: search1"); name = botStr = topStr = ""; objects = pack.getObjects(); if (objects.size() != 0) { name = getString(objects, 0, name); botStr = getString(objects, 1, botStr); topStr = getString(objects, 2, topStr); students = model.search(name, botStr, topStr); objects = new ArrayList<Object>(); objects.addAll(students); } else { objects = new ArrayList<Object>(); } oos.writeObject(new Package(Mode.SEARCH1, objects)); break; case SEARCH2: print("Client: search2"); name = ""; group = null; objects = pack.getObjects(); if (objects.size() != 0) { name = getString(objects, 0, name); object = pack.getObjects().get(1); if (isObjectClassEqualent(object, Integer.class)) { group = (Integer) object; } students = model.search(name, group); objects = new ArrayList<Object>(); objects.addAll(students); } else { objects = new ArrayList<Object>(); } oos.writeObject(new Package(Mode.SEARCH2, objects)); break; case SEARCH3: print("Client: search3"); name = botStr = topStr = ""; String examStr = ""; objects = pack.getObjects(); if (objects.size() != 0) { name = getString(objects, 0, name); examStr = getString(objects, 1, examStr); botStr = getString(objects, 2, botStr); topStr = getString(objects, 3, topStr); students = model .search(name, examStr, botStr, topStr); objects = new ArrayList<Object>(); objects.addAll(students); } else { objects = new ArrayList<Object>(); } oos.writeObject(new Package(Mode.SEARCH3, objects)); break; case SET_VIEWSIZE: print("Client: setViewSize"); objects = pack.getObjects(); if ((object = getFirstElementIfClassEqualent( Integer.class, objects)) != null) { viewSize = (Integer) object; model.setViewSize(viewSize); print("Server: new viewSize = " + viewSize); } break; default: print("Client: unknown command"); break; } } } } catch (final IOException e) { print("Server: Connection lost"); break; } catch (final ClassNotFoundException e) { print("ClassnotFoundException*****"); e.printStackTrace(); print("***************************"); break; } } try { oos.close(); ois.close(); } catch (final IOException e) { print("Server: can't close streams"); } read(); }
d9739548-aa84-4887-bb4f-1a19bee619fb
private void sendPackage(final ObjectOutputStream oos, final Mode inputMode, final Integer num) throws IOException { List<Object> objects; objects = new ArrayList<Object>(); objects.add(num); oos.writeObject(new Package(inputMode, objects)); }
084006d2-b82e-45ba-bdfb-f271349e7e2a
private void sendPackage(final ObjectOutputStream oos, final Mode inputMode, final List inputObjects) throws IOException { final List<Object> objects = new ArrayList<Object>(); objects.addAll(inputObjects); oos.writeObject(new Package(inputMode, objects)); }
84e17577-dbf2-4b97-838b-ed482c5f7292
public static boolean isNumeric(final String str) { if (str.length() == 0) { return false; } final NumberFormat formatter = NumberFormat.getInstance(); final ParsePosition pos = new ParsePosition(0); formatter.parse(str, pos); return str.length() == pos.getIndex(); }
811f8878-578a-4dc5-b23b-dde70e28103e
public Desktop(final ContentPane contentPane) { this.contentPane = contentPane; initialize(); }
43f5d177-684a-459d-b302-d52b7978f034
public void addStudent(final Student student) { if (student != null) { model.addStudent(student); final List<Student> pageOfStudents = model.getCurrPageOfStudent(); } }
c976366a-ad7e-467a-8318-6ce5163fad94
public void deleteStudents(final List<Student> students) { model.deleteStudents(students); }
c21c9882-c935-4c86-8141-0d6a4cca41ad
public void initialize() { setLayout(new BorderLayout(0, 0)); model = new Model(); openXML(new File("c:\\students.xml")); final Server server = new Server(model); server.open(); server.read(); }
bce9e4ac-5404-4e93-ae6d-201f066ee5a6
public void openXML(final File file) { model.openXML(file); }
7aa225cf-67c3-49bf-9b3d-ad428788c339
public void saveXML(final File file) { model.saveXML(file); }
e0f07f62-581c-4049-a93d-f7a1d51da3dc
public Vector<Student> search(final String name, final Integer group) { return model.search(name, group); }
43fe2630-55d8-4dcc-accb-97d56d261a11
public Vector<Student> search(final String name, final String botStr, final String topStr) { return model.search(name, botStr, topStr); }
47bb9068-a8bb-40c2-a1fb-0cf94970e63f
public Vector<Student> search(final String name, final String examStr, final String botStr, final String topStr) { return model.search(name, examStr, botStr, topStr); }
049cbb8f-0109-43ca-9339-f49c11f714a5
public Window() { super(Window.TITLE); initialize(); }
b2effd4e-ce07-4ad2-826f-f757ebbcbb66
public Window(final String title) { super(title); initialize(); }
f120b9e7-c3c0-4487-a418-080f58b38c14
public void exit() { setVisible(false); dispose(); }
de762d6d-8089-481b-a0f2-e74f63fb4b90
public final ContentPane getContPane() { return contentPane; }
ef34fb8d-53bb-42a0-873d-c7bf062fccb8
private void initialize() { contentPane = new ContentPane(this); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setContentPane(contentPane); setBounds(Window.defaultX, Window.defaultY, Window.defaultWidth, Window.defaultHeight); }
f6b66fb2-8f40-41c7-a6a6-5ced0c76316c
public ContentPane(final JFrame parent) { this.parent = parent; initialize(); }
2e504836-b609-4703-ad8f-0b64565c2376
public void about() { JOptionPane.showMessageDialog(null, ContentPane.ABOUT_AUTHOR, ContentPane.ABOUT_TITLE, JOptionPane.INFORMATION_MESSAGE); }
f71653ce-e615-48b3-b1cb-aa03e57cc45e
public void exit() { ((Window) parent).exit(); }
6833c94d-5902-4c83-8c7c-3e843c110d34
private void initialize() { setLayout(new BorderLayout(0, 0)); desktop = new Desktop(this); desktop.setBorder(BorderFactory.createLineBorder(Color.BLACK)); add(desktop, BorderLayout.CENTER); }
20f285c2-9a9e-4fcf-b5d2-45aad2399cd4
Mode(final int mode) { this.mode = mode; }
2f8e96a0-e6d0-45fd-b178-709e18fefe00
public final int getMode() { return mode; }
7615f7cd-ace0-4cc9-bbcd-626826a6d59b
public Package() { objects = new ArrayList<Object>(); }
1653c574-dcc4-489c-84ea-5307535925f7
public Package(final List<Object> objects) { this.objects = new ArrayList<Object>(); this.objects.addAll(objects); }
3da4c16d-6121-447b-b70c-6ff21ce7e574
public Package(final Mode mode) { setMode(mode); objects = new ArrayList<Object>(); }
eae0d965-5985-41ec-b010-e2fa72f78b9f
public Package(final Mode mode, final List<Object> objects) { setMode(mode); this.objects = new ArrayList<Object>(); this.objects.addAll(objects); }
c049d8ed-b684-4811-a915-83d090ebac82
public final Mode getMode() { return mode; }
48ac9068-5bfe-437a-8f9d-13be622496d7
public final List<Object> getObjects() { return objects; }
2e58ade6-f480-4dc8-8151-a798bb7f185a
public void setMode(final Mode mode) { if (mode != null) { this.mode = mode; } else { this.mode = Mode.EMPTY; System.out.println("class Package. setMode. input: mode == null"); } }
c41b24c7-52e2-4124-9619-5b7bb0d95f37
public void setObjects(final List<Object> objects) { this.objects.clear(); this.objects.addAll(objects); }
a186dc7a-8d78-4445-bbbe-04bfe8b6cb8b
public Spacebrew(SpacebrewClient client) { this.client = client; publishes = new ArrayList<SpacebrewMessage>(); subscribes = new ArrayList<SpacebrewMessage>(); callbacks = new HashMap<String, HashMap<String, Method>>(); //*//parent.registerMethod("pre", this); // substituted with the ReconnectTask below ReconnectTask rt = new ReconnectTask(this); timer.schedule(rt, 1000, reconnectInterval); setupMethods(); }
89edebf8-e22b-44d7-8836-fc366a64fd58
ReconnectTask(Spacebrew sb) { this.sb = sb; }
08ed3fad-f132-45e9-b2b4-1927c4547af1
@Override public void run() { if(!connected()) { sb.connect(sb.hostname, sb.port, sb.name, sb.description); } }
2e2e5bde-36db-42c5-99c4-5e15b52d3867
private void setupMethods() { try { onOpenMethod = getClass().getMethod("onSbOpen", new Class[]{}); } catch(Exception e) { //let's not print these messages: they confuse ppl and make them think they are doing something wrong //System.out.println("no onSbOpen method implemented"); } try { onCloseMethod = getClass().getMethod("onSbClose", new Class[]{}); } catch(Exception e) { // System.out.println("no onSbClose method implemented"); } try { onRangeMessageMethod = getClass().getMethod("onRangeMessage", new Class[]{String.class, int.class}); } catch(Exception e) { //System.out.println("no onRangeMessage method implemented"); } try { onStringMessageMethod = getClass().getMethod("onStringMessage", new Class[]{String.class, String.class}); } catch(Exception e) { //System.out.println("no onStringMessage method implemented"); } try { onBooleanMessageMethod = getClass().getMethod("onBooleanMessage", new Class[]{String.class, boolean.class}); } catch(Exception e) { //System.out.println("no onBooleanMessage method implemented"); } try { onOtherMessageMethod = getClass().getMethod("onOtherMessage", new Class[]{String.class, String.class}); } catch(Exception e) { //System.out.println("no onCustomMessage method implemented"); } try { onCustomMessageMethod = getClass().getMethod("onCustomMessage", new Class[]{String.class, String.class, String.class}); } catch(Exception e) { //System.out.println("no onCustomMessage method implemented"); } }
43b2d41b-7152-4856-8370-1776ac7e3bb9
public void addPublish(String name, boolean _default) { SpacebrewMessage m = new SpacebrewMessage(); m.name = name; m.type = "boolean"; if(_default) { m._default = "true"; } else { m._default = "false"; } publishes.add(m); if(connectionEstablished) { updatePubSub(); } }
b9be6be4-187c-4526-9df1-9bbddf7441f5
public void addPublish(String name, Integer _default) { SpacebrewMessage m = new SpacebrewMessage(); m.name = name; m.type = "range"; m._default = _default.toString(); publishes.add(m); if(connectionEstablished) { updatePubSub(); } }
91fdb2dd-b7ff-4cb2-96f3-f80ef39c27e8
public void addPublish(String name, String _default) { SpacebrewMessage m = new SpacebrewMessage(); m.name = name; m.type = "string"; m._default = _default; publishes.add(m); if(connectionEstablished) { updatePubSub(); } }
127f0206-bcbe-47ba-86a8-4777fdec68a5
public void addPublish(String name, String type, String _default) { SpacebrewMessage m = new SpacebrewMessage(); m.name = name; m.type = type; m._default = _default; publishes.add(m); if(connectionEstablished) { updatePubSub(); } }
ae7f4f7a-39b6-4322-b2c1-42372a382c7e
public void addPublish(String name, String type, boolean _default) { SpacebrewMessage m = new SpacebrewMessage(); m.name = name; m.type = type; m._default = Boolean.toString(_default); publishes.add(m); if(connectionEstablished) { updatePubSub(); } }
0aa40218-0d18-4433-b137-5c1a4e1f203b
public void addPublish(String name, String type, Integer _default) { SpacebrewMessage m = new SpacebrewMessage(); m.name = name; m.type = type; m._default = _default.toString(); publishes.add(m); if(connectionEstablished) { updatePubSub(); } }
5c1d045a-e693-4d09-9089-ea9b0c387d22
public void addSubscribe(String name, String type) { String methodName; SpacebrewMessage m = new SpacebrewMessage(); m.name = name; m.type = type.toLowerCase(); subscribes.add(m); //*// added handling Method method = null; if(type.equals("boolean")) { methodName = "onBooleanMessage"; try { method = client.getClass().getMethod(methodName, new Class[]{boolean.class}); } catch(Exception e) { System.err.println("method " + methodName + "(boolean) doesn't exist in your client."); System.err.println(e); } } else if(type.equals("range")) { methodName = "onRangeMessage"; try { method = client.getClass().getMethod(methodName, new Class[]{int.class}); } catch(Exception e) { System.err.println("method " + methodName + "(int) doesn't exist in your client."); System.err.println(e); } } else if(type.equals("string")) { methodName = "onStringMessage"; try { method = client.getClass().getMethod(methodName, new Class[]{int.class}); } catch(Exception e) { System.err.println("method " + methodName + "(String) doesn't exist in your client."); System.err.println(e); } } if(method != null) { if(!callbacks.containsKey(name)) { callbacks.put(name, new HashMap<String, Method>()); } callbacks.get(name).put(type, method); } if(connectionEstablished) { updatePubSub(); } }
5046bd78-6a53-4e8c-ba1d-ee354651079a
public void addSubscribe(String name, String type, String methodName) { SpacebrewMessage m = new SpacebrewMessage(); m.name = name; m.type = type.toLowerCase(); subscribes.add(m); //*// all client.getClass()... methods below were parent.client.getClass() Method method = null; if(type.equals("boolean")) { try { method = client.getClass().getMethod(methodName, new Class[]{boolean.class}); } catch(Exception e) { System.err.println("method " + methodName + "(boolean) doesn't exist in your client."); } } else if(type.equals("range")) { try { method = client.getClass().getMethod(methodName, new Class[]{int.class}); } catch(Exception e) { System.err.println("Error: method " + methodName + "(int) doesn't exist in your client."); } } else if(type.equals("string")) { try { method = client.getClass().getMethod(methodName, new Class[]{String.class}); } catch(Exception e) { System.err.println("Error: method " + methodName + "(String) doesn't exist in your client."); } } else { try { method = client.getClass().getMethod(methodName, new Class[]{String.class}); } catch(Exception e) { System.err.println("Error: method " + methodName + "(String) doesn't exist in your client."); } } if(method != null) { if(!callbacks.containsKey(name)) { callbacks.put(name, new HashMap<String, Method>()); } callbacks.get(name).put(type, method); } if(connectionEstablished) { updatePubSub(); } }
d3dad0a7-e913-45e6-834b-0e41a736f158
public void connect(String hostname, String name, String description) { Integer port = 9000; //*// commented just for simplicity now /* String[][] m = matchAll(hostname, "ws://((?:[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9])(?:\\.(?:[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9]))*):([0-9]{1,5})"); if (m != null) { if (m[0].length == 3) { hostname = m[0][1]; port = Integer.parseInt(m[0][2]); this.connect(hostname, port, _name, _description); System.err.println("Using a full websockets URL will be deprecated in future versions of the Spacebrew lib."); System.err.println("Pass just the host name or call the connect(host, port, name, description) instead"); } else { System.err.println("Spacebrew server URL is not valid."); } } else { this.connect(hostname, port, _name, _description); } */ this.connect(hostname, port, name, description); //*// }
1cae75e4-de39-4eac-aead-379b9ba01a61
public void connect(String hostname, Integer port, String name, String description) { this.name = name; this.description = description; this.hostname = hostname; this.port = port; //*//this.connectionRequested = true; try { if(verbose) { System.out.println("[connect] connecting to spacebrew "+ hostname); } wsClient = new WsClient(this, ("ws://" + hostname + ":" + Integer.toString(port))); wsClient.connect(); updatePubSub(); } catch(Exception e) { connectionEstablished = false; System.err.println(e.getMessage()); } }
f83f5efd-81bb-4596-9db1-b362e20a90df
public void close() { if(connectionEstablished) { wsClient.close(); } //*//connectionRequested = false; }
b4d36588-9dc5-410b-88da-fe267d8f6ac1
private void updatePubSub() { JSONArray publishers = new JSONArray(); for(int i = 0, len = publishes.size(); i < len; i++) { SpacebrewMessage m = publishes.get(i); JSONObject pub = new JSONObject(); pub.put("name", m.name); pub.put("type", m.type); pub.put("default", m._default); publishers.put(pub); } JSONArray subscribers = new JSONArray(); for(int i = 0; i < subscribes.size(); i++) { SpacebrewMessage m = subscribes.get(i); JSONObject subs = new JSONObject(); subs.put("name", m.name); subs.put("type", m.type); subscribers.put(subs); } JSONObject mObj = new JSONObject(); JSONObject tMs1 = new JSONObject(); JSONObject tMs2 = new JSONObject(); tMs1.put("messages", subscribers); tMs2.put("messages", publishers); mObj.put("name", name); mObj.put("description", description); mObj.put("subscribe", tMs1); mObj.put("publish", tMs2); tConfig.put("config", mObj); if(connectionEstablished) { wsClient.send(tConfig.toString()); } }
20cb5e9e-7b44-4214-ae95-2a1b7b23a5a2
public void send(String messageName, String type, String value) { JSONObject m = new JSONObject(); m.put("clientName", name); m.put("name", messageName); m.put("type", type); m.put("value", value); JSONObject sM = new JSONObject(); sM.put("message", m); if(connectionEstablished) { wsClient.send( sM.toString()); } else { System.err.println("[send] can't send message, not currently connected!"); } }
fca98844-f4ca-462b-942a-584bfffe546c
public void send(String messageName, int value) { String type = "range"; for(int i = 0, len = publishes.size(); i < len; i++) { SpacebrewMessage m = publishes.get(i); if(m.name.equals(messageName)) { type = m.type; break; } } this.send(messageName, type, Integer.toString(value)); }
03af6187-c2f7-40a7-8c56-bc2938ece125
public void send(String messageName, boolean value) { String type = "boolean"; for(int i = 0, len = publishes.size(); i < len; i++) { SpacebrewMessage m = publishes.get(i); if(m.name.equals(messageName)) { type = m.type; break; } } this.send(messageName, type, Boolean.toString(value)); }
b44d41b5-a32e-40d7-8064-9e65824a5a64
public void send(String messageName, String value) { String type = "string"; for(int i = 0, len = publishes.size(); i < len; i++) { SpacebrewMessage m = publishes.get(i); if(m.name.equals(messageName)) { type = m.type; break; } } this.send(messageName, type, value); }
b7016dc2-598b-4fd0-a15f-62fc00515cbd
public boolean connected() { return connectionEstablished; }
b155f3e6-2d5e-4328-8c59-20418e37c3ca
public void onOpen() { connectionEstablished = true; if(verbose) { System.out.println("[onOpen] spacebrew connection open!"); } wsClient.send(tConfig.toString()); // send config //*// /* if ( onOpenMethod != null ){ try { onOpenMethod.invoke( parent ); } catch( Exception e ){ System.err.println("[onOpen] invoke failed, disabling :("); onOpenMethod = null; } } */ }
a1b6b5ca-38e7-4c5c-a902-79489b1429c7
public void onClose() { //*// /* if ( onCloseMethod != null ){ try { onCloseMethod.invoke( parent ); } catch( Exception e ){ System.err.println("[onClose] invoke failed, disabling :("); onCloseMethod = null; } } */ connectionEstablished = false; if(verbose) { System.out.println("[onClose] spacebrew connection closed."); } }
7c5e4063-811d-49af-8ef5-442c29200223
public void onMessage(String message) { JSONObject m = new JSONObject(message).getJSONObject("message"); String name = m.getString("name"); String type = m.getString("type"); Method method = null; if(callbacks.containsKey(name)) { if(callbacks.get(name).containsKey(type)) { try { method = callbacks.get(name).get(type); } catch(Exception e) {} } } //*// all method.invoke(client, ...) methods below were method.invoke(this, ...) if(type.equals("string")) { if(method != null) { try { method.invoke(client, m.getString("value")); } catch(Exception e) { System.err.println("[" + method.getName() + "] invoke failed."); } } else if(onStringMessageMethod != null) { try { onStringMessageMethod.invoke(client, name, m.getString("value")); } catch(Exception e) { System.err.println("[onStringMessageMethod] invoke failed, disabling :("); onStringMessageMethod = null; } } } else if(type.equals("boolean")) { if(method != null) { try { method.invoke(client, m.getBoolean("value")); } catch(Exception e) { System.err.println("[" + method.getName() + "] invoke failed."); } } else if(onBooleanMessageMethod != null) { try { onBooleanMessageMethod.invoke(client, name, m.getBoolean("value")); } catch(Exception e) { System.err.println("[onBooleanMessageMethod] invoke failed, disabling :("); onBooleanMessageMethod = null; } } } else if(type.equals("range")) { if(method != null) { try { method.invoke(client, m.getInt("value")); } catch(Exception e) { System.err.println("[" + method.getName() + "] invoke failed."); } } else if(onRangeMessageMethod != null) { try { onRangeMessageMethod.invoke(client, name, m.getInt("value")); } catch(Exception e) { System.err.println("[onRangeMessageMethod] invoke failed, disabling :("); onRangeMessageMethod = null; } } } else { if(method != null) { try { method.invoke(client, m.getString("value")); } catch(Exception e) { System.err.println("[" + method.getName() + "] invoke failed."); } } else { if(onCustomMessageMethod != null) { try { onCustomMessageMethod.invoke(client, name, type, m.getString("value")); } catch(Exception e) { System.err.println("[onCustomMessageMethod] invoke failed, disabling :("); onCustomMessageMethod = null; } } if(onOtherMessageMethod != null) { try { onOtherMessageMethod.invoke(client, name, type, m.getString("value")); System.err.println("[onOtherMessageMethod] will be deprecated in future version of Spacebrew lib"); } catch(Exception e) { System.err.println("[onOtherMessageMethod] invoke failed, disabling :("); onOtherMessageMethod = null; } } } } }
e26b2218-3bce-4f57-9307-d21181e5a7d8
public WsClient( Object app, URI serverUri, Draft draft ) { super( serverUri, draft ); parent = app; setupMethods(); }
c5dcfaaa-8d29-44f3-94cd-f0628a711f57
public WsClient( Object app, URI serverURI ) { super( serverURI ); parent = app; setupMethods(); }
5c7efd1a-999e-4b55-8915-d466cb48bd24
public WsClient( Object app, String url ) throws URISyntaxException { super( new URI( url )); parent = app; setupMethods(); }
9c9e44ed-593a-4d75-a3fa-0e9e8141c741
public WsClient( Object app, String server, int port ) throws URISyntaxException { super( new URI( server +":"+port )); parent = app; setupMethods(); }
ab15d1a3-320b-4684-869c-d609eca8b66a
private void setupMethods() { try { onOpenMethod = parent.getClass().getMethod("onOpen", new Class[] { } ); } catch (Exception e) { } try { onCloseMethod = parent.getClass().getMethod("onClose", new Class[] { } ); } catch (Exception e) { } try { onMessageMethod = parent.getClass().getMethod("onMessage", new Class[] { String.class } ); } catch (Exception e) { } }
7df3ddd5-73ac-4311-8eb1-5f81a9c74d75
@Override public void onOpen( ServerHandshake handshakedata ) { if ( onOpenMethod != null ) { try { onOpenMethod.invoke( parent ); } catch( Exception e ) { System.err.println("onOpen invoke failed, disabling :("); onOpenMethod = null; } } }
6b4dd265-306c-44fd-8049-3367e87c4ccf
@Override public void onMessage( String message ) { if ( onMessageMethod != null ) { try { onMessageMethod.invoke( parent, message); } catch( Exception e ) { System.err.println("onMessage invoke failed, disabling :("); onMessageMethod = null; } } }
7e4cb86a-0e93-434f-bf6b-8c55a9fb9585
@Override public void onClose( int code, String reason, boolean remote ) { if ( onCloseMethod != null ) { try { onCloseMethod.invoke( parent ); } catch( Exception e ) { System.err.println("onClose invoke failed, disabling :("); onCloseMethod = null; } } }
743d9330-d955-438d-a1e3-587639265337
@Override public void onError( Exception ex ) { // System.err.println("on error :("); }
9073dbfd-1472-435c-8fac-10439a68313d
public FullExamplePub() { super("Publisher Example"); setBounds(100, 100, 300, 200); cl = new Spacebrew(this); cl.addPublish("a Boolean publisher", false); cl.addPublish("a range publisher", 0); cl.addPublish("a string publisher", ""); cl.addPublish("a custom publisher", "x,y", ""); cl.connect(hostname, "myfullpublisher", "A pure Java publisher"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container con = this.getContentPane(); con.add(pane); button.addMouseListener(this); slider.addChangeListener(this); text.addKeyListener(this); pane.addMouseMotionListener(this); text.setPreferredSize(new Dimension(200, 20)); pane.add(button); pane.add(slider); pane.add(text); setVisible(true); }
080146ac-bb0f-4108-8e5b-0d354f2c6065
@Override public void mousePressed(MouseEvent e) { cl.send("a Boolean publisher", true); }
aa5d0959-caf9-4df8-a691-576f7acb4083
@Override public void mouseReleased(MouseEvent e) { cl.send("a Boolean publisher", false); }
7e14c33b-2580-4726-a4ac-49ca3b692651
@Override public void stateChanged(ChangeEvent e) { cl.send("a range publisher", slider.getValue()); }
d53f8125-0897-4b4d-b0c7-9d8f13361841
@Override public void keyTyped(KeyEvent e) { cl.send("a string publisher", text.getText()); }
52163113-89a5-4425-a987-f5e39d57b5b5
@Override public void mouseMoved(MouseEvent e) { cl.send("a custom publisher", e.getX() + "," + e.getY()); }
fee7a8f4-bddb-44ad-b68d-b7ae518e28b0
public static void main(String[] args) { new FullExamplePub(); }
bf4ef72c-6582-4fc1-9808-001af8dc72f6
@Override public void mouseClicked(MouseEvent e) {}
a52d3829-49b4-43b8-a4c0-2475a49050ed
@Override public void mouseEntered(MouseEvent e) {}
d255ab8f-4229-4d49-b022-5a9d08855e5a
@Override public void mouseExited(MouseEvent e) {}
74be6ec1-2f29-42e0-a8dd-9838b1966717
@Override public void mouseDragged(MouseEvent e) {}
5b21b313-859f-46e1-9e85-84fe282e0074
@Override public void keyPressed(KeyEvent e) {}
7c6fd7e5-7f37-4046-9207-bf60b9cb7e48
@Override public void keyReleased(KeyEvent e) {}
026dd888-5356-4a16-ad43-cb7130ec9c2d
public ExampleSub() { super("Subscriber Example"); setBounds(100, 100, 300, 200); cl = new Spacebrew(this); cl.addSubscribe("a default Boolean subscriber", "boolean"); cl.addSubscribe("a Boolean subscriber", "boolean", "onReceive"); cl.connect(hostname, "mysubscriber", "A simple pure Java subscriber"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Container con = this.getContentPane(); con.add(pane); pane.add(label); setVisible(true); }
6bf16e7f-23ef-4e3a-b12c-df27a101fabb
public void onBooleanMessage(boolean value) { label.setText(value ? "click" : ""); }
e149011f-50c1-4534-a9f1-9d930c0be48f
public void onReceive(boolean value) { pane.setBackground(value ? Color.green : Color.lightGray); }