-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
138 lines (104 loc) · 3.99 KB
/
Main.java
File metadata and controls
138 lines (104 loc) · 3.99 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import java.io.*;
class dimuMessage implements Serializable
{
public int number, id;
public dimuMessage(int number, int id)
{
this.number = number; this.id = id;
}
@Override
public String toString() {
return "Object Message {" +
"number=" + number +
", id=" + id +
'}';
}
}
public class Main
{
static ObjectOutputStream objectOutputAB = null;
static ObjectInputStream objectInputAB = null;
static ObjectOutputStream objectOutputBC = null;
static ObjectInputStream objectInputBC = null;
public static void main(String[] args) throws Exception
{
PipedOutputStream posAB = new PipedOutputStream(); // A sends objects to B
PipedInputStream pisBA = new PipedInputStream(posAB);
PipedOutputStream posBA = new PipedOutputStream(); // B sends primitives to A
PipedInputStream pisAB = new PipedInputStream(posBA);
PipedOutputStream posBC = new PipedOutputStream(); // B sends objects to C
PipedInputStream pisCB = new PipedInputStream(posBC);
PipedOutputStream posCA = new PipedOutputStream(); // C sends primitives to A
PipedInputStream pisAC = new PipedInputStream(posCA);
Thread threadA = new Thread()
{
public void run()
{
try {
//A sends OBJECTS to B
dimuMessage m = new dimuMessage(777, 1);
System.out.println("A Sending object to B.... " + "\n" + m.toString());
objectOutputAB = new ObjectOutputStream(posAB);
objectOutputAB.writeObject(m);
objectOutputAB.flush();
objectOutputAB.close();
System.out.println("A Receiving primitive from B: " + pisBA.read());
pisBA.close();
System.out.println("A Receiving primitive from C: " + pisAC.read());
pisAC.close();
}
catch (Exception e) {
}
}
};
Thread threadB = new Thread()
{
public void run()
{
try {
//B sends primitive to A
System.out.println("B Sending primitive to A: " + 2);
posBA.write(2);
posBA.flush();
posBA.close();
//B sends OBJECTS to C
dimuMessage m = new dimuMessage(666, 2);
System.out.println("B Sending object to C... " + "\n" + m.toString());
objectOutputBC = new ObjectOutputStream(posBC);
objectOutputBC.writeObject(m);
objectOutputBC.flush();
objectOutputBC.close();
//B receives objects from A
objectInputBC = new ObjectInputStream(pisAB);
System.out.println("B Receiving object from A " + '\n' + objectInputBC.readObject().toString());
objectInputBC.close();
}
catch (Exception e) {
}
}
};
Thread threadC = new Thread()
{
public void run()
{
try
{
//C sends primitive to A
System.out.println("C Sending primitive to A: " + 3);
posCA.write(3);
posCA.flush();
posCA.close();
//C receives object from B
objectInputAB = new ObjectInputStream(pisCB);
System.out.println("C Receiving object from B: " + objectInputAB.readObject().toString());
objectInputAB.close();
}
catch (Exception e) {
}
}
};
threadA.start();
threadB.start();
threadC.start();
}
}