-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFriend.java
More file actions
111 lines (99 loc) · 3.55 KB
/
Friend.java
File metadata and controls
111 lines (99 loc) · 3.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
/**
* a class for both client and server.
* it can send and get data.
* the data that a friend send,is a String.
* a string that refers to what song the friend is listening.
* don't forget in constructor you should give port and ip of client and server/
*
* @author pouyan hesabi and omidmahyar.
* @version 1.0
* @since 6/29/2019
*/
public class Friend {
volatile private String myIpHost;
volatile private int myPort;
volatile private String clientIpHost;
volatile private int clientPort;
volatile private String inputData;
volatile private String username;
volatile private static ArrayList<String> allData = new ArrayList<>(50);
public static synchronized ArrayList<String> getAllData() {
return allData;
}
public synchronized String getInputData() {
return inputData;
}
public synchronized void setInputData(String inputData) {
this.inputData = inputData;
}
public Friend(String username, String myIpHost, int myPort, String clientIpHost, int clientPort) {
this.username = username;
this.myIpHost = myIpHost;
this.myPort = myPort;
this.clientIpHost = clientIpHost;
this.clientPort = clientPort;
new Thread(new ServerThread()).start();
}
/**
* a method that send the string that show a friend is listening the music with specific time and username.
* it doesn't return anything.
*
* @param outputData
* @throws IOException
*/
public synchronized void sendData(String outputData) throws IOException {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");
Date date = new Date();
String currentTime = simpleDateFormat.format(date);
Socket client = new Socket(clientIpHost, clientPort);
DataOutputStream out = new DataOutputStream(client.getOutputStream());
String pass = "\t" + currentTime + "\n" + outputData;
out.writeUTF(username + pass);
//System.out.println(username+pass);
// getAllData().add(username+pass);
// mainFrame.getCopyOfAllData().add(username+pass);
out.flush();
client.close();
}
/**
* a class that implements an interface class(Runnable)
* only one method that get data with DataInputStream and throws IOException.
*
* @author pouyan hesabi and omid mahyar.
* @version 1.0
* @see java.io.DataInputStream
* @see java.lang.Thread
*/
private class ServerThread implements Runnable {
@Override
public synchronized void run() {
Socket socket = null;
ServerSocket server = null;
int i = 0;
try {
server = new ServerSocket(myPort);
while (true) {
socket = server.accept();
DataInputStream in = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
setInputData(in.readUTF());
System.out.println(getInputData());
getAllData().add(getInputData());
//getAllData().add(getInputData());
socket.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}