Sunteți pe pagina 1din 3

// **********************************************************************

//
// Copyright (c) 2003-2013 ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

import Demo.*;

public class AllSensors extends Ice.Application {

public String getName() {


File nama = new File("C:\\Users\\inad\\eclipse-
workspace\\Tugas1\\bin\\Alice_Location.txt");
String[] user = nama.getName().split("_");
return user[0];
}

public void usage() {


System.out.println("Usage: " + appName() + " [--datagram|--twoway|--
oneway] [topic]");
}

public int run(String[] args) {


String option = "None";
String topicName = "sensor";
int i;

for (i = 0; i < args.length; ++i) {


String oldoption = option;
if (args[i].equals("--datagram")) {
option = "Datagram";
} else if (args[i].equals("--twoway")) {
option = "Twoway";
} else if (args[i].equals("--oneway")) {
option = "Oneway";
} else if (args[i].startsWith("--")) {
usage();
return 1;
} else {
topicName = args[i++];
break;
}

if (!oldoption.equals(option) && !oldoption.equals("None")) {


usage();
return 1;
}
}

if (i != args.length) {
usage();
return 1;
}

IceStorm.TopicManagerPrx manager = IceStorm.TopicManagerPrxHelper


.checkedCast(communicator().propertyToProxy("TopicManager.P
roxy"));
if (manager == null) {
System.err.println("invalid proxy");
return 1;
}

//
// Retrieve the topic.
//
IceStorm.TopicPrx topic;
try {
topic = manager.retrieve(topicName);
} catch (IceStorm.NoSuchTopic e) {
try {
topic = manager.create(topicName);
} catch (IceStorm.TopicExists ex) {
System.err.println(appName() + ": temporary failure, try
again.");
return 1;
}
}

//
// Get the topic's publisher object, and create a Clock proxy with
// the mode specified as an argument of this application.
//
Ice.ObjectPrx publisher = topic.getPublisher();
if (option.equals("Datagram")) {
publisher = publisher.ice_datagram();
} else if (option.equals("Twoway")) {
// Do nothing.
} else // if(oneway)
{
publisher = publisher.ice_oneway();
}
ClockPrx clock = ClockPrxHelper.uncheckedCast(publisher);

System.out.println("publishing tick events. Press ^C to terminate the


application.");
try {

BufferedReader locats = new BufferedReader(


new FileReader("C:\\Users\\inad\\eclipse-
workspace\\Tugas1\\bin\\"+getName()+"_Location.txt"));
BufferedReader tempes = new BufferedReader(
new FileReader("C:\\Users\\inad\\eclipse-
workspace\\Tugas1\\bin\\"+getName()+"_Temperature.txt"));
BufferedReader uvs = new BufferedReader(
new FileReader("C:\\Users\\inad\\eclipse-
workspace\\Tugas1\\bin\\"+getName()+"_UVR.txt"));
while (true) {
try {
int x = 0;
String[] arraylocat = locats.readLine().split(",");
String all1 = "<"+getName()+"> "+uvs.readLine() + ",
" + tempes.readLine() + " C, " + arraylocat[x];
int z = 1;

while (z <= Integer.parseInt(arraylocat[x + 1])) {


clock.tick(all1);
try {
Thread.currentThread().sleep(1000);
} catch (java.lang.InterruptedException e) {
}
z++;
}
} catch (Exception e) {
run(args);
}
}
} catch (Exception ex) {
System.out.println("Username tidak sesuai");
}
return 0;
}

public static void main(String[] args) {


int status = 0;
AllSensors app = new AllSensors();

status = app.main("AllSensors", args, "config.pub");


System.exit(status);
}
}

S-ar putea să vă placă și