Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
AndroidOnlabKviz2018
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
KGregPFerenc
AndroidOnlabKviz2018
Commits
41c1b120
Commit
41c1b120
authored
May 1, 2018
by
fpeterfalvi
Browse files
Options
Downloads
Patches
Plain Diff
Writing observer name and connecting to server in Swing.
parent
b5eb0fe4
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
KvizObserver/pom.xml
+2
-2
2 additions, 2 deletions
KvizObserver/pom.xml
KvizObserver/src/main/java/Main.java
+132
-20
132 additions, 20 deletions
KvizObserver/src/main/java/Main.java
with
134 additions
and
22 deletions
KvizObserver/pom.xml
+
2
−
2
View file @
41c1b120
...
@@ -80,8 +80,8 @@ The project was originally started in December 2002 by Arthur van Hoff at Strang
...
@@ -80,8 +80,8 @@ The project was originally started in December 2002 by Arthur van Hoff at Strang
<artifactId>
maven-compiler-plugin
</artifactId>
<artifactId>
maven-compiler-plugin
</artifactId>
<version>
3.3
</version>
<version>
3.3
</version>
<configuration>
<configuration>
<source>
${jvm.version}
</source>
<source>
1.9
</source>
<target>
${jvm.version}
</target>
<target>
1.9
</target>
</configuration>
</configuration>
</plugin>
</plugin>
<plugin>
<plugin>
...
...
This diff is collapsed.
Click to expand it.
KvizObserver/src/main/java/Main.java
+
132
−
20
View file @
41c1b120
import
java.awt.*
;
import
java.awt.event.ActionEvent
;
import
java.awt.event.ActionListener
;
import
java.io.BufferedReader
;
import
java.io.BufferedReader
;
import
java.io.IOException
;
import
java.io.IOException
;
import
java.io.InputStreamReader
;
import
java.io.InputStreamReader
;
...
@@ -15,7 +18,8 @@ import javax.jmdns.JmDNS;
...
@@ -15,7 +18,8 @@ import javax.jmdns.JmDNS;
import
javax.jmdns.ServiceEvent
;
import
javax.jmdns.ServiceEvent
;
import
javax.jmdns.ServiceInfo
;
import
javax.jmdns.ServiceInfo
;
import
javax.jmdns.ServiceListener
;
import
javax.jmdns.ServiceListener
;
import
javax.swing.SwingWorker
;
import
javax.swing.*
;
import
javax.swing.border.EmptyBorder
;
import
model.ServerModel
;
import
model.ServerModel
;
...
@@ -42,6 +46,9 @@ public class Main {
...
@@ -42,6 +46,9 @@ public class Main {
private
static
PrintWriter
out
=
null
;
private
static
PrintWriter
out
=
null
;
private
static
JFrame
frame
;
private
static
JPanel
mainPanel
;
private
static
JPanel
serverListPanel
;
private
static
class
SampleListener
implements
ServiceListener
{
private
static
class
SampleListener
implements
ServiceListener
{
@Override
@Override
...
@@ -101,31 +108,58 @@ public class Main {
...
@@ -101,31 +108,58 @@ public class Main {
}
}
public
static
void
main
(
String
[]
args
)
throws
InterruptedException
{
public
static
void
main
(
String
[]
args
)
throws
InterruptedException
{
try
{
servers
=
new
ArrayList
<
ServerModel
>();
servers
=
new
ArrayList
<
ServerModel
>();
threads
=
new
ArrayList
<
Thread
>();
threads
=
new
ArrayList
<
Thread
>();
System
.
out
.
println
(
"Set The ObserverName:"
);
frame
=
new
JFrame
(
"KvizObserver"
);
Scanner
s
=
new
Scanner
(
System
.
in
);
mainPanel
=
new
JPanel
(
new
CardLayout
());
CLIENT_NAME
=
s
.
nextLine
();
JPanel
observerNamePanel
=
new
JPanel
();
observerNamePanel
.
setLayout
(
new
BoxLayout
(
observerNamePanel
,
BoxLayout
.
Y_AXIS
));
JLabel
observerNameLabel
=
new
JLabel
(
"Observer name:"
);
observerNamePanel
.
add
(
observerNameLabel
);
JTextField
observerNameTextField
=
new
JTextField
();
observerNamePanel
.
add
(
observerNameTextField
);
JButton
startSearchingButton
=
new
JButton
(
"Start searching servers"
);
startSearchingButton
.
addActionListener
(
new
StartSearchingButtonListener
(
observerNameTextField
));
observerNamePanel
.
add
(
startSearchingButton
);
mainPanel
.
add
(
observerNamePanel
,
"OBSERVER_NAME_PANEL"
);
serverListPanel
=
new
JPanel
();
serverListPanel
.
setLayout
(
new
BoxLayout
(
serverListPanel
,
BoxLayout
.
Y_AXIS
));
mainPanel
.
add
(
serverListPanel
,
"SERVER_LIST_PANEL"
);
frame
.
add
(
mainPanel
,
BorderLayout
.
NORTH
);
frame
.
setPreferredSize
(
new
Dimension
(
300
,
200
));
frame
.
pack
();
frame
.
setLocationRelativeTo
(
null
);
frame
.
setDefaultCloseOperation
(
JFrame
.
EXIT_ON_CLOSE
);
frame
.
setVisible
(
true
);
//try {
//System.out.println("Set The ObserverName:");
//Scanner s = new Scanner(System.in);
//CLIENT_NAME = s.nextLine();
// Create a JmDNS instance
// Create a JmDNS instance
JmDNS
jmdns
=
JmDNS
.
create
(
InetAddress
.
getLocalHost
());
//
JmDNS jmdns = JmDNS.create(InetAddress.getLocalHost());
// Add a service listener
// Add a service listener
jmdns
.
addServiceListener
(
"_http._tcp.local."
,
new
SampleListener
());
//
jmdns.addServiceListener("_http._tcp.local.", new SampleListener());
// Wait a bit
// Wait a bit
Thread
.
sleep
(
3000
);
//
Thread.sleep(3000);
String
msg
=
""
;
//
String msg = "";
s
=
new
Scanner
(
System
.
in
);
/*Scanner
s = new Scanner(System.in);
System.out.println("Type 'Connect' to Connect:");
System.out.println("Type 'Connect' to Connect:");
msg = s.nextLine();
msg = s.nextLine();
if(msg.equals("Connect"))
if(msg.equals("Connect"))
...
@@ -137,16 +171,93 @@ public class Main {
...
@@ -137,16 +171,93 @@ public class Main {
if(msg.equals("Disconnect"))
if(msg.equals("Disconnect"))
{
{
onDisconnect();
onDisconnect();
}*/
/*} catch (UnknownHostException e) {
System.out.println(e.getMessage());
} catch (IOException e) {
System.out.println(e.getMessage());*/
//}
}
}
final
static
class
StartSearchingButtonListener
implements
ActionListener
{
private
JTextField
tf
;
public
StartSearchingButtonListener
(
JTextField
tf
)
{
this
.
tf
=
tf
;
}
public
void
actionPerformed
(
ActionEvent
ae
)
{
CLIENT_NAME
=
tf
.
getText
();
CardLayout
cl
=
(
CardLayout
)(
mainPanel
.
getLayout
());
cl
.
show
(
mainPanel
,
"SERVER_LIST_PANEL"
);
try
{
// Create a JmDNS instance
JmDNS
jmdns
=
JmDNS
.
create
(
InetAddress
.
getLocalHost
());
// Add a service listener
jmdns
.
addServiceListener
(
"_http._tcp.local."
,
new
SampleListener
());
}
catch
(
UnknownHostException
e
)
{
}
catch
(
UnknownHostException
e
)
{
System
.
out
.
println
(
e
.
getMessage
());
System
.
out
.
println
(
e
.
getMessage
());
}
catch
(
IOException
e
)
{
}
catch
(
IOException
e
)
{
System
.
out
.
println
(
e
.
getMessage
());
System
.
out
.
println
(
e
.
getMessage
());
}
}
}
}
private
static
void
addServerToServerListPanel
(
ServerModel
serverModel
)
{
JPanel
serverPanel
=
new
JPanel
();
serverPanel
.
setLayout
(
new
FlowLayout
(
FlowLayout
.
LEFT
));
JLabel
serverName
=
new
JLabel
(
serverModel
.
getHostName
());
serverPanel
.
add
(
serverName
);
serverPanel
.
add
(
Box
.
createRigidArea
(
new
Dimension
(
5
,
0
)));
JButton
connectButton
=
new
JButton
(
"Connect"
);
serverPanel
.
add
(
connectButton
);
serverPanel
.
add
(
Box
.
createRigidArea
(
new
Dimension
(
5
,
0
)));
JButton
disconnectButton
=
new
JButton
(
"Disconnect"
);
disconnectButton
.
setEnabled
(
false
);
serverPanel
.
add
(
disconnectButton
);
connectButton
.
addActionListener
(
new
ConnectButtonListener
(
serverModel
,
connectButton
,
disconnectButton
));
disconnectButton
.
addActionListener
(
new
DisconnectButtonListener
(
serverModel
,
connectButton
,
disconnectButton
));
serverListPanel
.
add
(
serverPanel
);
serverListPanel
.
revalidate
();
serverListPanel
.
repaint
();
}
final
static
class
ConnectButtonListener
implements
ActionListener
{
private
ServerModel
sm
;
private
JButton
connectButton
;
private
JButton
disconnectButton
;
public
ConnectButtonListener
(
ServerModel
sm
,
JButton
connectButton
,
JButton
disconnectButton
)
{
this
.
sm
=
sm
;
this
.
connectButton
=
connectButton
;
this
.
disconnectButton
=
disconnectButton
;
}
public
void
actionPerformed
(
ActionEvent
ae
)
{
onConnect
(
servers
.
indexOf
(
sm
));
connectButton
.
setEnabled
(
false
);
disconnectButton
.
setEnabled
(
true
);
}
}
final
static
class
DisconnectButtonListener
implements
ActionListener
{
private
ServerModel
sm
;
private
JButton
connectButton
;
private
JButton
disconnectButton
;
public
DisconnectButtonListener
(
ServerModel
sm
,
JButton
connectButton
,
JButton
disconnectButton
)
{
this
.
sm
=
sm
;
this
.
connectButton
=
connectButton
;
this
.
disconnectButton
=
disconnectButton
;
}
public
void
actionPerformed
(
ActionEvent
ae
)
{
onDisconnect
();
disconnectButton
.
setEnabled
(
false
);
connectButton
.
setEnabled
(
true
);
}
}
}
class
CommunicationThread
implements
Runnable
{
class
CommunicationThread
implements
Runnable
{
...
@@ -191,6 +302,7 @@ public class Main {
...
@@ -191,6 +302,7 @@ public class Main {
serverModel
.
setHostName
(
params
[
1
]);
serverModel
.
setHostName
(
params
[
1
]);
//Kiirni
//Kiirni
System
.
out
.
println
(
"Server Add: "
+
serverModel
.
getHostName
());
System
.
out
.
println
(
"Server Add: "
+
serverModel
.
getHostName
());
addServerToServerListPanel
(
serverModel
);
//
//
break
;
break
;
case
"ConnectionLost"
:
case
"ConnectionLost"
:
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment