Last change
on this file since 15014 was
15014,
checked in by pfleck, 7 years ago
|
Added code and tools for the ExternalEvaluationProblem. (e.g. Java-side evaluation)
|
File size:
1.7 KB
|
Line | |
---|
1 | package com.heuristiclab.problems.externalevaluation;
|
---|
2 |
|
---|
3 | import java.io.*;
|
---|
4 | import java.net.*;
|
---|
5 |
|
---|
6 | import com.google.protobuf.Message;
|
---|
7 | import com.google.protobuf.Message.Builder;
|
---|
8 |
|
---|
9 | public class ServerSocketChannel extends Channel {
|
---|
10 | private ServerSocket serverSocket;
|
---|
11 | private Socket clientSocket;
|
---|
12 | private int port;
|
---|
13 | private InetAddress bindAddress;
|
---|
14 | private StreamChannel streamChannel;
|
---|
15 |
|
---|
16 | public ServerSocketChannel(int port) {
|
---|
17 | this.port = port;
|
---|
18 | this.bindAddress = null;
|
---|
19 | }
|
---|
20 |
|
---|
21 | public ServerSocketChannel(int port, String ipAddress) {
|
---|
22 | this.port = port;
|
---|
23 | try {
|
---|
24 | this.bindAddress = InetAddress.getByName(ipAddress);
|
---|
25 | } catch (UnknownHostException e) {
|
---|
26 | // TODO Auto-generated catch block
|
---|
27 | e.printStackTrace();
|
---|
28 | }
|
---|
29 | }
|
---|
30 |
|
---|
31 | @Override
|
---|
32 | public void open() throws IOException {
|
---|
33 | super.open();
|
---|
34 | if (serverSocket == null) {
|
---|
35 | if (bindAddress == null)
|
---|
36 | serverSocket = new ServerSocket(port, 1);
|
---|
37 | else
|
---|
38 | serverSocket = new ServerSocket(port, 1, bindAddress);
|
---|
39 | }
|
---|
40 | listenForClient();
|
---|
41 | }
|
---|
42 |
|
---|
43 | private void listenForClient() throws IOException {
|
---|
44 | clientSocket = serverSocket.accept();
|
---|
45 | streamChannel = new StreamChannel(clientSocket.getInputStream(), clientSocket.getOutputStream());
|
---|
46 | streamChannel.open();
|
---|
47 | }
|
---|
48 |
|
---|
49 | @Override
|
---|
50 | public Message receive(Builder builder) throws IOException {
|
---|
51 | return streamChannel.receive(builder);
|
---|
52 | }
|
---|
53 |
|
---|
54 | @Override
|
---|
55 | public void send(Message msg) throws IOException {
|
---|
56 | streamChannel.send(msg);
|
---|
57 | }
|
---|
58 |
|
---|
59 | @Override
|
---|
60 | public void close() throws IOException {
|
---|
61 | streamChannel.close();
|
---|
62 | streamChannel = null;
|
---|
63 | clientSocket.close();
|
---|
64 | clientSocket = null;
|
---|
65 | serverSocket.close();
|
---|
66 | serverSocket = null;
|
---|
67 | }
|
---|
68 |
|
---|
69 | }
|
---|
Note: See
TracBrowser
for help on using the repository browser.