Skip to content

Commit

Permalink
change grid address and codec factory
Browse files Browse the repository at this point in the history
  • Loading branch information
息羽 committed Mar 7, 2016
1 parent 0c96e5e commit e5b3a76
Show file tree
Hide file tree
Showing 34 changed files with 540 additions and 123 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,9 @@
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.esotericsoftware</groupId>
<groupId>com.esotericsoftware.kryo</groupId>
<artifactId>kryo</artifactId>
<version>3.0.3</version>
<version>2.21</version>
<optional>true</optional>
</dependency>
<dependency>
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/darks/grid/GridContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
package darks.grid;

import java.io.Serializable;
import java.net.InetSocketAddress;

import darks.grid.beans.GridAddress;
import darks.grid.beans.MachineInfo;
import darks.grid.commons.MachineInfoFactory;
import darks.grid.config.GridConfiguration;
Expand All @@ -37,7 +37,7 @@ public class GridContext implements Serializable, GridManager

private String clusterName;

private InetSocketAddress serverAddress;
private GridAddress serverAddress;

private transient MachineInfoFactory machineInfoFactory;

Expand Down Expand Up @@ -112,12 +112,12 @@ public void setClusterName(String clusterName)
this.clusterName = clusterName;
}

public synchronized InetSocketAddress getServerAddress()
public synchronized GridAddress getServerAddress()
{
return serverAddress;
}

public synchronized void setServerAddress(InetSocketAddress serverAddress)
public synchronized void setServerAddress(GridAddress serverAddress)
{
this.serverAddress = serverAddress;
}
Expand Down
119 changes: 119 additions & 0 deletions src/main/java/darks/grid/beans/GridAddress.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/**
*
* Copyright 2015 The Darks Grid Project (Liu lihua)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package darks.grid.beans;

import java.io.Serializable;
import java.net.InetSocketAddress;
import java.net.SocketAddress;

import darks.grid.utils.StringUtils;

public class GridAddress implements Serializable
{

/**
*
*/
private static final long serialVersionUID = -715203941820641100L;

private String hostName;

private int port;

public GridAddress()
{

}

public GridAddress(InetSocketAddress addr)
{
this(addr.getHostName(), addr.getPort());
}

public GridAddress(String hostName, int port)
{
this.hostName = hostName;
this.port = port;
}

public InetSocketAddress getSocketAddress()
{
return new InetSocketAddress(hostName, port);
}

public static GridAddress wrap(InetSocketAddress addr)
{
return new GridAddress(addr);
}

public static GridAddress wrap(SocketAddress addr)
{
return wrap((InetSocketAddress) addr);
}

public String getHostName() {
return hostName;
}

public void setHostName(String hostName) {
this.hostName = hostName;
}

public int getPort() {
return port;
}

public void setPort(int port) {
this.port = port;
}

@Override
public String toString() {
return StringUtils.stringBuffer(hostName, ':', port);
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((hostName == null) ? 0 : hostName.hashCode());
result = prime * result + port;
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
GridAddress other = (GridAddress) obj;
if (hostName == null) {
if (other.hostName != null)
return false;
} else if (!hostName.equals(other.hostName))
return false;
if (port != other.port)
return false;
return true;
}


}
5 changes: 2 additions & 3 deletions src/main/java/darks/grid/beans/NodeId.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@

import java.io.ByteArrayOutputStream;
import java.lang.management.ManagementFactory;
import java.net.InetSocketAddress;

import darks.grid.GridRuntime;
import darks.grid.GridException;
import darks.grid.GridRuntime;
import darks.grid.utils.BytesUtils;
import darks.grid.utils.NetworkUtils;

Expand All @@ -41,7 +40,7 @@ public static String localId()
{
byte[] macBytes = NetworkUtils.getMacBytes();
String proccessId = ManagementFactory.getRuntimeMXBean().getName();
InetSocketAddress ipAddress = GridRuntime.network().getBindAddress();
GridAddress ipAddress = GridRuntime.network().getBindAddress();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
if (macBytes != null)
baos.write(macBytes);
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/darks/grid/beans/meta/MasterMeta.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

package darks.grid.beans.meta;

import java.net.InetSocketAddress;
import darks.grid.beans.GridAddress;

public class MasterMeta extends BaseMeta
{
Expand All @@ -29,14 +29,14 @@ public class MasterMeta extends BaseMeta

private String nodeId;

private InetSocketAddress address;
private GridAddress address;

public MasterMeta()
{

}

public MasterMeta(String nodeId, InetSocketAddress address)
public MasterMeta(String nodeId, GridAddress address)
{
super();
this.nodeId = nodeId;
Expand All @@ -53,12 +53,12 @@ public void setNodeId(String nodeId)
this.nodeId = nodeId;
}

public InetSocketAddress getAddress()
public GridAddress getAddress()
{
return address;
}

public void setAddress(InetSocketAddress address)
public void setAddress(GridAddress address)
{
this.address = address;
}
Expand Down
72 changes: 72 additions & 0 deletions src/main/java/darks/grid/config/CodecConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/**
*
* Copyright 2015 The Darks Grid Project (Liu lihua)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package darks.grid.config;

import java.util.HashMap;
import java.util.Map;

import darks.grid.network.codec.GridCodec;

public class CodecConfig
{



private String type;

private Class<? extends GridCodec> codecClass = null;

private Map<String, String> parameters = new HashMap<String, String>();

public CodecConfig()
{

}

public void addParameter(String key, String value)
{
parameters.put(key, value);
}

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}

public Class<? extends GridCodec> getCodecClass() {
return codecClass;
}

public void setCodecClass(Class<? extends GridCodec> codecClass) {
this.codecClass = codecClass;
}

public Map<String, String> getParameters() {
return parameters;
}

public void setParameters(Map<String, String> parameters) {
this.parameters = parameters;
}



}
52 changes: 51 additions & 1 deletion src/main/java/darks/grid/config/GridConfigFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import darks.grid.GridException;
import darks.grid.config.EventsConfig.EventsChannelConfig;
import darks.grid.config.MasterConfig.MasterTaskConfig;
import darks.grid.network.codec.GridCodec;
import darks.grid.utils.IOUtils;
import darks.grid.utils.ReflectUtils;

Expand Down Expand Up @@ -97,7 +98,7 @@ private static GridConfiguration parseDocument(Document doc)
Element el = (Element) node;
if ("network".equalsIgnoreCase(el.getNodeName()))
{
parseAttrForObject(el, config.getNetworkConfig());
parseNetwork(config, el);
}
else if ("task".equalsIgnoreCase(el.getNodeName()))
{
Expand Down Expand Up @@ -136,6 +137,55 @@ else if ("constant".equalsIgnoreCase(el.getNodeName()))
return config;
}

private static void parseNetwork(GridConfiguration config, Element el)
{
parseAttrForObject(el, config.getNetworkConfig());
NodeList nodesList = el.getChildNodes();
for (int i = 0; i < nodesList.getLength(); i++)
{
Node node = nodesList.item(i);
if (node instanceof Element)
{
Element elChild = (Element) node;
if ("codec".equalsIgnoreCase(elChild.getNodeName()))
{
parseNetworkCodec(config.getNetworkConfig().getCodecConfig(), elChild);
}
}
}
}

@SuppressWarnings("unchecked")
private static void parseNetworkCodec(CodecConfig config, Element el)
{
try
{
NamedNodeMap nameNodes = el.getAttributes();
for (int i = 0; i < nameNodes.getLength(); i++)
{
Node node = nameNodes.item(i);
String attrName = node.getNodeName().trim();
String attrValue = node.getNodeValue().trim();
if ("type".equalsIgnoreCase(attrName))
{
config.setType(attrValue);
}
else if ("class".equalsIgnoreCase(attrName))
{
config.setCodecClass((Class<? extends GridCodec>) Class.forName(attrValue));
}
else
{
config.addParameter(attrName, attrValue);
}
}
}
catch (Exception e)
{
throw new GridException("Fail to parse codec. Cause " + e.getMessage(), e);
}
}

private static void parseEvents(GridConfiguration config, Element el)
{
parseAttrForObject(el, config.getEventsConfig());
Expand Down
8 changes: 7 additions & 1 deletion src/main/java/darks/grid/config/NetworkConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ public class NetworkConfig

private boolean cacheHistoryNodes = true;

private CodecConfig codecConfig = new CodecConfig();

public NetworkConfig()
{

Expand Down Expand Up @@ -300,8 +302,12 @@ public void setConnectFailRetry(int connectFailRetry)
{
this.connectFailRetry = connectFailRetry;
}

public CodecConfig getCodecConfig() {
return codecConfig;
}

@Override
@Override
public String toString()
{
return "NetworkConfig [bindHost=" + bindHost + ", bindPort=" + bindPort
Expand Down
Loading

0 comments on commit e5b3a76

Please sign in to comment.