id
stringlengths
36
36
text
stringlengths
1
1.25M
2aaf4fd9-4194-4fc6-8649-6f50260e3ff3
public Word(String value, int noa, int aic, int ain, double pc, double pn) { super(); this.value = value; this.noa = noa; this.aic = aic; this.ain = ain; this.pc = pc; this.pn = pn; }
8ee9f4fb-0239-47fa-b9e5-2388503aa6bb
public WTmp(String value, int count) { super(); this.val = value; this.num = count; }
b390b9bb-14e8-4cbe-b87e-639562200835
public String getVal() { return val; }
4027672f-2f1f-41d2-beb1-a1c7fd7168c3
public void setVal(String val) { this.val = val; }
651a5cbf-3e8f-450f-a663-a4d8a9bca104
public int getNum() { return num; }
e9145e55-63cf-436c-80ff-d8062229b585
public void setNum(int num) { this.num = num; }
d18a7cd7-4a4a-4e56-b407-4ef783da4312
public static void show(String t) { System.out.println(t); }
ffb02eef-f8d9-4b16-b493-736b909f3fa0
public String getName() { return name; }
3e38866f-411a-40da-ae71-13d3cd26ce6e
public void setName(String name) { this.name = name; }
4f2c3f04-d854-4067-8074-f5030e90f3b9
public boolean isRealType() { return realType; }
6800a304-d1c9-4c8f-92bb-a1a4607826e6
public void setRealType(boolean realType) { this.realType = realType; }
5990a50d-a0ba-4973-9a4f-ea8beb1442bb
public boolean isPredictType() { return predictType; }
bc832d7e-1441-4d39-a19b-24a93bb89c74
public void setPredictType(boolean predictType) { this.predictType = predictType; }
c5a486e8-b7cc-4264-852e-e3c239f6cd00
public Result(String name, boolean realType, boolean predictType) { super(); this.name = name; this.realType = realType; this.predictType = predictType; }
69fedb82-4f94-4f68-a990-216fb7aeab13
public Algo() { countCC = countCN = countV = 0; resultSet = new ArrayList<Result>(); run(); returnResultSet(); if(NbcMain.isRun) { showResultSet(); } }
6ed1b433-3213-478d-8038-e05473a9c394
private void showResultSet(){ int i = 0; for (Result result : resultSet) { if(result.isRealType() == result.isPredictType()) { i++; } } double sucRate = Math.floor(((i * 1.0f)/resultSet.size()) * 100.0f)/100.0; NbcMain.listLogs.removeAll(); NbcMain.listLogs.add("ALGORITHM RESULT"); NbcMain.listLogs.add("-----------------------------------------"); NbcMain.listLogs.add("Successful Prediction: " + sucRate); NbcMain.listLogs.add("Unsuccessful Prediction: " + Math.round((1 - sucRate) * 100.0f)/100.0); NbcMain.listLogs.add("-----------------------------------------"); for (Result result : resultSet) { NbcMain.listLogs.add(result.getName() + " - " + result.isRealType() + " -> " + result.isPredictType()); } }
2bc285c5-9127-4fcb-a8c5-d7542041f893
private void showResultSetInCS() { for (Result result : resultSet) { InitCon.show(result.isRealType() + " " + result.isPredictType()); } }
07a3cec9-b706-4b42-9cf5-ed958ddedd88
private void run() { countCC = countC(); InitCon.show("countCC:" + countCC + ""); isConfirm = false; countCN = countC(); InitCon.show("countCN:" + countCN + ""); countV = countVf(); InitCon.show("countV: " + countV + ""); }
1baa43d8-0a62-48e3-b602-5b17a318a29f
private int countC() { int sum = 0; if (isConfirm) { for (WTmp w : WordCalcu.lConfirmed) { // InitCon.show("Con " + w.getVal() + " " + w.getNum()); sum += w.getNum(); } } else { for (WTmp w : WordCalcu.lNegative) { // InitCon.show("Neg " + w.getVal() + " " + w.getNum()); sum += w.getNum(); } } return sum; }
a5a86b3f-940b-4cbd-bdf4-00fc98a754b1
private int countVf() { int v = WordCalcu.lConfirmed.size() + WordCalcu.lNegative.size(); // InitCon.show(WordCalcu.lConfirmed.size() + " " + // WordCalcu.lNegative.size()); for (WTmp w1 : WordCalcu.lNegative) { for (WTmp w2 : WordCalcu.lConfirmed) { if (w1.getVal().equals(w2.getVal())) { // InitCon.show("Same: " + w1.getVal() + " " + w2.getVal()); v--; } } } // InitCon.show(v + ""); return v; }
e2ade120-765a-4db9-9129-a1a3c8edefc1
private void returnResultSet() { File path = new File(InitCon.PATH_TEST); File[] files = path.listFiles(); for (int i = 0; i < files.length; i++) { // duyet tung file // tra ve la thuoc hay ko? if (files[i].isFile()) { List<String> lstString = handleFile(files[i].getPath()); // tra ve ket qua trong resultSet List<Word> lstWord = new ArrayList<Word>(); initLstWord(lstWord, lstString); boolean r = choosePath(lstWord); boolean realType = Character.toString(files[i].getName().charAt(0)).equals("t") ? true : false; // InitCon.show(realType + ""); Result result = new Result(files[i].getName(), realType, r); resultSet.add(result); // break; } } }
5db6eb21-2abd-46b8-a420-4736e1002068
private List<String> handleFile(String path) { BufferedReader br = null; List<String> lstString = new ArrayList<String>(); try { String sCurrentLine; br = new BufferedReader(new FileReader(path)); while ((sCurrentLine = br.readLine()) != null) { splitLine(sCurrentLine, lstString); } } catch (IOException e) { e.printStackTrace(); } finally { try { if (br != null) br.close(); } catch (IOException ex) { ex.printStackTrace(); } } return lstString; }
8d1bce49-2c4d-4488-8f9a-4e0e5e604e6f
private void splitLine(String line, List<String> strs) { // InitCon.show(line); for (String txt : line.split(" ")) { if(txt.length() > 0) { strs.add(makeWordLower(txt)); } } }
08cdc921-691d-41ec-9ae1-2cf0d748de8d
private String makeWordLower(String word) { if (word.length() == 0) { return ""; } return word.toLowerCase(); }
5f433061-604a-4fae-b6fe-641dcee857f8
private void initLstWord(List<Word> lstWord, List<String> lstString) { for (String str : lstString) { boolean isIn = false; for (Word word : lstWord) { if (word.getValue().equals(str)) { word.setNoa(word.getNoa() + 1); isIn = true; } } if (!isIn) { Word w = new Word(str, 1, 0, 0, 0, 0); lstWord.add(w); } } for (Word word : lstWord) { for (WTmp wtmp : WordCalcu.lConfirmed) { if (word.getValue().equals(wtmp.getVal())) { word.setAic(wtmp.getNum()); } } for (WTmp wtmp : WordCalcu.lNegative) { if (word.getValue().equals(wtmp.getVal())) { word.setAin(wtmp.getNum()); } } } for (Word word : lstWord) { double s1 = (word.getAic() + 1 * 1.0f) / (countCC + countV); double s2 = (word.getAin() + 1 * 1.0f) / (countCN + countV); word.setPc(s1); word.setPn(s2); } // for (Word word : lstWord) { // InitCon.show(word.getValue() + " " + word.getNoa() + " " // + word.getAic() + " " + word.getAin() + " " + word.getPc() // + " " + word.getPn()); // } }
735f0240-494b-41ab-b301-1ab0b36894fa
private boolean choosePath(List<Word> lstWord) { double r1 = Math.log10(CateCalcu.cs.get(0).getRoi()); double r2 = Math.log10(CateCalcu.cs.get(1).getRoi()); // InitCon.show(r1 + " " + r2); for (Word word : lstWord) { // InitCon.show(word.getNoa() * Math.log10(word.getPc()) + " "); r1 += word.getNoa() * Math.log10(word.getPc()); r2 += word.getNoa() * Math.log10(word.getPn()); } // InitCon.show(r1 + " " + r2); return (r1 > r2) ? true : false; }
20ea9bf3-4b8b-41cd-8b05-d6d30667b0cd
public static void main(String[] args){ try { File fileDir = new File("input/confirmed/train1.txt"); BufferedReader in = new BufferedReader( new InputStreamReader( new FileInputStream(fileDir), "UTF8")); String str; while ((str = in.readLine()) != null) { System.out.println(str); } in.close(); } catch (UnsupportedEncodingException e) { System.out.println(e.getMessage()); } catch (IOException e) { System.out.println(e.getMessage()); } catch (Exception e) { System.out.println(e.getMessage()); } }
49f6c6c9-cb4b-4e14-afd5-aa220b1f47fa
public static void main(String[] args){ try { File fileDir = new File("input/confirmed/train1.txt"); Writer out = new BufferedWriter(new OutputStreamWriter( new FileOutputStream(fileDir), "UTF8")); out.append("Xin chào Việt Nam UTF-8").append("\r\n"); out.flush(); out.close(); } catch (UnsupportedEncodingException e) { System.out.println(e.getMessage()); } catch (IOException e) { System.out.println(e.getMessage()); } catch (Exception e) { System.out.println(e.getMessage()); } }
7331267d-c79a-4580-b5b8-394ab6d0a30b
public CateCalcu() { cs = new ArrayList<Cate>(); fc = new File(InitCon.PATH + InitCon.CONFIRMED); fn = new File(InitCon.PATH + InitCon.NEGATIVE); initCs(true); initCs(false); showCs(); // showCsUI(); }
041ecb7d-ac2b-44b2-98bc-d408f260be6b
private int countAllFile(){ return fc.listFiles().length + fn.listFiles().length; }
4b29de53-740f-41a2-bb95-74f5b52b892f
private void initCs(boolean isConfirmed){ int nof = 0; double rc = 0.0f; if(isConfirmed) { nof = fc.listFiles().length; rc = Math.round((1.0f * nof)/countAllFile() * 100.0)/ 100.0; Cate c = new Cate(InitCon.CONFIRMED, rc); cs.add(c); } else { nof = fn.listFiles().length; rc = Math.round((1.0f * nof)/countAllFile() * 100.0)/ 100.0; Cate c = new Cate(InitCon.NEGATIVE, rc); cs.add(c); } }
8243454b-a1e5-4d51-b05b-ef331de07ffe
private void showCs(){ for (Cate cate : cs) { InitCon.show(cate.getName() + " " + cate.getRoi()); } }
cff36708-33c5-4e3e-8eed-a3c0243665d2
private void showCsUI(){ NbcMain.listLogs.add("Calculate rate"); NbcMain.listLogs.add("-------------------------------"); for (Cate cate : cs) { NbcMain.listLogs.add(cate.getName() + " " + cate.getRoi()); } NbcMain.listLogs.add("-------------------------------"); }
2a3af3ec-afca-487e-9942-df9a9509e24c
public WordCalcu() { lConfirmed = new ArrayList<WTmp>(); lPreCon = new ArrayList<String>(); lNegative = new ArrayList<WTmp>(); lPreNeg = new ArrayList<String>(); entrance(); }
689f702e-533c-4bae-a555-42d90236805a
private void entrance() { // NbcMain.listLogs.add("Word Calculate"); // NbcMain.listLogs.add("-----------------------------"); readAllFile(InitCon.PATH + InitCon.CONFIRMED, true); initWT(lPreCon); readAllFile(InitCon.PATH + InitCon.NEGATIVE, false); initWT(lPreNeg); // NbcMain.listLogs.add("-----------------------------"); }
dde534e4-2bdb-4a88-8209-bd1302634a28
private void readAllFile(String path, boolean isCon) { isConfirm = isCon; File folder = new File(path); listFilesForFolder(folder); }
f564a09e-37f4-4b89-8a41-5ec526ddc817
private void listFilesForFolder(File folder) { for (File fileEntry : folder.listFiles()) { if (fileEntry.isDirectory()) { listFilesForFolder(fileEntry); } else { readFile(folder + "\\" + fileEntry.getName()); } } }
e3e2e5dc-e4c9-4617-acda-b8f1f7c4a38e
private void readFile(String path) { BufferedReader br = null; try { String sCurrentLine; br = new BufferedReader(new FileReader(path)); while ((sCurrentLine = br.readLine()) != null) { String tmpStr = makeWordLower(sCurrentLine); splitLine(tmpStr); } } catch (IOException e) { e.printStackTrace(); } finally { try { if (br != null) br.close(); } catch (IOException ex) { ex.printStackTrace(); } } }
7465755d-3643-4e95-ba8c-47427e42eac3
private String makeWordLower(String word) { if (word.length() == 0) { return ""; } return word.toLowerCase(); }
89928dab-7299-4719-aa17-6905c4f5437f
private void splitLine(String line) { for (String txt : line.split(" ")) { // String str = Character.toString(txt.charAt(txt.length() - 1)); // if (!str.matches("[a-zA-Z0-9]+")) { // txt = txt.replace(txt.substring(txt.length() - 1), ""); // } if (txt.length() > 0) { if (isConfirm == true) { lPreCon.add(txt); } else { lPreNeg.add(txt); } } } }
27c1d33b-729a-4df6-b533-ede6a4d41ce4
private void initWT(List<String> lst) { List<WTmp> lwt = (isConfirm) ? lConfirmed : lNegative; for (String str : lst) { boolean flags = false; for (WTmp wTmp : lwt) { if (wTmp.getVal().equals(str)) { wTmp.setNum(wTmp.getNum() + 1); flags = true; } } if (!flags) { WTmp w = new WTmp(str, 1); lwt.add(w); } } // for (WTmp wTmp : lwt) { // InitCon.show(wTmp.getVal() + " " + wTmp.getNum()); // } }
274192ea-32fc-4995-9861-ccc934892b5c
public static void main(String[] args) { try { Client client = Client.create(); WebResource webResource = client .resource("http://localhost:8080/services/api/people/put"); String input = "{\"id\":3,\"firstName\":\"Daniels\",\"lastName\":\"Horecki1\"}"; ClientResponse response = webResource.type(MediaType.APPLICATION_JSON) .put(ClientResponse.class, input); if (response.getStatus() != 201) { throw new RuntimeException("Failed : HTTP error code : " + response.getStatus()); } logger.info("Output from Server .... \n"); String output = response.getEntity(String.class); logger.info(output); } catch (Exception e) { logger.error(e.getMessage()); } }
2c26ecee-f72f-448a-a27e-ab6bfc45dd15
public static void main(String[] args) { try { Client client = Client.create(); WebResource webResource = client .resource("http://localhost:8080/services/api/people/"); String input = "{\"id\":3,\"firstName\":\"Daniel\",\"lastName\":\"Horecki2\"}"; ClientResponse response = webResource.type(MediaType.APPLICATION_JSON) .put(ClientResponse.class, input); if (response.getStatus() != 201) { throw new RuntimeException("Failed : HTTP error code : " + response.getStatus()); } logger.info("Output from Server .... \n"); String output = response.getEntity(String.class); logger.info(output); } catch (Exception e) { logger.error(e.getMessage()); } }
aa41c819-4242-46f5-9fdc-a15b7ee38d28
public static void main(String[] args) { try { Client client = Client.create(); WebResource webResource = client .resource("http://localhost:8080/services/api/people/8"); ClientResponse response = webResource.delete(ClientResponse.class); if (response.getStatus() != 201) { throw new RuntimeException("Failed : HTTP error code : " + response.getStatus()); } logger.info("Output from Server .... \n"); String output = response.getEntity(String.class); logger.info(output); } catch (Exception e) { logger.error(e.getMessage()); } }
cd7d287d-0ff2-4e81-a1c5-34352334dac0
public static void main(String[] args) { try { Client client = Client.create(); WebResource webResource = client .resource("http://localhost:8080/rest/json/metallica/get"); ClientResponse response = webResource.accept("application/json") .get(ClientResponse.class); if (response.getStatus() != 200) { throw new RuntimeException("Failed : HTTP error code : " + response.getStatus()); } String output = response.getEntity(String.class); logger.info("Output from Server .... \n"); logger.info(output); } catch (Exception e) { logger.error(e.getMessage()); } }
f0a312ce-b49f-4aa4-ae0f-87f59219fd2e
public static void main(String[] args) { try { Client client = Client.create(); WebResource webResource = client .resource("http://localhost:8080/services/api/people/delete/9"); ClientResponse response = webResource.delete(ClientResponse.class); if (response.getStatus() != 201) { throw new RuntimeException("Failed : HTTP error code : " + response.getStatus()); } logger.info("Output from Server .... \n"); String output = response.getEntity(String.class); logger.info(output); } catch (Exception e) { logger.error(e.getMessage()); } }
ba432e99-72f0-43a8-90c5-5e5741a5cfb1
public static void main(String[] args) { try { Client client = Client.create(); WebResource webResource = client .resource("http://localhost:8080/services/api/people/Witzka"); ClientResponse response = webResource.accept("application/json") .get(ClientResponse.class); if (response.getStatus() != 200) { throw new RuntimeException("Failed : HTTP error code : " + response.getStatus()); } String output = response.getEntity(String.class); logger.info("Output from Server .... \n"); logger.info(output); } catch (Exception e) { logger.error(e.getMessage()); } }
2a9e0b0b-c852-4269-86f7-4298c04042ab
public static void main(String[] args) { try { Client client = Client.create(); WebResource webResource = client .resource("http://localhost:8080/services/api/people/post"); String input = "{\"firstName\":\"Pawel\",\"lastName\":\"Sadowski\"}"; ClientResponse response = webResource.type("application/json") .post(ClientResponse.class, input); if (response.getStatus() != 201) { throw new RuntimeException("Failed : HTTP error code : " + response.getStatus()); } logger.info("Output from Server .... \n"); String output = response.getEntity(String.class); logger.info(output); } catch (Exception e) { logger.error(e.getMessage()); } }
f2749aa4-6183-4ec1-b45c-3a89cbc8a2aa
public String getUserFirstName(int userId)throws EJBException, RemoteException;
1e6567a5-4ccb-4bba-98ec-55c0705a14c9
public UserSessionFacadeEJBRemote create() throws RemoteException, CreateException;
04a66107-8133-472d-822e-5f57e54c0140
public void ejbCreate() throws CreateException { }
bd20ddaa-73c3-4031-bf50-22989edbfc42
public void ejbActivate() throws EJBException, RemoteException { }
863b084a-5623-46dd-8dce-614f3513d9ef
public void ejbPassivate() throws EJBException, RemoteException { }
c87fa3a1-d9d3-4ae4-a7ca-0e5b27c82d17
public void ejbRemove() throws EJBException, RemoteException { }
4a9c6370-85ad-4db4-ab5e-b2143a2afa25
public void setSessionContext(SessionContext ctx) throws EJBException, RemoteException { this.ctx = ctx; }
737f49c9-255f-4eb1-8f97-e706e8c74bb1
public String getUserFirstName(int userId) throws EJBException, RemoteException { return "Yathumagi"; }
7b9a01e8-2771-452e-9047-e424e14f7a35
public static void main(String ap[]) throws NamingException, EJBException, RemoteException{ //getInitialContext("t3://localhost:7001"); String nameFromEJB = new UserDelegate().getUserName(); System.out.println(" Name: "+ nameFromEJB); }
397783d8-eec5-4c9b-b39c-1d8f36a81107
public static InitialContext getInitialContext(String url) throws NamingException { Hashtable<String, String> env = new Hashtable<String, String>(); env.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY); env.put(Context.PROVIDER_URL, url); //env.put("weblogic.jndi.createIntermediateContexts", "true"); return new InitialContext(env); }
0b799019-3250-458b-ab4d-4f765f47e013
protected ServiceLocator() throws Exception { try { ic = getInitialContext(); } catch (Exception e) { throw new Exception(e.getMessage()); } }
06f80c3e-b095-46fb-ae29-94ee311f5994
public static synchronized ServiceLocator getInstance() throws Exception { if (serviceLocator == null) { serviceLocator = new ServiceLocator(); } return serviceLocator; }
b970f12d-5232-4253-a9a5-e4c41c4a4047
private InitialContext getInitialContext() throws Exception { try { //return new InitialContext(); return AppMain.getInitialContext("t3://localhost:7001"); } catch (Exception e) { throw new Exception("Unable to create InitialContext"); } }
261d91d9-0433-492e-9961-d3f863fbbbbf
public EJBHome getEjbHome(String ejbName, Class ejbClass) throws Exception { try { EJBHome ejbHome = null; Object object = ic.lookup(ejbName); ejbHome = (EJBHome) PortableRemoteObject.narrow(object, ejbClass); if (ejbHome == null) { throw new Exception("Could not get home for " + ejbName); } return ejbHome; } catch (NamingException ne) { throw new Exception(ne.getMessage()); } }
a04997bd-3d51-461e-b69a-56ff1f662626
public EJBLocalHome getEjbLocalHome(String ejbName) throws Exception { try { EJBLocalHome ejbLocalHome = null; Object object = ic.lookup(ejbName); ejbLocalHome = (EJBLocalHome) object; if (ejbLocalHome == null) { throw new Exception( "Could not get local home for " + ejbName); } return ejbLocalHome; } catch (NamingException ne) { throw new Exception(ne.getMessage()); } }
a34aec6f-1596-4002-a36b-969a2304e4f4
public UserDelegate(){ initializeAccountSessionFacadeEJBRemote(); }
c2e524af-a012-4703-9b35-9c87593754ff
private void initializeAccountSessionFacadeEJBRemote() throws RuntimeException { if (userSessionFacadeEJBHome == null) { String FACADE_NAME = "UserSessionFacadeEJB"; Class FACADE_CLASS = UserSessionFacadeEJB.class; try { ServiceLocator locator = ServiceLocator.getInstance(); userSessionFacadeEJBHome = (UserSessionFacadeEJBHome)locator.getEjbHome(FACADE_NAME, FACADE_CLASS); if (userSessionFacadeEJBHome == null) { System.out.println("userSessionFacadeEJBHome is Null"); throw new RuntimeException("Did not get home for " + FACADE_NAME); }else{ System.out.println("userSessionFacadeEJBHome is not Null"); } userSessionFacadeEJBRemote = userSessionFacadeEJBHome.create(); } catch (CreateException e) { throw new RuntimeException(e.getMessage()); } catch (RemoteException e) { throw new RuntimeException(e.getMessage()); } catch (Exception e) { throw new RuntimeException(e.getMessage()); } } }
8689059a-5cef-4918-80de-9ee87856350f
public String getUserName() throws EJBException, RemoteException{ String name = userSessionFacadeEJBRemote.getUserFirstName(1); return name; }
c7275a31-8562-4885-be71-9791f622e68e
public static void main(String[] args) throws UnknownHostException, IOException { String sentence; String modifiedSentence; BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in)); Socket clientSocket = new Socket("hostname", 6789); DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream()); BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); sentence = inFromUser.readLine(); outToServer.writeBytes(sentence + '\n'); modifiedSentence = inFromServer.readLine(); System.out.println ("FROM SERVER: " + modifiedSentence); clientSocket.close(); }
d06bebb0-30d3-46e3-abcc-579e4e695459
public static void main(String[] args) throws IOException { String clientSentence; String capitalizedSentence; ServerSocket welcomeSocket = new ServerSocket(6789); while(true) { Socket connectionSocket = welcomeSocket.accept(); BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream())); DataOutputStream outToClient =new DataOutputStream (connectionSocket.getOutputStream()); clientSentence = inFromClient.readLine(); capitalizedSentence = clientSentence.toUpperCase() + '\n'; outToClient.writeBytes(capitalizedSentence); } // welcomeSocket.close(); }
4fcf140b-b7f3-422a-8728-6543ff4b24b6
public Model() { //this.v = v; r = new Random(System.currentTimeMillis()); try { hostName = InetAddress.getLocalHost().getHostName(); this.ipAddress = InetAddress.getByName(hostName).getHostAddress(); if(this.ipAddress.startsWith("127")) { //Found the loopback address; need to test local adapters throw new UnknownHostException(); } System.out.println("Starting up client with hostname: " + hostName + " and IP " + ipAddress); } catch (UnknownHostException e) { try { ipAddress = this.getIPAddress(); } catch (SocketException e1) { e1.printStackTrace(); System.out.println("Failed to get IP address from both DNS lookup and local adapters, exiting..."); System.exit(-4); } System.out.println("Could not get hostname"); } }
2e48b022-fe02-4aa7-bc1a-c626dc2daaa3
public void joinServer(String userName) { try { Socket toServer = new Socket(server, directorySocket); ObjectOutputStream outToServer = new ObjectOutputStream(toServer.getOutputStream()); this.userName = userName; ServerMessage m = new ServerMessage(); m.setCommand(JOIN); m.setUser(new UserBean(this.hostName, userName, ipAddress)); outToServer.writeObject(m); } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
73a6ac28-93a2-4c74-92c1-988e873eb104
public void leaveServer() { try { Socket toServer = new Socket(server, directorySocket); ObjectOutputStream outToServer = new ObjectOutputStream(toServer.getOutputStream()); ServerMessage m = new ServerMessage(); m.setCommand(LEAVE); m.setUser(new UserBean(this.hostName,this.userName, ipAddress)); //Username could be null but shouldn't read it on server outToServer.writeObject(m); } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
277626f8-ec87-4636-aba3-890563f63551
public ArrayList<UserBean> listOnlineUsers() { try{ Socket toServer = new Socket(server,directorySocket); ObjectOutputStream outToServer = new ObjectOutputStream(toServer.getOutputStream()); ServerMessage m = new ServerMessage(); m.setCommand(LIST); outToServer.writeObject(m); ObjectInputStream serverInput = new ObjectInputStream(toServer.getInputStream()); onlineUsers = (ArrayList<UserBean>) serverInput.readObject(); outToServer.close(); serverInput.close(); toServer.close(); }catch(UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } return onlineUsers; }
3e38d8b8-09f5-4eb8-9482-2da3b93020bb
public boolean setServer(String server) { boolean validServer = false; if(server.length() > 0) { Socket toServer; try { this.server = server; System.out.println("Connecting to: " + server); toServer = new Socket(server,directorySocket); ObjectOutputStream outToServer = new ObjectOutputStream(toServer.getOutputStream()); ServerMessage m = new ServerMessage(); m.setCommand(TEST); outToServer.writeObject(m); ObjectInputStream serverInput = new ObjectInputStream(toServer.getInputStream()); String reply = (String) serverInput.readObject(); if(reply.equals("ACTIVE")) { validServer = true; System.out.println("Server replied; valid server"); } toServer.close(); } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } } return validServer; }
9f4837d0-083a-4a8e-8785-5873cd34f9f3
public void sendInvite(String userName) { String user = userName.split(": ")[1]; UserBean receiver = null; for(UserBean u: this.onlineUsers) { if(u.getUserName().equals(user)) { receiver = u; System.out.println(user); break; } } Socket controlSocket; try { System.out.println("Setting up connection to: " + receiver.getIpAddress()); controlSocket = new Socket(receiver.getIpAddress(), Model.controlDataSocketNumber); ObjectOutputStream toPeer = new ObjectOutputStream(controlSocket.getOutputStream()); ClientMessage c = new ClientMessage(); c.setCommand("INVITE"); c.setUser(new UserBean(this.hostName, this.userName, this.ipAddress)); this.piece = this.generatePiece(); c.setPiece(piece); this.opponent = receiver; toPeer.writeObject(c); controlSocket.close(); } catch (IOException e) { e.printStackTrace(); } }
3244e0f6-cc6a-4a65-b2b6-7518c67f00b0
public void acceptInvite(UserBean possibleOpponent, int possiblePiece) { this.opponent = possibleOpponent; this.piece = possiblePiece; Socket controlSocket; try { controlSocket = new Socket(opponent.getIpAddress(), Model.controlDataSocketNumber); ObjectOutputStream toPeer = new ObjectOutputStream(controlSocket.getOutputStream()); ClientMessage c = new ClientMessage(); c.setCommand("ACCEPT"); c.setUser(new UserBean(this.hostName, this.userName, this.ipAddress)); System.out.println("Accepting invite"); toPeer.writeObject(c); controlSocket.close(); this.haveGame = true; System.out.println("Starting game"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
2882dc39-5744-4caa-b5f2-f5ba42a6e30b
public void setHaveGame() { this.haveGame = true; this.isTurn = true; System.out.println("Setting have game"); }
7e2008e1-54ac-452b-839e-b3a81a7b5c25
public boolean getHaveGame() { return this.haveGame; }
e599bfd2-c18c-4492-b482-77125500f79b
public String getIPAddress() throws SocketException { Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); while(en.hasMoreElements()) { NetworkInterface i = en.nextElement(); Enumeration<InetAddress> addresses = i.getInetAddresses(); while(addresses.hasMoreElements()) { InetAddress ad = addresses.nextElement(); if(!ad.isLoopbackAddress() && ad instanceof Inet4Address) { System.out.println(ad.getHostAddress()); return ad.getHostAddress(); } } } return null; }
887cc8b0-eef9-4126-bf4c-c7cd0257d52b
public void rejectInvite(UserBean possibleOpponent) { Socket controlSocket; try { controlSocket = new Socket(possibleOpponent.getIpAddress(), Model.controlDataSocketNumber); ObjectOutputStream toPeer = new ObjectOutputStream(controlSocket.getOutputStream()); ClientMessage c = new ClientMessage(); c.setCommand("REJECT"); c.setUser(new UserBean(this.hostName, this.userName, this.ipAddress)); System.out.println("Rejecting invite"); toPeer.writeObject(c); controlSocket.close(); } catch (IOException e) { e.printStackTrace(); } }
a3057cae-cb5d-41de-a481-77e1f13878d2
public boolean makeMove(int row, int col) { boolean canMove = false; if(this.haveGame && isTurn) { if(board[row][col] == 0) { canMove = true; board[row][col] = piece; Socket controlSocket; try { System.out.println("Setting up connection to: " + opponent.getIpAddress()); controlSocket = new Socket(opponent.getIpAddress(), Model.gameDataSocketNumber); ObjectOutputStream toPeer = new ObjectOutputStream(controlSocket.getOutputStream()); ClientMessage c = new ClientMessage(); c.setCommand("MOVE"); c.setRow(row); c.setColumn(col); c.setPiece(this.piece); c.setUser(new UserBean(this.hostName, this.userName, this.ipAddress)); toPeer.writeObject(c); controlSocket.close(); } catch (IOException e) { e.printStackTrace(); } } } this.isTurn = false; return canMove; }
fd9e64db-4e16-4a91-9c1c-d1c3c71a9193
public int getPiece() { return this.piece; }
5f705f24-dae4-49c9-a97a-00188eb11c61
private int generatePiece() { int toReturn = 0; int max = 1; int min = -1; while(toReturn == 0) { toReturn = r.nextInt((max - min) + 1) + min; } System.out.println(toReturn); return toReturn; }
6d6c41e6-bf19-43aa-8ed5-e724b3d41d71
public void setOpBoardMove(int row, int col, int oppiece) { board[row][col] = oppiece; }
319ae1a7-1377-4dbc-a2f2-183bf9f82965
public int haveWinCondition() { int win = 0; //Horizontal for(int i = 0; i < 3; i++) { int total = 0; for(int j = 0; j < 3; j++) { total += board[i][j]; } if(total == (this.piece * 3)) { //Win win = 1; }else if(total == (this.piece * -3)) { //lose win = -1; } } //Vertical for(int i = 0; i < 3; i++) { int total = 0; for(int j = 0; j < 3; j++) { total += board[j][i]; } if(total == (this.piece * 3)) { //Win win = 1; }else if(total == (this.piece * -3)) { //lose win = -1; } } //Diagonal int rightDiag = board[0][0] + board[1][1] + board[2][2]; int leftDiag = board[0][2] + board[1][1] + board[2][0]; if(rightDiag == (this.piece * 3)) { win = 1; }else if(rightDiag == (this.piece * -3)) { win = -1; }else if(leftDiag == (this.piece * 3)) { win = 1; }else if(leftDiag == (this.piece * -3)) { win = -1; } if(win == 0) { int numFill = 0; for(int i = 0; i < 3; i++) { for(int j = 0; j < 3; j++) { if(board[i][j] != 0) { numFill++; } } } if(numFill == 9) { win = -2; } } return win; }
6b648afa-e5e5-4d54-bb14-f5b9c9844108
public void endGame() { this.opponent = null; this.haveGame = false; this.isTurn = false; for(int i = 0; i < 3; i++) { for(int j = 0; j < 3; j++) { board[i][j] = 0; } } }
cc2637a4-fa99-43e9-9e20-6ec7c25b8014
public boolean isTurn() { return isTurn; }
72961092-a41d-4d0b-a9cb-865b70e434a5
public void setIsTurn() { this.isTurn = true; }
78744686-8307-4b09-8dd8-ac6d6265f095
public boolean isAccept() { return accept; }
bc15e579-cb0e-47db-bf67-71b611f1c984
public void setAccept(boolean accept) { this.accept = accept; }
9cb11e93-0d88-4d64-a05d-cc6e05298d8b
public UserBean getUser() { return user; }
4d997563-a1be-416e-bdcc-2c9db020774c
public void setUser(UserBean user) { this.user = user; }
0653f528-c63b-421d-9810-e360c7d66a87
public String getCommand() { return command; }
7cca5525-d7c4-4c76-a7d6-daa2b06062d3
public void setCommand(String command) { this.command = command; }
48f52777-b28f-4632-9beb-704d4eb79484
public int getRow() { return row; }
0b0d4f08-39e8-4b3b-b538-e953f0eb75d8
public void setRow(int row) { this.row = row; }
4cbe8670-cd7d-4037-b3f0-9aad426d7659
public int getColumn() { return column; }
3e68555a-e502-413b-afee-93c413b9b281
public void setColumn(int column) { this.column = column; }
9eb19ed9-72d3-4955-af38-01beead91d41
public int getPiece() { return piece; }
620064aa-3f80-4566-9818-1ce5fa620410
public void setPiece(int piece) { this.piece = piece; }
4081783b-bc5d-488d-9291-d143072e8c0b
public ClientMessage() { }